Skip to content

Commit 13863bc

Browse files
committed
Rework logger init to work with enabled_logs config
1 parent bdf604d commit 13863bc

File tree

3 files changed

+43
-40
lines changed

3 files changed

+43
-40
lines changed

sentry-ruby/lib/sentry-ruby.rb

Lines changed: 36 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -95,27 +95,6 @@ def exception_locals_tp
9595
# @return [Metrics::Aggregator, nil]
9696
attr_reader :metrics_aggregator
9797

98-
# @!attribute [r] logger
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.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
117-
attr_reader :logger
118-
11998
##### Patch Registration #####
12099

121100
# @!visibility private
@@ -261,11 +240,6 @@ def init(&block)
261240
config = Configuration.new
262241
yield(config) if block_given?
263242

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/
267-
@logger = StructuredLogger.new(config) if config.enable_logs
268-
269243
config.detect_release
270244
apply_patches(config)
271245
config.validate
@@ -640,18 +614,45 @@ def continue_trace(env, **options)
640614
get_current_hub.continue_trace(env, **options)
641615
end
642616

643-
##### Helpers #####
644-
645-
# @!visibility private
617+
# Returns the structured logger instance that implements Sentry's SDK telemetry logs protocol.
618+
#
619+
# This logger is only available when logs are enabled in the configuration.
620+
#
621+
# @example Enable logs in configuration
622+
# Sentry.init do |config|
623+
# config.dsn = "YOUR_DSN"
624+
# config.enable_logs = true
625+
# end
626+
#
627+
# @example Basic usage
628+
# Sentry.logger.info("User logged in successfully", user_id: 123)
629+
# Sentry.logger.error("Failed to process payment",
630+
# transaction_id: "tx_123",
631+
# error_code: "PAYMENT_FAILED"
632+
# )
633+
#
634+
# @see https://develop.sentry.dev/sdk/telemetry/logs/ Sentry SDK Telemetry Logs Protocol
635+
#
636+
# @return [StructuredLogger, nil] The structured logger instance or nil if logs are disabled
646637
def logger
647-
warn <<~STR
648-
[sentry] `Sentry.logger` will no longer be used as internal SDK logger when `enable_logs` feature is turned on.
649-
Use Sentry.configuration.sdk_logger for SDK-specific logging needs."
650-
STR
651-
652-
configuration.sdk_logger
638+
@logger ||=
639+
if configuration.enable_logs
640+
# Initialize the public-facing Structured Logger if logs are enabled
641+
# This creates a StructuredLogger instance that implements Sentry's SDK telemetry logs protocol
642+
# @see https://develop.sentry.dev/sdk/telemetry/logs/
643+
StructuredLogger.new(configuration)
644+
else
645+
warn <<~STR
646+
[sentry] `Sentry.logger` will no longer be used as internal SDK logger when `enable_logs` feature is turned on.
647+
Use Sentry.configuration.sdk_logger for SDK-specific logging needs."
648+
STR
649+
650+
configuration.sdk_logger
651+
end
653652
end
654653

654+
##### Helpers #####
655+
655656
# @!visibility private
656657
def sys_command(command)
657658
result = `#{command} 2>&1` rescue nil

sentry-ruby/lib/sentry/configuration.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,7 @@ def initialize
468468
self.rack_env_whitelist = RACK_ENV_WHITELIST_DEFAULT
469469
self.traces_sampler = nil
470470
self.enable_tracing = nil
471-
self.enable_logs = true
471+
self.enable_logs = false
472472

473473
self.profiler_class = Sentry::Profiler
474474

sentry-ruby/spec/sentry/structured_logger_spec.rb

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@
33
require "spec_helper"
44

55
RSpec.describe Sentry::StructuredLogger do
6-
context "when log events are not enabled" do
6+
context "when enable_logs is set to false" do
77
before do
8-
perform_basic_setup
8+
perform_basic_setup do |config|
9+
config.enable_logs = false
10+
end
911
end
1012

11-
it "logger is not set up" do
12-
expect(Sentry.logger).to be_nil
13+
it "configures default SDK logger" do
14+
expect(Sentry.logger).to be(Sentry.configuration.sdk_logger)
1315
end
1416
end
1517

0 commit comments

Comments
 (0)