rails_semantic_logger 5.1.0 → 5.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -10,9 +10,50 @@ module ActionDispatch
10
10
  Rails.application.deprecators.silence do
11
11
  return if !log_rescued_responses?(request) && wrapper.rescue_response?
12
12
 
13
- level = request.get_header("action_dispatch.debug_exception_log_level")
14
- ActionController::Base.logger.log(level, wrapper.exception)
13
+ level = request.get_header("action_dispatch.debug_exception_log_level")
14
+ logger = ActionController::Base.logger
15
+ call_site = semantic_logger_call_site(wrapper, logger, level)
16
+ logger.log(level, **semantic_logger_arguments(wrapper), call_site: call_site)
15
17
  end
16
18
  end
19
+
20
+ # Returns the entry's call site, or nil when it records none: where the application raised the
21
+ # exception, the first frame of its backtrace that the request's backtrace cleaner keeps before
22
+ # Rails took over (see RailsSemanticLogger::ApplicationFrame.in_backtrace), or else false, as for a
23
+ # routing error raised in middleware, rather than the frame that logs the entry.
24
+ def semantic_logger_call_site(wrapper, logger, level)
25
+ cleaner = wrapper.backtrace_cleaner || ::Rails.backtrace_cleaner
26
+ RailsSemanticLogger.exception_call_site(wrapper.exception, cleaner, logger, level)
27
+ end
28
+
29
+ # Returns the arguments of the entry that logs the exception.
30
+ #
31
+ # When Rails' own log shows no backtrace for the exception, the entry has only its class and
32
+ # message, and the exceptions that caused it, which Rails lists as "Caused by", with
33
+ # Semantic Logger's `log_exception: :partial`, after the response's status, such as
34
+ # "404 Not Found". The causes' backtraces, which Rails 8.0 and later also log, are left out too.
35
+ # Otherwise a request for a missing route, such as /apple-touch-icon.png, would log dozens of
36
+ # middleware frames that say nothing about the application.
37
+ #
38
+ # Rails decides that in ExceptionWrapper#exception_trace, which is empty only for the exceptions
39
+ # in ExceptionWrapper.silent_exceptions (ActionController::RoutingError and
40
+ # ActionDispatch::Http::MimeNegotiation::InvalidType, unless the application adds others), and
41
+ # only when none of their frames is the application's once the request's backtrace cleaner has
42
+ # cleaned them. Every other exception is logged with its backtrace, even with no application
43
+ # frames, as in Rails' own log. The exception itself is left unchanged, since Rails renders it,
44
+ # and error services hooked into Rails report it.
45
+ #
46
+ # ExceptionWrapper only reads backtrace_locations, so it finds no frames at all in a backtrace
47
+ # set from Strings, such as with `raise error, message, backtrace`. That exception keeps its
48
+ # backtrace.
49
+ def semantic_logger_arguments(wrapper)
50
+ exception = wrapper.exception
51
+ # Rails wraps a SyntaxError in a proxy, which is not an Exception.
52
+ exception = exception.__getobj__ if exception.is_a?(ActiveSupport::SyntaxErrorProxy)
53
+ return {exception: exception} unless exception.backtrace_locations && wrapper.exception_trace.empty?
54
+
55
+ status = wrapper.status_code
56
+ {message: "#{status} #{::Rack::Utils::HTTP_STATUS_CODES[status]}", exception: exception, log_exception: :partial}
57
+ end
17
58
  end
18
59
  end
@@ -8,7 +8,11 @@ module ActionView
8
8
 
9
9
  undef_method :log_error
10
10
  def log_error(exception)
11
- ActionView::Base.logger.fatal(exception)
11
+ logger = ActionView::Base.logger
12
+ logger.fatal(
13
+ exception: exception,
14
+ call_site: RailsSemanticLogger.exception_call_site(exception, ::Rails.backtrace_cleaner, logger, :fatal)
15
+ )
12
16
  end
13
17
  end
14
18
  end
@@ -122,6 +122,37 @@ module RailsSemanticLogger
122
122
  # * Do not replace the SolidQueue logger / log subscriber.
123
123
  #
124
124
  # config.rails_semantic_logger.replace_solid_queue_logger = false
