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.



15
16
17
18
19
20
21
# File 'lib/sentry/debug_structured_logger.rb', line 15

def initialize(configuration)
  @log = DebugLogFile.new(configuration.structured_logging.file_path, File.basename(DEFAULT_LOG_FILE_PATH, ".log"))
  @log_file = @log.path
  @backend = initialize_backend(configuration)

  super(@backend)
end

Instance Attribute Details

#backend ⇒ Object (readonly)

Returns the value of attribute backend.



13
14
15
# File 'lib/sentry/debug_structured_logger.rb', line 13

def backend
  @backend
end

#log_file ⇒ Object (readonly)

Returns the value of attribute log_file.



13
14
15
# File 'lib/sentry/debug_structured_logger.rb', line 13

def log_file
  @log_file
end

Instance Method Details

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



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

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
  }

  @log.append(log_event_json)
  log_event_json
end

#clear ⇒ Object



65
66
67
68
69
70
# File 'lib/sentry/debug_structured_logger.rb', line 65

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

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

Raises:

  • (ArgumentError)


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

def log(level, message = nil, parameters:, **attributes)
  message = yield if block_given?
  raise ArgumentError, "message or block is required" if message.nil?

  log_event = capture_log_event(level, message, parameters, **attributes)
  backend.log(level, message, parameters: parameters, **attributes)
  log_event
end

#logged_events ⇒ Object



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

def logged_events
  @log.entries
end

#start_run ⇒ Object



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

def start_run
  @log.start_run
end