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 can optionally also send log events to Sentry via the normal structured logger if logging is enabled.

Defined Under Namespace

Classes: NoOpLogger

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.



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

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.



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

def backend
  @backend
end

#log_fileObject (readonly)

Returns the value of attribute log_file.



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

def log_file
  @log_file
end

Instance Method Details

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



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

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



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

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



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

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



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

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