125
+ #
126
+ # * Do not redact the application's filter parameters from every log entry.
127
+ #
128
+ # Rails filters `config.filter_parameters` only from the request parameters that it logs. By
129
+ # default, Rails Semantic Logger also has Semantic Logger replace the value of every key that
130
+ # they match with "[FILTERED]" in every log entry: in its payload, named tags, and context, at
131
+ # any depth, before any appender writes it. Keys are matched by name, both as written and in
132
+ # snake_case, so `X-Api-Key` is also matched as `x_api_key`. The filters are read once the
133
+ # application has initialized, so those added in `config/initializers/*`, such as
134
+ # `config/initializers/filter_parameter_logging.rb`, apply. See `SemanticLogger.sensitive_keys=`.
135
+ #
136
+ # Note:
137
+ # * The filters match any key that contains them, so Rails' default filters (:passw, :email,
138
+ # :secret, :token, :_key, :crypt, :salt, :certificate, :otp, :ssn, :cvv, :cvc) also redact
139
+ # keys such as `max_tokens`, `token_count`, `cache_key`, and `api_key_id`, every `email`, and
140
+ # the `concurrency_key` of Solid Queue's entries.
141
+ # * Rails' default filters do not match `Authorization` or `Cookie`; add them to
142
+ # `config.filter_parameters` to redact those, e.g. `config.filter_parameters += [:authorization]`.
143
+ # * Active Job `arguments` and Action Mailer `args` are logged as JSON text, which is not
144
+ # redacted. For a job that takes a secret, leave its arguments out of the log with
145
+ # `self.log_arguments = false`.
146
+ # * Proc filters are not applied, since they change values rather than match keys.
147
+ # * A dotted filter, such as "credit_card.code", only matches a key named that, not a `code` key
148
+ # nested in `credit_card`, including the "model.attribute" filters that Rails adds for Active
149
+ # Record encrypted attributes.
150
+ # * An application that assigns its own `SemanticLogger.sensitive_keys` keeps them. Assigning
151
+ # nil does not turn redaction off, since it cannot be told apart from never assigning it: use
152
+ # this option.
153
+ # * Entries logged before the application has initialized may not be redacted.
154
+ #
155
+ # config.rails_semantic_logger.redact_filter_parameters = false
125
156
  class Options
126
157
  # Settings consumed while the logger itself is built, during Rails'
127
158
  # `:initialize_logger` initializer. Changing them after that (e.g. from
@@ -131,7 +162,9 @@ module RailsSemanticLogger
131
162
  # Settings consumed as Rails finishes initializing (`config.after_initialize`),
132
163
  # which runs *after* `config/initializers/*`. They may still be set there, but
133
164
  # changing them after the application has booted has no effect.
134
- POST_INIT_SETTINGS = %i[started processing rendered quiet_assets action_message_format].freeze
165
+ POST_INIT_SETTINGS = %i[
166
+ started processing rendered quiet_assets action_message_format redact_filter_parameters
167
+ ].freeze
135
168
 
136
169
  attr_reader(*LOGGER_INIT_SETTINGS, *POST_INIT_SETTINGS)
137
170
 
@@ -167,6 +200,7 @@ module RailsSemanticLogger
167
200
  @action_message_format = nil
168
201
  @replace_sidekiq_logger = true
169
202
  @replace_solid_queue_logger = true
203
+ @redact_filter_parameters = true
170
204
  end
171
205
 
172
206
  # Declare the appenders for Rails Semantic Logger to create, replacing the
@@ -256,14 +290,56 @@ module RailsSemanticLogger
256
290
  @add_file_appender = value
257
291
  end
258
292
 
293
+ # Emit the deprecation warnings recorded by the deprecated appender option
294
+ # setters, and warn immediately from any later call. Called by the engine once
295
+ # Rails has applied the application's deprecation configuration.
296
+ #
297
+ # The deprecated options are documented to be set in `config/application.rb` or
298
+ # `config/environments/*.rb`, which Rails reads in `load_environment_config`,
299
+ # long before `active_support.deprecation_behavior` applies
300
+ # `config.active_support.report_deprecations`, `config.active_support.deprecation`,
301
+ # and any per-deprecator settings. Warning from the setter itself would therefore
302
+ # land in a window where nothing the application configures can reach it. The
303
+ # setters record instead, and this flushes once the settings are in place.
304
+ #
305
+ # Each option is reported once, attributed to the first place it was set, so an
306
+ # application setting several deprecated options gets one warning per option
307
+ # rather than one per assignment.
308
+ def flush_deprecations!
309
+ @deprecations_flushed = true
310
+ pending = pending_deprecations
311
+ @pending_deprecations = {}
312
+ pending.each_value { |message, callstack| RailsSemanticLogger.deprecator.warn(message, callstack) }
313
+ nil
314
+ end
315
+
259
316
  private
260
317
 
261
318
  def deprecate_appender_option(option, via: "appenders.add(...)")
