class Sentry::DebugStructuredLogger
is enabled.
It can optionally also send log events to Sentry via the normal structured logger if logging
DebugStructuredLogger is a logger that captures structured log events to a file for debugging purposes.
def capture_log_event(level, message, parameters, **attributes)
def capture_log_event(level, message, parameters, **attributes) log_event_json = { timestamp: Time.now.utc.iso8601, level: level.to_s, message: message, parameters: parameters, attributes: attributes } File.open(log_file, "a") { |file| file << JSON.dump(log_event_json) << "\n" } log_event_json end
def clear
def clear File.write(log_file, "") if backend.respond_to?(:config) backend.config.sdk_logger.debug("DebugStructuredLogger: Cleared events from #{log_file}") end end
def initialize(configuration)
def initialize(configuration) @log_file = initialize_log_file( configuration.structured_logging.file_path || DEFAULT_LOG_FILE_PATH ) @backend = initialize_backend(configuration) super(@backend) end
def initialize_backend(configuration)
def initialize_backend(configuration) if configuration.enable_logs StructuredLogger.new(configuration) else # Create a no-op logger if logging is disabled NoOpLogger.new end end
def initialize_log_file(log_file_path)
def initialize_log_file(log_file_path) log_file = Pathname(log_file_path) FileUtils.mkdir_p(log_file.dirname) unless log_file.dirname.exist? log_file end
def log(level, message, parameters:, **attributes)
def log(level, message, parameters:, **attributes) log_event = capture_log_event(level, message, parameters, **attributes) backend.log(level, message, parameters: parameters, **attributes) log_event end
def logged_events
def logged_events File.readlines(log_file).map do |line| JSON.parse(line) end end