Class: Sentry::StructuredLogger

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

Overview

The StructuredLogger class implements Sentry’s SDK telemetry logs protocol. It provides methods for logging messages at different severity levels and sending them to Sentry with structured data.

This class follows the Sentry Logs Protocol as defined in: develop.sentry.dev/sdk/telemetry/logs/

Examples:

Basic usage

Sentry.logger.info("User logged in", user_id: 123)

With structured data

Sentry.logger.warn("API request failed",
  status_code: 404,
  endpoint: "/api/users",
  request_id: "abc-123"
)

With a message template

# Using positional parameters
Sentry.logger.info("User %s logged in", ["Jane Doe"])

# Using hash parameters
Sentry.logger.info("User %{name} logged in", name: "Jane Doe")

# Using hash parameters and extra attributes
Sentry.logger.info("User %{name} logged in", name: "Jane Doe", user_id: 312)

See Also:

Constant Summary collapse

LEVELS =

Severity number mapping for log levels according to the Sentry Logs Protocol

{
  trace: 1,
  debug: 5,
  info: 9,
  warn: 13,
  error: 17,
  fatal: 21
}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ StructuredLogger

Initializes a new StructuredLogger instance

Parameters:



51
52
53
# File 'lib/sentry/structured_logger.rb', line 51

def initialize(config)
  @config = config
end

Instance Method Details

#debug(message, parameters = [], **attributes) ⇒ LogEvent?

Logs a message at DEBUG level

Parameters:

  • message (String)

    The log message

  • parameters (Array) (defaults to: [])

    Array of values to replace template parameters in the message

  • attributes (Hash)

    Additional attributes to include with the log

Returns:

  • (LogEvent, nil)

    The created log event or nil if logging is disabled



73
74
75
# File 'lib/sentry/structured_logger.rb', line 73

def debug(message, parameters = [], **attributes)
  log(__method__, message, parameters: parameters, **attributes)
end

#error(message, parameters = [], **attributes) ⇒ LogEvent?

Logs a message at ERROR level

Parameters:

  • message (String)

    The log message

  • parameters (Array) (defaults to: [])

    Array of values to replace template parameters in the message

  • attributes (Hash)

    Additional attributes to include with the log

Returns:

  • (LogEvent, nil)

    The created log event or nil if logging is disabled



106
107
108
# File 'lib/sentry/structured_logger.rb', line 106

def error(message, parameters = [], **attributes)
  log(__method__, message, parameters: parameters, **attributes)
end

#fatal(message, parameters = [], **attributes) ⇒ LogEvent?

Logs a message at FATAL level

Parameters:

  • message (String)

    The log message

  • parameters (Array) (defaults to: [])

    Array of values to replace template parameters in the message

  • attributes (Hash)

    Additional attributes to include with the log

Returns:

  • (LogEvent, nil)

    The created log event or nil if logging is disabled



117
118
119
# File 'lib/sentry/structured_logger.rb', line 117

def fatal(message, parameters = [], **attributes)
  log(__method__, message, parameters: parameters, **attributes)
end

#info(message, parameters = [], **attributes) ⇒ LogEvent?

Logs a message at INFO level

Parameters:

  • message (String)

    The log message

  • parameters (Array) (defaults to: [])

    Array of values to replace template parameters in the message

  • attributes (Hash)

    Additional attributes to include with the log

Returns:

  • (LogEvent, nil)

    The created log event or nil if logging is disabled



84
85
86
# File 'lib/sentry/structured_logger.rb', line 84

def info(message, parameters = [], **attributes)
  log(__method__, message, parameters: parameters, **attributes)
end

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

Logs a message at the specified level

Parameters:

  • level (Symbol)

    The log level (:trace, :debug, :info, :warn, :error, :fatal)

  • message (String)

    The log message

  • parameters (Array, Hash)

    Array or Hash of values to replace template parameters in the message

  • attributes (Hash)

    Additional attributes to include with the log

Returns:

  • (LogEvent, nil)

    The created log event or nil if logging is disabled



129
130
131
132
133
134
135
136
# File 'lib/sentry/structured_logger.rb', line 129

def log(level, message, parameters:, **attributes)
  case parameters
  when Array then
    Sentry.capture_log(message, level: level, severity: LEVELS[level], parameters: parameters, **attributes)
  else
    Sentry.capture_log(message, level: level, severity: LEVELS[level], **parameters)
  end
end

#trace(message, parameters = [], **attributes) ⇒ LogEvent?

Logs a message at TRACE level

Parameters:

  • message (String)

    The log message

  • parameters (Array) (defaults to: [])

    Array of values to replace template parameters in the message

  • attributes (Hash)

    Additional attributes to include with the log

Returns:

  • (LogEvent, nil)

    The created log event or nil if logging is disabled



62
63
64
# File 'lib/sentry/structured_logger.rb', line 62

def trace(message, parameters = [], **attributes)
  log(__method__, message, parameters: parameters, **attributes)
end

#warn(message, parameters = [], **attributes) ⇒ LogEvent?

Logs a message at WARN level

Parameters:

  • message (String)

    The log message

  • parameters (Array) (defaults to: [])

    Array of values to replace template parameters in the message

  • attributes (Hash)

    Additional attributes to include with the log

Returns:

  • (LogEvent, nil)

    The created log event or nil if logging is disabled



95
96
97
# File 'lib/sentry/structured_logger.rb', line 95

def warn(message, parameters = [], **attributes)
  log(__method__, message, parameters: parameters, **attributes)
end