262
- RailsSemanticLogger.deprecator.warn(
319
+ message =
263
320
  "`config.rails_semantic_logger.#{option}=` is deprecated and will be removed in a future release. " \
264
321
  "Declare the destination and formatting directly instead, via " \
265
322
  "`config.rails_semantic_logger.appenders { |appenders| #{via} }`."
266
- )
323
+
324
+ # Skip this method and the setter that called it, so the warning is attributed
325
+ # to the application code that set the option, whether it is emitted now or
326
+ # flushed later by #flush_deprecations!.
327
+ callstack = caller_locations(2)
328
+
329
+ return RailsSemanticLogger.deprecator.warn(message, callstack) if deprecations_flushed?
330
+
331
+ pending_deprecations[option] ||= [message, callstack]
332
+ nil
333
+ end
334
+
335
+ # Deprecations recorded before Rails applied its deprecation configuration,
336
+ # keyed by option so that each is reported only once. See #flush_deprecations!.
337
+ def pending_deprecations
338
+ @pending_deprecations ||= {}
339
+ end
340
+
341
+ def deprecations_flushed?
342
+ defined?(@deprecations_flushed) && @deprecations_flushed
267
343
  end
268
344
 
269
345
  # Warn when an init-time setting is changed after the logger and its
@@ -42,7 +42,8 @@ module RailsSemanticLogger
42
42
  }
43
43
  handle.start
44
44
 
45
- logger.send(self.class.started_request_log_level) { started_request_message(request) }
45
+ # No call site: no application code has run yet, and this middleware's caller is another one.
46
+ logger.send(self.class.started_request_log_level, call_site: false) { started_request_message(request) }
46
47
  status, headers, body = @app.call(env)
47
48
  body = ::Rack::BodyProxy.new(body, &instrumenter_finish)
48
49
  [status, headers, body]
@@ -11,6 +11,8 @@ module RailsSemanticLogger
11
11
  job_hash = ctx[:job] || {}
12
12
  klass = job_hash["display_class"] || job_hash["wrapped"] || job_hash["class"]
13
13
  logger = klass ? SemanticLogger[klass] : ::Sidekiq.logger
14
+ # No call site: Sidekiq calls this handler with none of the application's code on the stack.
15
+ ctx = ctx.merge(call_site: false)
14
16
  ctx[:context] ? logger.info(ctx[:context], ctx) : logger.info(ctx)
15
17
  end
16
18
  end
@@ -28,7 +28,8 @@ module RailsSemanticLogger
28
28
  logger.info(
29
29
  "Start #perform",
30
30
  metric: "sidekiq.queue.latency",
31
- metric_amount: job_latency_ms(item)
31
+ metric_amount: job_latency_ms(item),
32
+ call_site: false
32
33
  )
33
34
  end
34
35
 
@@ -39,6 +40,7 @@ module RailsSemanticLogger
39
40
  on_exception_level: :error,
40
41
  log_exception: :full,
41
42
  metric: "sidekiq.job.perform",
43
+ call_site: false,
42
44
  &block
43
45
  )
44
46
  elsif block_given?
@@ -79,17 +81,19 @@ module RailsSemanticLogger
79
81
  (@config && @config[:logged_job_attributes]) || DEFAULT_LOGGED_JOB_ATTRIBUTES
80
82
  end
81
83
 
84
+ # Never negative: the clock of the host that enqueued the job can be ahead of this one's, and
85
+ # Semantic Logger 5.3 deprecates a negative metric_amount.
82
86
  def job_latency_ms(job)
83
87
  return unless job && job["enqueued_at"]
84
88
 
85
89
  enqueued_at = job["enqueued_at"]
86
90
  if enqueued_at.is_a?(Float)
87
91
  # Sidekiq <= 7: seconds since epoch
88
- (Time.now.to_f - enqueued_at) * 1000
92
+ [(Time.now.to_f - enqueued_at) * 1000, 0.0].max
89
93
  else
90
94
  # Sidekiq 8+: milliseconds since epoch
91
95
  now = Process.clock_gettime(Process::CLOCK_REALTIME, :millisecond)
92
- now - enqueued_at
96
+ [now - enqueued_at, 0].max
93
97
  end
94
98
  end
95
99
  end
@@ -164,8 +164,10 @@ module RailsSemanticLogger
164
164
  }
165
165
  end
166
166
 
167
+ # Solid Queue's own processes log these events, with no application code on the stack, so they
168
+ # record no call site rather than one inside Solid Queue.
167
169
  def log_event(event, level, action, exception: nil, **attributes)
168
- logger.public_send(level) do
170
+ logger.public_send(level, call_site: false) do
169
171
  msg = {message: action, payload: attributes, duration: event.duration}
170
172
  msg[:exception] = exception if exception
