Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions sentry-rails/spec/sentry/rails/log_subscriber_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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")
Expand Down Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions sentry-ruby/lib/sentry-ruby.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
41 changes: 21 additions & 20 deletions sentry-ruby/lib/sentry/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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."
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Comment thread
solnic marked this conversation as resolved.
end
8 changes: 5 additions & 3 deletions sentry-ruby/lib/sentry/debug_structured_logger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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?

Expand Down
4 changes: 2 additions & 2 deletions spec/apps/rails-mini/app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading