Skip to content

Commit 5def2ff

Browse files
dingsdaxjeromeplclaude
authored
fix(logs): Support Logger block syntax in Sentry.logger (#3178)
* 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. * fix(logs): Support block syntax in DebugStructuredLogger DebugStructuredLogger overrides every level method and #log with a required message, so block syntax raised ArgumentError when it was configured as the logger class. Accept an optional message and resolve it from the block there too. Raise a clear ArgumentError when neither a message nor a block is given, instead of letting nil reach LogEvent and fail with NoMethodError. Use yield/block_given? in the plain #log methods; the level methods keep &block only to forward it (Ruby 2.7 has no anonymous block forwarding, and define_method cannot yield). Refs #2911 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * fix(logs): Let the block take precedence over the positional argument Ruby's Logger treats the positional argument of its level methods as the progname when a block is given, and logs the block's result as the message. Match that so callers like info("request") { details } log the details instead of the progname. Refs #3177 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Jérôme Parent-Lévesque <jerome.parent-levesque@potloc.com> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 0c76f63 commit 5def2ff

4 files changed

Lines changed: 72 additions & 15 deletions

File tree

‎sentry-ruby/lib/sentry/debug_structured_logger.rb‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,20 @@ def initialize(configuration)
2525

2626
# Override all log level methods to capture events
2727
%i[trace debug info warn error fatal].each do |level|
28-
define_method(level) do |message, parameters = [], **attributes|
28+
define_method(level) do |message = nil, parameters = [], **attributes, &block|
29+
message = block.call if block
30+
raise ArgumentError, "message or block is required" if message.nil?
31+
2932
log_event = capture_log_event(level, message, parameters, **attributes)
3033
backend.public_send(level, message, parameters, **attributes)
3134
log_event
3235
end
3336
end
3437

35-
def log(level, message, parameters:, **attributes)
38+
def log(level, message = nil, parameters:, **attributes)
39+
message = yield if block_given?
40+
raise ArgumentError, "message or block is required" if message.nil?
41+
3642
log_event = capture_log_event(level, message, parameters, **attributes)
3743
backend.log(level, message, parameters: parameters, **attributes)
3844
log_event

‎sentry-ruby/lib/sentry/structured_logger.rb‎

Lines changed: 16 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,10 @@ 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)
130+
message = yield if block_given?
131+
raise ArgumentError, "message or block is required" if message.nil?
132+
130133
case parameters
131134
when Array then
132135
Sentry.capture_log(message, level: level, severity: LEVELS[level], parameters: parameters, **attributes)

‎sentry-ruby/spec/sentry/debug_structured_logger_spec.rb‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,24 @@
5252
expect(log_event["parameters"]).to eq(["param1", "param2"])
5353
expect(log_event["attributes"]["extra_attr"]).to eq("extra")
5454
end
55+
56+
it "supports block syntax" do
57+
debug_logger.public_send(level) { "Test #{level} block message" }
58+
59+
log_event = debug_logger.logged_events.last
60+
expect(log_event["message"]).to eq("Test #{level} block message")
61+
end
62+
63+
it "uses the block over the positional argument" do
64+
debug_logger.public_send(level, "progname") { "from block" }
65+
66+
expect(debug_logger.logged_events.last["message"]).to eq("from block")
67+
end
68+
69+
it "raises ArgumentError when neither message nor block is given" do
70+
expect { debug_logger.public_send(level) }
71+
.to raise_error(ArgumentError, "message or block is required")
72+
end
5573
end
5674
end
5775
end
@@ -68,6 +86,14 @@
6886
expect(log_event["message"]).to eq("Test log message")
6987
expect(log_event["attributes"]["custom_attr"]).to eq("custom_value")
7088
end
89+
90+
it "supports block syntax" do
91+
debug_logger.log(:info, parameters: []) { "Test log block message" }
92+
93+
log_event = debug_logger.logged_events.last
94+
expect(log_event["level"]).to eq("info")
95+
expect(log_event["message"]).to eq("Test log block message")
96+
end
7197
end
7298

7399
describe "#logged_events" do

‎sentry-ruby/spec/sentry/structured_logger_spec.rb‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,28 @@
122122
expect(log_event[:attributes]["sentry.message.parameter.day"]).to eql({ value: "Monday", type: "string" })
123123
end
124124

125+
it "logs with block syntax for compatibility with ruby Logger" do
126+
Sentry.logger.public_send(level) { "Hello world" }
127+
128+
expect(sentry_logs).to_not be_empty
129+
130+
log_event = sentry_logs.last
131+
132+
expect(log_event[:level]).to eql(level)
133+
expect(log_event[:body]).to eql("Hello world")
134+
end
135+
136+
it "uses the block over the positional argument, like ruby Logger" do
137+
Sentry.logger.public_send(level, "progname") { "from block" }
138+
139+
expect(sentry_logs.last[:body]).to eql("from block")
140+
end
141+
142+
it "raises ArgumentError when neither message nor block is given" do
143+
expect { Sentry.logger.public_send(level) }
144+
.to raise_error(ArgumentError, "message or block is required")
145+
end
146+
125147
context "handling of malformed strings" do
126148
let(:malformed_string_default) do
127149
Sentry::Utils::EncodingHelper::MALFORMED_STRING

0 commit comments

Comments
 (0)