171
173
  # Emit a metric for every info/warn/error entry, named after the notification
@@ -1,3 +1,3 @@
1
1
  module RailsSemanticLogger
2
- VERSION = "5.1.0".freeze
2
+ VERSION = "5.3.0".freeze
3
3
  end
@@ -37,6 +37,7 @@ module RailsSemanticLogger
37
37
  autoload :LogSubscriber, "rails_semantic_logger/solid_queue/log_subscriber"
38
38
  end
39
39
 
40
+ autoload :ApplicationFrame, "rails_semantic_logger/application_frame"
40
41
  autoload :Appenders, "rails_semantic_logger/appenders"
41
42
  autoload :Options, "rails_semantic_logger/options"
42
43
 
@@ -46,6 +47,76 @@ module RailsSemanticLogger
46
47
  @deprecator ||= ActiveSupport::Deprecation.new("6.0", "rails_semantic_logger")
47
48
  end
48
49
 
50
+ # Whether an entry that the logger logs at the level records a call site: the logger logs at that
51
+ # level, and the level is at or above SemanticLogger.call_site_level. Otherwise there is no reason
52
+ # to look for the entry's call site. The level is any that Semantic Logger accepts, such as :info
53
+ # or ::Logger::INFO.
54
+ def self.call_site?(logger, level)
55
+ index = SemanticLogger::Levels.index(level)
56
+ index >= SemanticLogger.call_site_level_index && logger.public_send(:"#{SemanticLogger::Levels.level(index)}?")
57
+ end
58
+
59
+ # Returns the call site of an entry about the exception that the logger logs at the level, or nil
60
+ # when the entry records none: where the application raised the exception (see
61
+ # ApplicationFrame.in_backtrace), or else false, as for a routing error raised in middleware, or an
62
+ # exception that Rails raised itself, so that the entry records no call site rather than the frame
63
+ # that logged it.
64
+ def self.exception_call_site(exception, backtrace_cleaner, logger, level)
65
+ return unless call_site?(logger, level)
66
+
67
+ ApplicationFrame.in_backtrace(exception, backtrace_cleaner).call_site || false
68
+ end
69
+
70
+ # Returns [SemanticLogger::CallSite|nil] where the class's instance method is defined, such as a
71
+ # controller's action or a job's #perform, labelled with the class or module that defines it, as
72
+ # Ruby 3.4 labels the method's frames, such as "ArticlesController#index". It is the call site of an
73
+ # entry about that method when none of the application's code is on the stack, as when Rails
74
+ # reports that an action has completed.
75
+ #
76
+ # nil when the class has no such method, as for an action that only renders its template, or when
77
+ # Ruby cannot say where it is defined.
78
+ def self.method_call_site(klass, method_name)
79
+ method = klass.instance_method(method_name)
80
+ path, line = method.source_location
81
+ return unless path.is_a?(String) && line.is_a?(Integer)
82
+
83
+ owner = method.owner.name
84
+ label = owner ? "#{owner}##{method_name}" : method_name.to_s
85
+ SemanticLogger::CallSite.new(path: -path, line: line, label: -label)
86
+ rescue NameError
87
+ nil
88
+ end
89
+
90
+ # Record where each entry was logged in development, at every level, unless the application has
91
+ # already chosen a call site level. The engine calls this before the application's configuration
92
+ # is read, so that config/application.rb, config/environments/development.rb or an initializer can
93
+ # choose another level, or turn it off with `config.semantic_logger.call_site_level = nil`.
94
+ def self.record_call_sites_in_development(env)
95
+ SemanticLogger.call_site_level = :trace if env.development? && SemanticLogger.call_site_level.nil?
96
+ end
97
+
98
+ # Redact the application's filter parameters (`config.filter_parameters`) from every log entry,
99
+ # by making them Semantic Logger's sensitive keys (see SemanticLogger.sensitive_keys=), unless
100
+ # the application turned this off with `config.rails_semantic_logger.redact_filter_parameters`,
101
+ # or assigned its own `SemanticLogger.sensitive_keys`. An assigned nil cannot be told apart from
102
+ # none, so it does not turn redaction off.
103
+ #
104
+ # Called by the engine once the application has initialized, so that the filters added in
105
+ # `config/initializers/*` apply.
106
+ #
107
+ # Proc filters are left out: Semantic Logger asks the filter about each key by name alone, with
108
+ # no value, and a Proc filter changes the value it is given, rather than saying whether the key
109
+ # is sensitive.
110
+ def self.redact_filter_parameters(config)
111
+ return unless config.rails_semantic_logger.redact_filter_parameters
112
+ return unless SemanticLogger.sensitive_keys.nil?
113
+
114
+ filters = config.filter_parameters.reject { |filter| filter.respond_to?(:call) }
115
+ return if filters.empty?
116
+
117
+ SemanticLogger.sensitive_keys = ActiveSupport::ParameterFilter.new(filters)
118
+ end
119
+
49
120
  # Warn that a setting was changed too late to take effect, so the change has no
