Skip to content

Commit b1ccc4b

Browse files
committed
fix: Add block syntax support to StructuredLogger for compatibility
Ruby's Logger supports passing the message as a block. For Sentry's structured logger to be used interchangeably with Ruby's logger, it should also support this syntax.
1 parent aaa5a9a commit b1ccc4b

2 files changed

Lines changed: 25 additions & 13 deletions

File tree

sentry-ruby/lib/sentry/structured_logger.rb

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ def initialize(config)
5959
# @param attributes [Hash] Additional attributes to include with the log
6060
#
6161
# @return [LogEvent, nil] The created log event or nil if logging is disabled
62-
def trace(message, parameters = [], **attributes)
63-
log(__method__, message, parameters: parameters, **attributes)
62+
def trace(message = nil, parameters = [], **attributes, &block)
63+
log(__method__, message, parameters: parameters, **attributes, &block)
6464
end
6565

6666
# Logs a message at DEBUG level
@@ -70,8 +70,8 @@ def trace(message, parameters = [], **attributes)
7070
# @param attributes [Hash] Additional attributes to include with the log
7171
#
7272
# @return [LogEvent, nil] The created log event or nil if logging is disabled
73-
def debug(message, parameters = [], **attributes)
74-
log(__method__, message, parameters: parameters, **attributes)
73+
def debug(message = nil, parameters = [], **attributes, &block)
74+
log(__method__, message, parameters: parameters, **attributes, &block)
7575
end
7676

7777
# Logs a message at INFO level
@@ -81,8 +81,8 @@ def debug(message, parameters = [], **attributes)
8181
# @param attributes [Hash] Additional attributes to include with the log
8282
#
8383
# @return [LogEvent, nil] The created log event or nil if logging is disabled
84-
def info(message, parameters = [], **attributes)
85-
log(__method__, message, parameters: parameters, **attributes)
84+
def info(message = nil, parameters = [], **attributes, &block)
85+
log(__method__, message, parameters: parameters, **attributes, &block)
8686
end
8787

8888
# Logs a message at WARN level
@@ -92,8 +92,8 @@ def info(message, parameters = [], **attributes)
9292
# @param attributes [Hash] Additional attributes to include with the log
9393
#
9494
# @return [LogEvent, nil] The created log event or nil if logging is disabled
95-
def warn(message, parameters = [], **attributes)
96-
log(__method__, message, parameters: parameters, **attributes)
95+
def warn(message = nil, parameters = [], **attributes, &block)
96+
log(__method__, message, parameters: parameters, **attributes, &block)
9797
end
9898

9999
# Logs a message at ERROR level
@@ -103,8 +103,8 @@ def warn(message, parameters = [], **attributes)
103103
# @param attributes [Hash] Additional attributes to include with the log
104104
#
105105
# @return [LogEvent, nil] The created log event or nil if logging is disabled
106-
def error(message, parameters = [], **attributes)
107-
log(__method__, message, parameters: parameters, **attributes)
106+
def error(message = nil, parameters = [], **attributes, &block)
107+
log(__method__, message, parameters: parameters, **attributes, &block)
108108
end
109109

110110
# Logs a message at FATAL level
@@ -114,8 +114,8 @@ def error(message, parameters = [], **attributes)
114114
# @param attributes [Hash] Additional attributes to include with the log
115115
#
116116
# @return [LogEvent, nil] The created log event or nil if logging is disabled
117-
def fatal(message, parameters = [], **attributes)
118-
log(__method__, message, parameters: parameters, **attributes)
117+
def fatal(message = nil, parameters = [], **attributes, &block)
118+
log(__method__, message, parameters: parameters, **attributes, &block)
119119
end
120120

121121
# Logs a message at the specified level
@@ -126,7 +126,8 @@ def fatal(message, parameters = [], **attributes)
126126
# @param attributes [Hash] Additional attributes to include with the log
127127
#
128128
# @return [LogEvent, nil] The created log event or nil if logging is disabled
129-
def log(level, message, parameters:, **attributes)
129+
def log(level, message = nil, parameters:, **attributes, &block)
130+
message = yield if message.nil? && block_given?
130131
case parameters
131132
when Array then
132133
Sentry.capture_log(message, level: level, severity: LEVELS[level], parameters: parameters, **attributes)

sentry-ruby/spec/sentry/structured_logger_spec.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,17 @@
135135
expect(log_event[:attributes]["sentry.message.parameter.day"]).to eql({ value: "Monday", type: "string" })
136136
end
137137

138+
it "logs with block syntax for compatibility with ruby Logger" do
139+
Sentry.logger.public_send(level) { "Hello world" }
140+
141+
expect(sentry_logs).to_not be_empty
142+
143+
log_event = sentry_logs.last
144+
145+
expect(log_event[:level]).to eql(level)
146+
expect(log_event[:body]).to eql("Hello world")
147+
end
148+
138149
context "handling of malformed strings" do
139150
let(:malformed_string_default) do
140151
Sentry::Utils::EncodingHelper::MALFORMED_STRING

0 commit comments

Comments
 (0)