From 29310a4f91517fcbae0dd81d45b0d90a7daba8d1 Mon Sep 17 00:00:00 2001 From: Peter Solnica Date: Thu, 4 Sep 2025 07:13:10 +0000 Subject: [PATCH] Introduce structured_logging config namespace --- .../spec/sentry/rails/log_subscriber_spec.rb | 8 ++-- sentry-ruby/lib/sentry-ruby.rb | 3 +- sentry-ruby/lib/sentry/configuration.rb | 41 ++++++++++--------- .../lib/sentry/debug_structured_logger.rb | 8 ++-- spec/apps/rails-mini/app.rb | 4 +- spec/spec_helper.rb | 4 +- 6 files changed, 35 insertions(+), 33 deletions(-) diff --git a/sentry-rails/spec/sentry/rails/log_subscriber_spec.rb b/sentry-rails/spec/sentry/rails/log_subscriber_spec.rb index 10135859c..94e095ec7 100644 --- a/sentry-rails/spec/sentry/rails/log_subscriber_spec.rb +++ b/sentry-rails/spec/sentry/rails/log_subscriber_spec.rb @@ -44,7 +44,7 @@ def error_test_event(event) before do make_basic_app do |config| config.enable_logs = true - config.structured_logger_class = Sentry::DebugStructuredLogger + config.structured_logging.logger_class = Sentry::DebugStructuredLogger end end @@ -57,7 +57,8 @@ def error_test_event(event) logged_events = Sentry.logger.logged_events expect(logged_events).not_to be_empty - log_event = logged_events.first + log_event = logged_events.find { |event| event["message"] == "Test event occurred" } + expect(log_event).not_to be_nil expect(log_event["level"]).to eq("info") expect(log_event["message"]).to eq("Test event occurred") expect(log_event["attributes"]["test_data"]).to eq("sample_data") @@ -241,8 +242,7 @@ def filtering_event(event) before do make_basic_app do |config, app| config.enable_logs = true - - config.structured_logger_class = Sentry::DebugStructuredLogger + config.structured_logging.logger_class = Sentry::DebugStructuredLogger config.send_default_pii = true end end diff --git a/sentry-ruby/lib/sentry-ruby.rb b/sentry-ruby/lib/sentry-ruby.rb index 728675b98..48b44e9f2 100644 --- a/sentry-ruby/lib/sentry-ruby.rb +++ b/sentry-ruby/lib/sentry-ruby.rb @@ -642,8 +642,7 @@ def logger # Initialize the public-facing Structured Logger if logs are enabled # Use configured structured logger class or default to StructuredLogger # @see https://develop.sentry.dev/sdk/telemetry/logs/ - logger_class = configuration.structured_logger_class || StructuredLogger - logger_class.new(configuration) + configuration.structured_logging.logger_class.new(configuration) else warn <<~STR [sentry] `Sentry.logger` will no longer be used as internal SDK logger when `enable_logs` feature is turned on. diff --git a/sentry-ruby/lib/sentry/configuration.rb b/sentry-ruby/lib/sentry/configuration.rb index 7e8ef503b..cfcf29d89 100644 --- a/sentry-ruby/lib/sentry/configuration.rb +++ b/sentry-ruby/lib/sentry/configuration.rb @@ -13,6 +13,7 @@ require "sentry/linecache" require "sentry/interfaces/stacktrace_builder" require "sentry/logger" +require "sentry/structured_logger" require "sentry/log_event_buffer" module Sentry @@ -201,18 +202,6 @@ def capture_exception_frame_locals=(value) # @return [String, nil] attr_accessor :sdk_debug_transport_log_file - # File path for DebugStructuredLogger to log events to. If not set, defaults to a temporary file. - # This is useful for debugging and testing structured logging. - # @return [String, nil] - attr_accessor :sdk_debug_structured_logger_log_file - - # The class to use as a structured logger. - # If this option is not set, it will return `nil`, and Sentry will use - # `Sentry::StructuredLogger` by default when logs are enabled. - # - # @return [Class, nil] - attr_reader :structured_logger_class - # @deprecated Use {#sdk_logger=} instead. def logger=(logger) warn "[sentry] `config.logger=` is deprecated. Please use `config.sdk_logger=` instead." @@ -306,6 +295,10 @@ def logger # @return [Boolean] attr_accessor :enable_logs + # Structured logging configuration. + # @return [StructuredLoggingConfiguration] + attr_reader :structured_logging + # Easier way to use performance tracing # If set to true, will set traces_sample_rate to 1.0 # @deprecated It will be removed in the next major release. @@ -502,6 +495,7 @@ def initialize @transport = Transport::Configuration.new @cron = Cron::Configuration.new @metrics = Metrics::Configuration.new + @structured_logging = StructuredLoggingConfiguration.new @gem_specs = Hash[Gem::Specification.map { |spec| [spec.name, spec.version.to_s] }] if Gem::Specification.respond_to?(:map) run_post_initialization_callbacks @@ -624,14 +618,6 @@ def profiler_class=(profiler_class) @profiler_class = profiler_class end - def structured_logger_class=(klass) - unless klass.is_a?(Class) - raise Sentry::Error.new("config.structured_logger_class must be a class. got: #{klass.class}") - end - - @structured_logger_class = klass - end - def sending_allowed? spotlight || sending_to_dsn_allowed? end @@ -809,4 +795,19 @@ def processor_count available_processor_count || Concurrent.processor_count end end + + class StructuredLoggingConfiguration + # File path for DebugStructuredLogger to log events to + # @return [String, Pathname, nil] + attr_accessor :file_path + + # The class to use as a structured logger. + # @return [Class] + attr_accessor :logger_class + + def initialize + @file_path = nil + @logger_class = Sentry::StructuredLogger + end + end end diff --git a/sentry-ruby/lib/sentry/debug_structured_logger.rb b/sentry-ruby/lib/sentry/debug_structured_logger.rb index 129822c4b..2e5835a4b 100644 --- a/sentry-ruby/lib/sentry/debug_structured_logger.rb +++ b/sentry-ruby/lib/sentry/debug_structured_logger.rb @@ -16,7 +16,9 @@ class DebugStructuredLogger < SimpleDelegator attr_reader :log_file, :backend def initialize(configuration) - @log_file = initialize_log_file(configuration) + @log_file = initialize_log_file( + configuration.structured_logging.file_path || DEFAULT_LOG_FILE_PATH + ) @backend = initialize_backend(configuration) super(@backend) @@ -74,8 +76,8 @@ def initialize_backend(configuration) end end - def initialize_log_file(configuration) - log_file = Pathname(configuration.sdk_debug_structured_logger_log_file || DEFAULT_LOG_FILE_PATH) + def initialize_log_file(log_file_path) + log_file = Pathname(log_file_path) FileUtils.mkdir_p(log_file.dirname) unless log_file.dirname.exist? diff --git a/spec/apps/rails-mini/app.rb b/spec/apps/rails-mini/app.rb index 184d9b4af..8c1f8db33 100644 --- a/spec/apps/rails-mini/app.rb +++ b/spec/apps/rails-mini/app.rb @@ -44,8 +44,8 @@ def debug_log_path config.background_worker_threads = 0 config.enable_logs = true - config.structured_logger_class = Sentry::DebugStructuredLogger - config.sdk_debug_structured_logger_log_file = debug_log_path.join("sentry_e2e_tests.log") + config.structured_logging.logger_class = Sentry::DebugStructuredLogger + config.structured_logging.file_path = debug_log_path.join("sentry_e2e_tests.log") config.rails.structured_logging.enabled = true diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 6329d91c7..ea53b64b9 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -37,9 +37,9 @@ config.before(:suite) do Test::Helper.perform_basic_setup do |config| config.transport.transport_class = Sentry::DebugTransport - config.structured_logger_class = Sentry::DebugStructuredLogger - config.sdk_debug_structured_logger_log_file = Test::Helper.debug_log_path.join("sentry_e2e_tests.log") config.enable_logs = true + config.structured_logging.logger_class = Sentry::DebugStructuredLogger + config.structured_logging.file_path = Test::Helper.debug_log_path.join("sentry_e2e_tests.log") end Test::Helper.clear_logged_events