Skip to content

Commit d7546d2

Browse files
committed
YARD docs for structured logger
1 parent 5c4e109 commit d7546d2

File tree

2 files changed

+82
-5
lines changed

2 files changed

+82
-5
lines changed

sentry-ruby/lib/sentry-ruby.rb

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,24 @@ def exception_locals_tp
9696
attr_reader :metrics_aggregator
9797

9898
# @!attribute [r] logger
99-
# @return [Logging::Device]
99+
# Returns the structured logger instance that implements Sentry's SDK telemetry logs protocol.
100+
# This logger is only available when logs are enabled in the configuration.
101+
#
102+
# @example Enable logs in configuration
103+
# Sentry.init do |config|
104+
# config.dsn = "YOUR_DSN"
105+
# config._experiments = { enable_logs: true }
106+
# end
107+
#
108+
# @example Basic usage
109+
# Sentry.logger.info("User logged in successfully", user_id: 123)
110+
# Sentry.logger.error("Failed to process payment",
111+
# transaction_id: "tx_123",
112+
# error_code: "PAYMENT_FAILED"
113+
# )
114+
#
115+
# @see https://develop.sentry.dev/sdk/telemetry/logs/ Sentry SDK Telemetry Logs Protocol
116+
# @return [StructuredLogger, nil] The structured logger instance or nil if logs are disabled
100117
attr_reader :logger
101118

102119
##### Patch Registration #####
@@ -244,7 +261,9 @@ def init(&block)
244261
config = Configuration.new
245262
yield(config) if block_given?
246263

247-
# Public-facing Structured Logger
264+
# Initialize the public-facing Structured Logger if logs are enabled
265+
# This creates a StructuredLogger instance that implements Sentry's SDK telemetry logs protocol
266+
# @see https://develop.sentry.dev/sdk/telemetry/logs/
248267
@logger = StructuredLogger.new(config) if config._experiments[:enable_logs]
249268

250269
config.detect_release
@@ -499,12 +518,19 @@ def capture_check_in(slug, status, **options)
499518
end
500519

501520
# Captures a log event and sends it to Sentry via the currently active hub.
521+
# This is the underlying method used by the StructuredLogger class.
502522
#
503523
# @param message [String] the log message
504524
# @param [Hash] options Extra log event options
505-
# @option options [Symbol] level The log level
525+
# @option options [Symbol] level The log level (:trace, :debug, :info, :warn, :error, :fatal)
526+
# @option options [Integer] severity The severity number according to the Sentry Logs Protocol
527+
# @option options [Hash] Additional attributes to include with the log
506528
#
507-
# @return [LogEvent, nil]
529+
# @example Direct usage (prefer using Sentry.logger instead)
530+
# Sentry.capture_log("User logged in", level: :info, user_id: 123)
531+
#
532+
# @see https://develop.sentry.dev/sdk/telemetry/logs/ Sentry SDK Telemetry Logs Protocol
533+
# @return [LogEvent, nil] The created log event or nil if logging is disabled
508534
def capture_log(message, **options)
509535
return unless initialized?
510536
get_current_hub.capture_log_event(message, **options)

sentry-ruby/lib/sentry/structured_logger.rb

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,27 @@
11
# frozen_string_literal: true
22

33
module Sentry
4+
# The StructuredLogger class implements Sentry's SDK telemetry logs protocol.
5+
# It provides methods for logging messages at different severity levels and
6+
# sending them to Sentry with structured data.
7+
#
8+
# This class follows the Sentry Logs Protocol as defined in:
9+
# https://develop.sentry.dev/sdk/telemetry/logs/
10+
#
11+
# @example Basic usage
12+
# Sentry.logger.info("User logged in", user_id: 123)
13+
#
14+
# @example With structured data
15+
# Sentry.logger.warn("API request failed",
16+
# status_code: 404,
17+
# endpoint: "/api/users",
18+
# request_id: "abc-123"
19+
# )
20+
#
21+
# @see https://develop.sentry.dev/sdk/telemetry/logs/ Sentry SDK Telemetry Logs Protocol
422
class StructuredLogger
5-
# https://develop.sentry.dev/sdk/telemetry/logs/#log-severity-number
23+
# Severity number mapping for log levels according to the Sentry Logs Protocol
24+
# @see https://develop.sentry.dev/sdk/telemetry/logs/#log-severity-number
625
LEVELS = {
726
"trace" => 1,
827
"debug" => 5,
@@ -12,36 +31,68 @@ class StructuredLogger
1231
"fatal" => 21
1332
}.freeze
1433

34+
# @return [Configuration] The Sentry configuration
1535
attr_reader :config
1636

37+
# Initializes a new StructuredLogger instance
38+
# @param config [Configuration] The Sentry configuration
1739
def initialize(config)
1840
@config = config
1941
end
2042

43+
# Logs a message at TRACE level
44+
# @param message [String] The log message
45+
# @param payload [Hash] Additional attributes to include with the log
46+
# @return [LogEvent, nil] The created log event or nil if logging is disabled
2147
def trace(message, payload = {})
2248
log(:trace, message, payload)
2349
end
2450

51+
# Logs a message at DEBUG level
52+
# @param message [String] The log message
53+
# @param payload [Hash] Additional attributes to include with the log
54+
# @return [LogEvent, nil] The created log event or nil if logging is disabled
2555
def debug(message, payload = {})
2656
log(:debug, message, payload)
2757
end
2858

59+
# Logs a message at INFO level
60+
# @param message [String] The log message
61+
# @param payload [Hash] Additional attributes to include with the log
62+
# @return [LogEvent, nil] The created log event or nil if logging is disabled
2963
def info(message, payload = {})
3064
log(:info, message, payload)
3165
end
3266

67+
# Logs a message at WARN level
68+
# @param message [String] The log message
69+
# @param payload [Hash] Additional attributes to include with the log
70+
# @return [LogEvent, nil] The created log event or nil if logging is disabled
3371
def warn(message, payload = {})
3472
log(:warn, message, payload)
3573
end
3674

75+
# Logs a message at ERROR level
76+
# @param message [String] The log message
77+
# @param payload [Hash] Additional attributes to include with the log
78+
# @return [LogEvent, nil] The created log event or nil if logging is disabled
3779
def error(message, payload = {})
3880
log(:error, message, payload)
3981
end
4082

83+
# Logs a message at FATAL level
84+
# @param message [String] The log message
85+
# @param payload [Hash] Additional attributes to include with the log
86+
# @return [LogEvent, nil] The created log event or nil if logging is disabled
4187
def fatal(message, payload = {})
4288
log(:fatal, message, payload)
4389
end
4490

91+
# Logs a message at the specified level
92+
# @param level [Symbol] The log level (:trace, :debug, :info, :warn, :error, :fatal)
93+
# @param message [String] The log message
94+
# @param payload [Hash] Additional attributes to include with the log
95+
# @return [LogEvent, nil] The created log event or nil if logging is disabled
4596
def log(level, message, payload)
4697
Sentry.capture_log(message, level: level, severity: LEVELS[level], **payload)
4798
end

0 commit comments

Comments
 (0)