Class: Sentry::DebugStructuredLogger

Inherits:
SimpleDelegator
  • Object
show all
Defined in:
lib/sentry/debug_structured_logger.rb

Overview

DebugStructuredLogger is a logger that captures structured log events to a file for debugging purposes.

It also sends log events to Sentry via the normal structured logger.

Constant Summary collapse

DEFAULT_LOG_FILE_PATH =
File.join("log", "sentry_debug_logs.log")

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(configuration) ⇒ DebugStructuredLogger

Returns a new instance of DebugStructuredLogger.



17
18
19
20
21
22
23
24
# File 'lib/sentry/debug_structured_logger.rb', line 17

def initialize(configuration)
  @log_file = initialize_log_file(
    configuration.structured_logging.file_path || DEFAULT_LOG_FILE_PATH
  )
  @backend = initialize_backend(configuration)

  super(@backend)
end

Instance Attribute Details

#backendObject (readonly)

Returns the value of attribute backend.



15
16
17
# File 'lib/sentry/debug_structured_logger.rb', line 15

def backend
  @backend
end

#log_fileObject (readonly)

Returns the value of attribute log_file.



15
16
17
# File 'lib/sentry/debug_structured_logger.rb', line 15

def log_file
  @log_file
end

Instance Method Details

#capture_log_event(level, message, parameters, **attributes) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/sentry/debug_structured_logger.rb', line 41

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

#clearObject



60
61
62
63
64
65
# File 'lib/sentry/debug_structured_logger.rb', line 60

def clear
  File.write(log_file, "")
  if backend.respond_to?(:config)
    backend.config.sdk_logger.debug("DebugStructuredLogger: Cleared events from #{log_file}")
  end
end

#log(level, message, parameters:, **attributes) ⇒ Object



35
36
37
38
39
# File 'lib/sentry/debug_structured_logger.rb', line 35

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

#logged_eventsObject



54
55
56
57
58
# File 'lib/sentry/debug_structured_logger.rb', line 54

def logged_events
  File.readlines(log_file).map do |line|
    JSON.parse(line)
  end
end