@@ -23,78 +23,98 @@ class StructuredLogger
2323 # Severity number mapping for log levels according to the Sentry Logs Protocol
2424 # @see https://develop.sentry.dev/sdk/telemetry/logs/#log-severity-number
2525 LEVELS = {
26- " trace" => 1 ,
27- " debug" => 5 ,
28- " info" => 9 ,
29- " warn" => 13 ,
30- " error" => 17 ,
31- " fatal" => 21
26+ trace : 1 ,
27+ debug : 5 ,
28+ info : 9 ,
29+ warn : 13 ,
30+ error : 17 ,
31+ fatal : 21
3232 } . freeze
3333
3434 # @return [Configuration] The Sentry configuration
35+ # @!visibility private
3536 attr_reader :config
3637
3738 # Initializes a new StructuredLogger instance
39+ #
3840 # @param config [Configuration] The Sentry configuration
3941 def initialize ( config )
4042 @config = config
4143 end
4244
4345 # Logs a message at TRACE level
46+ #
4447 # @param message [String] The log message
45- # @param payload [Hash] Additional attributes to include with the log
48+ # @param parameters [Array] Array of values to replace template parameters in the message
49+ # @param attributes [Hash] Additional attributes to include with the log
4650 # @return [LogEvent, nil] The created log event or nil if logging is disabled
47- def trace ( message , payload = { } )
48- log ( :trace , message , payload )
51+ def trace ( message , parameters = [ ] , ** attributes )
52+ log ( __method__ , message , parameters : parameters , ** attributes )
4953 end
5054
5155 # Logs a message at DEBUG level
56+ #
5257 # @param message [String] The log message
53- # @param payload [Hash] Additional attributes to include with the log
58+ # @param parameters [Array] Array of values to replace template parameters in the message
59+ # @param attributes [Hash] Additional attributes to include with the log
5460 # @return [LogEvent, nil] The created log event or nil if logging is disabled
55- def debug ( message , payload = { } )
56- log ( :debug , message , payload )
61+ def debug ( message , parameters = [ ] , ** attributes )
62+ log ( __method__ , message , parameters : parameters , ** attributes )
5763 end
5864
5965 # Logs a message at INFO level
66+ #
6067 # @param message [String] The log message
61- # @param payload [Hash] Additional attributes to include with the log
68+ # @param parameters [Array] Array of values to replace template parameters in the message
69+ # @param attributes [Hash] Additional attributes to include with the log
6270 # @return [LogEvent, nil] The created log event or nil if logging is disabled
63- def info ( message , payload = { } )
64- log ( :info , message , payload )
71+ def info ( message , parameters = [ ] , ** attributes )
72+ log ( __method__ , message , parameters : parameters , ** attributes )
6573 end
6674
6775 # Logs a message at WARN level
76+ #
6877 # @param message [String] The log message
69- # @param payload [Hash] Additional attributes to include with the log
78+ # @param parameters [Array] Array of values to replace template parameters in the message
79+ # @param attributes [Hash] Additional attributes to include with the log
7080 # @return [LogEvent, nil] The created log event or nil if logging is disabled
71- def warn ( message , payload = { } )
72- log ( :warn , message , payload )
81+ def warn ( message , parameters = [ ] , ** attributes )
82+ log ( __method__ , message , parameters : parameters , ** attributes )
7383 end
7484
7585 # Logs a message at ERROR level
86+ #
7687 # @param message [String] The log message
77- # @param payload [Hash] Additional attributes to include with the log
88+ # @param parameters [Array] Array of values to replace template parameters in the message
89+ # @param attributes [Hash] Additional attributes to include with the log
7890 # @return [LogEvent, nil] The created log event or nil if logging is disabled
79- def error ( message , payload = { } )
80- log ( :error , message , payload )
91+ def error ( message , parameters = [ ] , ** attributes )
92+ log ( __method__ , message , parameters : parameters , ** attributes )
8193 end
8294
8395 # Logs a message at FATAL level
96+ #
8497 # @param message [String] The log message
85- # @param payload [Hash] Additional attributes to include with the log
98+ # @param parameters [Array] Array of values to replace template parameters in the message
99+ # @param attributes [Hash] Additional attributes to include with the log
86100 # @return [LogEvent, nil] The created log event or nil if logging is disabled
87- def fatal ( message , payload = { } )
88- log ( :fatal , message , payload )
101+ def fatal ( message , parameters = [ ] , ** attributes )
102+ log ( __method__ , message , parameters : parameters , ** attributes )
89103 end
90104
91105 # Logs a message at the specified level
106+ #
92107 # @param level [Symbol] The log level (:trace, :debug, :info, :warn, :error, :fatal)
93108 # @param message [String] The log message
94- # @param payload [Hash] Additional attributes to include with the log
109+ # @param parameters [Array] Array of values to replace template parameters in the message
110+ # @param attributes [Hash] Additional attributes to include with the log
95111 # @return [LogEvent, nil] The created log event or nil if logging is disabled
96- def log ( level , message , payload )
97- Sentry . capture_log ( message , level : level , severity : LEVELS [ level ] , **payload )
112+ def log ( level , message , parameters :, **attributes )
113+ if parameters . is_a? ( Hash ) && attributes . empty?
114+ Sentry . capture_log ( message , level : level , severity : LEVELS [ level ] , parameters : [ ] , **parameters )
115+ else
116+ Sentry . capture_log ( message , level : level , severity : LEVELS [ level ] , parameters : parameters , **attributes )
117+ end
98118 end
99119 end
100120end
0 commit comments