Skip to content

Commit c51d8ff

Browse files
authored
Introduce structured_logging config namespace (#2692)
1 parent ec50db4 commit c51d8ff

6 files changed

Lines changed: 35 additions & 33 deletions

File tree

sentry-rails/spec/sentry/rails/log_subscriber_spec.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def error_test_event(event)
4444
before do
4545
make_basic_app do |config|
4646
config.enable_logs = true
47-
config.structured_logger_class = Sentry::DebugStructuredLogger
47+
config.structured_logging.logger_class = Sentry::DebugStructuredLogger
4848
end
4949
end
5050

@@ -57,7 +57,8 @@ def error_test_event(event)
5757
logged_events = Sentry.logger.logged_events
5858
expect(logged_events).not_to be_empty
5959

60-
log_event = logged_events.first
60+
log_event = logged_events.find { |event| event["message"] == "Test event occurred" }
61+
expect(log_event).not_to be_nil
6162
expect(log_event["level"]).to eq("info")
6263
expect(log_event["message"]).to eq("Test event occurred")
6364
expect(log_event["attributes"]["test_data"]).to eq("sample_data")
@@ -241,8 +242,7 @@ def filtering_event(event)
241242
before do
242243
make_basic_app do |config, app|
243244
config.enable_logs = true
244-
245-
config.structured_logger_class = Sentry::DebugStructuredLogger
245+
config.structured_logging.logger_class = Sentry::DebugStructuredLogger
246246
config.send_default_pii = true
247247
end
248248
end

sentry-ruby/lib/sentry-ruby.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -642,8 +642,7 @@ def logger
642642
# Initialize the public-facing Structured Logger if logs are enabled
643643
# Use configured structured logger class or default to StructuredLogger
644644
# @see https://develop.sentry.dev/sdk/telemetry/logs/
645-
logger_class = configuration.structured_logger_class || StructuredLogger
646-
logger_class.new(configuration)
645+
configuration.structured_logging.logger_class.new(configuration)
647646
else
648647
warn <<~STR
649648
[sentry] `Sentry.logger` will no longer be used as internal SDK logger when `enable_logs` feature is turned on.

sentry-ruby/lib/sentry/configuration.rb

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
require "sentry/linecache"
1414
require "sentry/interfaces/stacktrace_builder"
1515
require "sentry/logger"
16+
require "sentry/structured_logger"
1617
require "sentry/log_event_buffer"
1718

1819
module Sentry
@@ -201,18 +202,6 @@ def capture_exception_frame_locals=(value)
201202
# @return [String, nil]
202203
attr_accessor :sdk_debug_transport_log_file
203204

204-
# File path for DebugStructuredLogger to log events to. If not set, defaults to a temporary file.
205-
# This is useful for debugging and testing structured logging.
206-
# @return [String, nil]
207-
attr_accessor :sdk_debug_structured_logger_log_file
208-
209-
# The class to use as a structured logger.
210-
# If this option is not set, it will return `nil`, and Sentry will use
211-
# `Sentry::StructuredLogger` by default when logs are enabled.
212-
#
213-
# @return [Class, nil]
214-
attr_reader :structured_logger_class
215-
216205
# @deprecated Use {#sdk_logger=} instead.
217206
def logger=(logger)
218207
warn "[sentry] `config.logger=` is deprecated. Please use `config.sdk_logger=` instead."
@@ -306,6 +295,10 @@ def logger
306295
# @return [Boolean]
307296
attr_accessor :enable_logs
308297

298+
# Structured logging configuration.
299+
# @return [StructuredLoggingConfiguration]
300+
attr_reader :structured_logging
301+
309302
# Easier way to use performance tracing
310303
# If set to true, will set traces_sample_rate to 1.0
311304
# @deprecated It will be removed in the next major release.
@@ -502,6 +495,7 @@ def initialize
502495
@transport = Transport::Configuration.new
503496
@cron = Cron::Configuration.new
504497
@metrics = Metrics::Configuration.new
498+
@structured_logging = StructuredLoggingConfiguration.new
505499
@gem_specs = Hash[Gem::Specification.map { |spec| [spec.name, spec.version.to_s] }] if Gem::Specification.respond_to?(:map)
506500

507501
run_post_initialization_callbacks
@@ -624,14 +618,6 @@ def profiler_class=(profiler_class)
624618
@profiler_class = profiler_class
625619
end
626620

627-
def structured_logger_class=(klass)
628-
unless klass.is_a?(Class)
629-
raise Sentry::Error.new("config.structured_logger_class must be a class. got: #{klass.class}")
630-
end
631-
632-
@structured_logger_class = klass
633-
end
634-
635621
def sending_allowed?
636622
spotlight || sending_to_dsn_allowed?
637623
end
@@ -809,4 +795,19 @@ def processor_count
809795
available_processor_count || Concurrent.processor_count
810796
end
811797
end
798+
799+
class StructuredLoggingConfiguration
800+
# File path for DebugStructuredLogger to log events to
801+
# @return [String, Pathname, nil]
802+
attr_accessor :file_path
803+
804+
# The class to use as a structured logger.
805+
# @return [Class]
806+
attr_accessor :logger_class
807+
808+
def initialize
809+
@file_path = nil
810+
@logger_class = Sentry::StructuredLogger
811+
end
812+
end
812813
end

sentry-ruby/lib/sentry/debug_structured_logger.rb

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ class DebugStructuredLogger < SimpleDelegator
1616
attr_reader :log_file, :backend
1717

1818
def initialize(configuration)
19-
@log_file = initialize_log_file(configuration)
19+
@log_file = initialize_log_file(
20+
configuration.structured_logging.file_path || DEFAULT_LOG_FILE_PATH
21+
)
2022
@backend = initialize_backend(configuration)
2123

2224
super(@backend)
@@ -74,8 +76,8 @@ def initialize_backend(configuration)
7476
end
7577
end
7678

77-
def initialize_log_file(configuration)
78-
log_file = Pathname(configuration.sdk_debug_structured_logger_log_file || DEFAULT_LOG_FILE_PATH)
79+
def initialize_log_file(log_file_path)
80+
log_file = Pathname(log_file_path)
7981

8082
FileUtils.mkdir_p(log_file.dirname) unless log_file.dirname.exist?
8183

spec/apps/rails-mini/app.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ def debug_log_path
4444
config.background_worker_threads = 0
4545

4646
config.enable_logs = true
47-
config.structured_logger_class = Sentry::DebugStructuredLogger
48-
config.sdk_debug_structured_logger_log_file = debug_log_path.join("sentry_e2e_tests.log")
47+
config.structured_logging.logger_class = Sentry::DebugStructuredLogger
48+
config.structured_logging.file_path = debug_log_path.join("sentry_e2e_tests.log")
4949

5050
config.rails.structured_logging.enabled = true
5151

spec/spec_helper.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@
3737
config.before(:suite) do
3838
Test::Helper.perform_basic_setup do |config|
3939
config.transport.transport_class = Sentry::DebugTransport
40-
config.structured_logger_class = Sentry::DebugStructuredLogger
41-
config.sdk_debug_structured_logger_log_file = Test::Helper.debug_log_path.join("sentry_e2e_tests.log")
4240
config.enable_logs = true
41+
config.structured_logging.logger_class = Sentry::DebugStructuredLogger
42+
config.structured_logging.file_path = Test::Helper.debug_log_path.join("sentry_e2e_tests.log")
4343
end
4444

4545
Test::Helper.clear_logged_events

0 commit comments

Comments
 (0)