50
121
  # effect. When `config_initializers_too_late` is true (the default), the setting
51
122
  # is consumed while the logger is built, *before* `config/initializers/*` is
@@ -118,10 +189,20 @@ module RailsSemanticLogger
118
189
  def self.swap_subscriber(old_class, new_class, notifier)
119
190
  subscribers = ActiveSupport::LogSubscriber.subscribers.grep(old_class)
120
191
  subscribers.each { |subscriber| unattach(subscriber) }
192
+ unsubscribe_event_reporter(old_class)
121
193
 
122
194
  new_class.attach_to(notifier)
123
195
  end
124
196
 
197
+ # Rails 8.2 moved its text log subscribers onto ActiveSupport.event_reporter,
198
+ # so they are no longer attached to ActiveSupport::Notifications.
199
+ def self.unsubscribe_event_reporter(old_class)
200
+ return unless defined?(ActiveSupport::EventReporter::LogSubscriber)
201
+ return unless old_class < ActiveSupport::EventReporter::LogSubscriber
202
+
203
+ ActiveSupport.event_reporter.unsubscribe(old_class)
204
+ end
205
+
125
206
  def self.unattach(subscriber)
126
207
  subscriber_patterns(subscriber).each do |pattern|
127
208
  listeners_for(ActiveSupport::Notifications.notifier, pattern).each do |sub|
@@ -142,7 +223,7 @@ module RailsSemanticLogger
142
223
  notifier.all_listeners_for(pattern)
143
224
  end
144
225
 
145
- private_class_method :listeners_for, :subscriber_patterns, :unattach
226
+ private_class_method :listeners_for, :subscriber_patterns, :unattach, :unsubscribe_event_reporter
146
227
  end
147
228
 
148
229
  require("rails_semantic_logger/extensions/mongoid/config") if defined?(Mongoid)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails_semantic_logger
3
3
  version: !ruby/object:Gem::Version
4
- version: 5.1.0
4
+ version: 5.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Reid Morrison
@@ -43,14 +43,14 @@ dependencies:
43
43
  requirements:
44
44
  - - ">="
45
45
  - !ruby/object:Gem::Version
46
- version: '5.1'
46
+ version: '5.3'
47
47
  type: :runtime
48
48
  prerelease: false
49
49
  version_requirements: !ruby/object:Gem::Requirement
50
50
  requirements:
51
51
  - - ">="
52
52
  - !ruby/object:Gem::Version
53
- version: '5.1'
53
+ version: '5.3'
54
54
  executables: []
55
55
  extensions: []
56
56
  extra_rdoc_files: []
@@ -65,6 +65,7 @@ files:
65
65
  - lib/rails_semantic_logger/active_job/log_subscriber.rb
66
66
  - lib/rails_semantic_logger/active_record/log_subscriber.rb
67
67
  - lib/rails_semantic_logger/appenders.rb
68
+ - lib/rails_semantic_logger/application_frame.rb
68
69
  - lib/rails_semantic_logger/engine.rb
69
70
  - lib/rails_semantic_logger/extensions/action_cable/tagged_logger_proxy.rb
70
71
  - lib/rails_semantic_logger/extensions/action_controller/live.rb
@@ -83,13 +84,13 @@ files:
83
84
  - lib/rails_semantic_logger/sidekiq/loggable.rb
84
85
  - lib/rails_semantic_logger/solid_queue/log_subscriber.rb
85
86
  - lib/rails_semantic_logger/version.rb
86
- homepage: https://logger.rocketjob.io
87
+ homepage: https://logger.reidmorrison.com
87
88
  licenses:
88
89
  - Apache-2.0
89
90
  metadata:
90
91
  bug_tracker_uri: https://github.com/reidmorrison/rails_semantic_logger/issues
91
- documentation_uri: https://logger.rocketjob.io
92
- source_code_uri: https://github.com/reidmorrison/rails_semantic_logger/tree/v5.1.0
92
+ documentation_uri: https://logger.reidmorrison.com
93
+ source_code_uri: https://github.com/reidmorrison/rails_semantic_logger/tree/v5.3.0
93
94
  changelog_uri: https://github.com/reidmorrison/rails_semantic_logger/blob/main/CHANGELOG.md
94
95
  rubygems_mfa_required: 'true'
95
96
  rdoc_options: []