rails_semantic_logger 5.2.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.
- checksums.yaml +4 -4
- data/Rakefile +13 -2
- data/lib/rails_semantic_logger/action_controller/log_subscriber.rb +202 -33
- data/lib/rails_semantic_logger/action_mailer/log_subscriber.rb +21 -1
- data/lib/rails_semantic_logger/action_view/log_subscriber.rb +49 -16
- data/lib/rails_semantic_logger/active_job/log_subscriber.rb +62 -14
- data/lib/rails_semantic_logger/active_record/log_subscriber.rb +54 -27
- data/lib/rails_semantic_logger/application_frame.rb +103 -0
- data/lib/rails_semantic_logger/engine.rb +30 -11
- data/lib/rails_semantic_logger/extensions/action_controller/live.rb +11 -6
- data/lib/rails_semantic_logger/extensions/action_dispatch/debug_exceptions.rb +43 -2
- data/lib/rails_semantic_logger/extensions/action_view/streaming_template_renderer.rb +5 -1
- data/lib/rails_semantic_logger/options.rb +35 -1
- data/lib/rails_semantic_logger/rack/logger.rb +2 -1
- data/lib/rails_semantic_logger/sidekiq/defaults.rb +2 -0
- data/lib/rails_semantic_logger/sidekiq/job_logger.rb +7 -3
- data/lib/rails_semantic_logger/solid_queue/log_subscriber.rb +3 -1
- data/lib/rails_semantic_logger/version.rb +1 -1
- data/lib/rails_semantic_logger.rb +82 -1
- metadata +5 -4
|
@@ -9,9 +9,13 @@
|
|
|
9
9
|
#
|
|
10
10
|
# The upstream subscriber is functionally identical across Rails 7.2, 8.0, and 8.1 for everything
|
|
11
11
|
# that affects structured output. The only differences between those versions are in the internal
|
|
12
|
-
# `query_source_location` helper
|
|
13
|
-
#
|
|
14
|
-
#
|
|
12
|
+
# `query_source_location` helper, which finds the "↳ source" that `verbose_query_logs` adds below
|
|
13
|
+
# each debug line, and a `:nodoc:` comment. Rails 7.2 and 8.0 walk `Thread.each_caller_location`
|
|
14
|
+
# with the backtrace cleaner's `clean_frame`, and 8.1 calls the cleaner's `first_clean_frame`, which
|
|
15
|
+
# does the same. This subscriber walks the frames with `RailsSemanticLogger::ApplicationFrame`, as
|
|
16
|
+
# 7.2 and 8.0 do, on every version, and adds the source to the payload. The same frame is the entry's
|
|
17
|
+
# call site (see SemanticLogger.call_site_level). As a result no version-specific behavior is
|
|
18
|
+
# required here.
|
|
15
19
|
#
|
|
16
20
|
# As of Rails 8.1 there is also a parallel ActiveRecord::StructuredEventSubscriber (it emits
|
|
17
21
|
# structured events to Rails.event rather than text to Rails.logger). It is the authoritative,
|
|
@@ -25,6 +29,9 @@ module RailsSemanticLogger
|
|
|
25
29
|
class LogSubscriber < ActiveSupport::LogSubscriber
|
|
26
30
|
IGNORE_PAYLOAD_NAMES = %w[SCHEMA EXPLAIN].freeze
|
|
27
31
|
|
|
32
|
+
# The application's, as Rails gives its own Active Record log subscriber for verbose_query_logs.
|
|
33
|
+
class_attribute :backtrace_cleaner, default: ::Rails.backtrace_cleaner
|
|
34
|
+
|
|
28
35
|
def strict_loading_violation(event)
|
|
29
36
|
return unless logger.debug?
|
|
30
37
|
|
|
@@ -34,10 +41,14 @@ module RailsSemanticLogger
|
|
|
34
41
|
|
|
35
42
|
log_payload = {owner: owner.name, association: reflection.name}
|
|
36
43
|
log_payload[:class] = reflection.klass.name unless reflection.polymorphic?
|
|
44
|
+
frame = application_frame
|
|
45
|
+
source = frame&.source if ::ActiveRecord.verbose_query_logs
|
|
46
|
+
log_payload[:source] = source if source
|
|
37
47
|
|
|
38
48
|
logger.debug(
|
|
39
|
-
message:
|
|
40
|
-
payload:
|
|
49
|
+
message: reflection.strict_loading_violation_message(owner),
|
|
50
|
+
payload: log_payload,
|
|
51
|
+
call_site: frame&.call_site || false
|
|
41
52
|
)
|
|
42
53
|
end
|
|
43
54
|
|
|
@@ -56,15 +67,21 @@ module RailsSemanticLogger
|
|
|
56
67
|
log_payload[:async] = true
|
|
57
68
|
log_payload[:lock_wait] = payload[:lock_wait] if payload[:lock_wait]
|
|
58
69
|
end
|
|
70
|
+
frame = application_frame
|
|
71
|
+
source = frame&.source if ::ActiveRecord.verbose_query_logs
|
|
72
|
+
log_payload[:source] = source if source
|
|
59
73
|
|
|
60
74
|
log = {
|
|
61
|
-
message:
|
|
62
|
-
payload:
|
|
63
|
-
duration:
|
|
75
|
+
message: name,
|
|
76
|
+
payload: log_payload,
|
|
77
|
+
duration: event.duration,
|
|
78
|
+
# No call site when no application code ran the query, rather than Active Support's frame.
|
|
79
|
+
call_site: frame&.call_site || false
|
|
64
80
|
}
|
|
65
81
|
|
|
66
|
-
# Log the location of the query itself.
|
|
67
|
-
|
|
82
|
+
# Log the location of the query itself. The entry is logged at :debug, so that is the level
|
|
83
|
+
# to compare, as Semantic Logger does, rather than the logger's own level.
|
|
84
|
+
if SemanticLogger::Levels.index(:debug) >= SemanticLogger.backtrace_level_index
|
|
68
85
|
log[:backtrace] = SemanticLogger::Utils.strip_backtrace(caller)
|
|
69
86
|
end
|
|
70
87
|
|
|
@@ -73,9 +90,19 @@ module RailsSemanticLogger
|
|
|
73
90
|
|
|
74
91
|
private
|
|
75
92
|
|
|
93
|
+
# Returns [ApplicationFrame|nil] the application code that ran the query, or that triggered the
|
|
94
|
+
# strict loading violation, when the entry needs it: as its source, which Rails logs as
|
|
95
|
+
# "↳ source" below the entry when `verbose_query_logs` is on, or as its call site. nil when it
|
|
96
|
+
# needs neither.
|
|
97
|
+
def application_frame
|
|
98
|
+
return unless ::ActiveRecord.verbose_query_logs || RailsSemanticLogger.call_site?(logger, :debug)
|
|
99
|
+
|
|
100
|
+
RailsSemanticLogger::ApplicationFrame.on_stack(backtrace_cleaner, for_source: ::ActiveRecord.verbose_query_logs)
|
|
101
|
+
end
|
|
102
|
+
|
|
76
103
|
# When multiple values are received for a single bound field, it is converted into an array
|
|
77
104
|
def add_bind_value(binds, key, value)
|
|
78
|
-
key = key.downcase.to_sym
|
|
105
|
+
key = key.downcase.to_sym
|
|
79
106
|
|
|
80
107
|
value = (Array(binds[key]) << value) if binds.key?(key)
|
|
81
108
|
binds[key] = value
|
|
@@ -89,32 +116,32 @@ module RailsSemanticLogger
|
|
|
89
116
|
binds = {}
|
|
90
117
|
casted_params = type_casted_binds(payload[:type_casted_binds])
|
|
91
118
|
payload[:binds].each_with_index do |attr, i|
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
119
|
+
name = bind_name(attr)
|
|
120
|
+
value = render_bind(attr, filter(name, casted_params[i]))
|
|
121
|
+
# Binds without a column name (casted binds: plain values, as passed to `select_all`) are
|
|
122
|
+
# keyed by their parameter position, `$1`, `$2`, ..., as Rails 8.2 does.
|
|
123
|
+
add_bind_value(binds, name || "$#{i + 1}", value)
|
|
95
124
|
end
|
|
96
125
|
binds
|
|
97
126
|
end
|
|
98
127
|
|
|
99
|
-
|
|
100
|
-
|
|
128
|
+
# The column name a bind is filtered and logged under, or nil when it has none.
|
|
129
|
+
# Rails 7.2 to 8.1 also filter by the name of any bind that responds to `name`, including
|
|
130
|
+
# `[column, value]` pairs, so those keep their name here and are filtered on every version.
|
|
131
|
+
# A Symbol responds to `name` too, but as a bind it is a plain value, not a column.
|
|
132
|
+
def bind_name(attr)
|
|
133
|
+
if attr.is_a?(Array)
|
|
134
|
+
column = attr.first
|
|
135
|
+
column.name if column.respond_to?(:name)
|
|
136
|
+
elsif attr.respond_to?(:name) && !attr.is_a?(Symbol)
|
|
101
137
|
attr.name
|
|
102
|
-
elsif attr.respond_to?(:[]) && attr[index].respond_to?(:name)
|
|
103
|
-
attr[index].name
|
|
104
138
|
end
|
|
105
139
|
end
|
|
106
140
|
|
|
107
141
|
def render_bind(attr, value)
|
|
108
|
-
|
|
109
|
-
when ActiveModel::Attribute
|
|
110
|
-
value = "<#{attr.value_for_database.to_s.bytesize} bytes of binary data>" if attr.type.binary? && attr.value
|
|
111
|
-
when Array
|
|
112
|
-
attr = attr.first
|
|
113
|
-
else
|
|
114
|
-
attr = nil
|
|
115
|
-
end
|
|
142
|
+
return value unless attr.is_a?(ActiveModel::Attribute) && attr.type.binary? && attr.value
|
|
116
143
|
|
|
117
|
-
|
|
144
|
+
"<#{attr.value_for_database.to_s.bytesize} bytes of binary data>"
|
|
118
145
|
end
|
|
119
146
|
|
|
120
147
|
# Filter sensitive bind values using the same parameter filter Rails uses for ActiveRecord,
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
module RailsSemanticLogger
|
|
2
|
+
# The application code behind a log entry: the first frame that the application's backtrace cleaner
|
|
3
|
+
# keeps, on the stack that led to the entry (.on_stack) or in an exception's backtrace (.in_backtrace).
|
|
4
|
+
#
|
|
5
|
+
# Rails' verbose_*_logs options write that frame as the entry's source (#source), wherever it is. It
|
|
6
|
+
# is the entry's call site (#call_site, see SemanticLogger.call_site_level) only when Rails did not
|
|
7
|
+
# take over from the application before reaching it: application code further out, such as an
|
|
8
|
+
# around_action or a worker's middleware, did not cause what Rails then logged.
|
|
9
|
+
class ApplicationFrame
|
|
10
|
+
# This gem's own code, which is never the application code behind an entry: lib/rails_semantic_logger/
|
|
11
|
+
# and lib/rails_semantic_logger.rb, as Ruby loaded them.
|
|
12
|
+
OWN_PATHS = ["#{File.dirname(__FILE__)}/", "#{File.dirname(__FILE__)}.rb"].freeze
|
|
13
|
+
|
|
14
|
+
# Where Rails dispatches an action, and renders its template once the action has returned.
|
|
15
|
+
ACTION_DISPATCH_PATHS = %w[
|
|
16
|
+
/action_controller/metal/basic_implicit_render.rb /action_controller/metal/implicit_render.rb
|
|
17
|
+
].freeze
|
|
18
|
+
|
|
19
|
+
# Where Active Job performs a job from its serialized form (Base.execute), as a worker does with a job
|
|
20
|
+
# that it took from its queue, and as the inline and test adapters do.
|
|
21
|
+
JOB_EXECUTION_PATH = "/active_job/execution.rb".freeze
|
|
22
|
+
|
|
23
|
+
# The name Rails gives the method of a compiled template, such as
|
|
24
|
+
# "_app_views_orders_show_html_erb__1935574751676049736_7680", with numbers that change on every boot,
|
|
25
|
+
# as matched by Rails' own backtrace cleaner's RENDER_TEMPLATE_PATTERN.
|
|
26
|
+
COMPILED_TEMPLATE_METHOD = /\A_\w+_{2,3}\d+_\d+\z/
|
|
27
|
+
|
|
28
|
+
# Returns [ApplicationFrame] the application code on the current call stack, skipping this gem's own
|
|
29
|
+
# frames. Without for_source the walk stops where Rails took over, since a frame beyond it is not the
|
|
30
|
+
# entry's call site.
|
|
31
|
+
def self.on_stack(backtrace_cleaner, for_source: false)
|
|
32
|
+
took_over = false
|
|
33
|
+
Thread.each_caller_location do |location|
|
|
34
|
+
next if location.path.start_with?(*OWN_PATHS)
|
|
35
|
+
|
|
36
|
+
took_over ||= rails_took_over?(location)
|
|
37
|
+
return new(backtrace_cleaner, nil, took_over: true) if took_over && !for_source
|
|
38
|
+
return new(backtrace_cleaner, location, took_over: took_over) if backtrace_cleaner.clean_frame(location)
|
|
39
|
+
end
|
|
40
|
+
new(backtrace_cleaner, nil, took_over: took_over)
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
# Returns [ApplicationFrame] the application code in the exception's backtrace, where it raised the
|
|
44
|
+
# exception. The search stops where Rails took over, as for an exception that Rails raised while it
|
|
45
|
+
# rendered an action's template.
|
|
46
|
+
def self.in_backtrace(exception, backtrace_cleaner)
|
|
47
|
+
(exception.backtrace_locations || []).each do |location|
|
|
48
|
+
return new(backtrace_cleaner, nil, took_over: true) if rails_took_over?(location)
|
|
49
|
+
return new(backtrace_cleaner, location) if backtrace_cleaner.clean_frame(location)
|
|
50
|
+
end
|
|
51
|
+
new(backtrace_cleaner, nil)
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
# Whether Rails, at this frame, takes over from the application's code to run code of its own: it
|
|
55
|
+
# dispatches an action and renders its template once the action has returned, or performs a job from
|
|
56
|
+
# its serialized form.
|
|
57
|
+
def self.rails_took_over?(location)
|
|
58
|
+
path = location.path.to_s
|
|
59
|
+
path.end_with?(*ACTION_DISPATCH_PATHS) ||
|
|
60
|
+
(path.end_with?(JOB_EXECUTION_PATH) && base_label(location) == "execute")
|
|
61
|
+
end
|
|
62
|
+
private_class_method :rails_took_over?
|
|
63
|
+
|
|
64
|
+
# Returns [String|nil] the frame's method name. Rails 7.2 and 8.0 give the line with a SyntaxError
|
|
65
|
+
# a frame that has none.
|
|
66
|
+
def self.base_label(location)
|
|
67
|
+
location.base_label if location.respond_to?(:base_label)
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
# A Thread::Backtrace::Location, or, in the backtrace of a SyntaxError that Rails wraps, a frame
|
|
71
|
+
# that responds to #path and #lineno, and may have a #label and #base_label.
|
|
72
|
+
attr_reader :location
|
|
73
|
+
|
|
74
|
+
def initialize(backtrace_cleaner, location, took_over: false)
|
|
75
|
+
@backtrace_cleaner = backtrace_cleaner
|
|
76
|
+
@location = location
|
|
77
|
+
@took_over = took_over
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
# Returns [String|nil] the frame as the backtrace cleaner writes it, such as
|
|
81
|
+
# "app/models/user.rb:12:in 'User.recent'", which Rails logs as the "↳ source" of an entry. nil when
|
|
82
|
+
# the cleaner keeps no frame.
|
|
83
|
+
def source
|
|
84
|
+
# A String: Rails 8.0+'s cleaner returns the location unchanged when BACKTRACE is set.
|
|
85
|
+
@backtrace_cleaner.clean_frame(location).to_s if location
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
# Returns [Thread::Backtrace::Location|SemanticLogger::CallSite|nil] the entry's call site, or nil
|
|
89
|
+
# when there is no application frame, or Rails took over before it. Another frame becomes a call
|
|
90
|
+
# site with its path, line and label, as Semantic Logger records a location. A frame in a compiled
|
|
91
|
+
# template has no label, since Rails names the template's method with numbers that change on
|
|
92
|
+
# every boot.
|
|
93
|
+
def call_site
|
|
94
|
+
return if location.nil? || @took_over
|
|
95
|
+
|
|
96
|
+
compiled = COMPILED_TEMPLATE_METHOD.match?(self.class.base_label(location).to_s)
|
|
97
|
+
return location if location.is_a?(Thread::Backtrace::Location) && !compiled
|
|
98
|
+
|
|
99
|
+
label = location.label&.to_s if !compiled && location.respond_to?(:label)
|
|
100
|
+
SemanticLogger::CallSite.new(path: -location.path.to_s, line: location.lineno.to_i, label: label && -label)
|
|
101
|
+
end
|
|
102
|
+
end
|
|
103
|
+
end
|
|
@@ -21,6 +21,24 @@ module RailsSemanticLogger
|
|
|
21
21
|
|
|
22
22
|
config.rails_semantic_logger = RailsSemanticLogger::Options.new
|
|
23
23
|
|
|
24
|
+
# Clean the backtraces of logged exceptions with the application's backtrace cleaner, as Rails'
|
|
25
|
+
# own log does: only the application's frames, relative to Rails.root, honoring the silencers in
|
|
26
|
+
# config/initializers/backtrace_silencers.rb, and every frame when BACKTRACE is set.
|
|
27
|
+
#
|
|
28
|
+
# Assigned before the application's configuration is read, so that the application can choose
|
|
29
|
+
# another cleaner, or none, with `config.semantic_logger.backtrace_cleaner = ...` in
|
|
30
|
+
# config/application.rb or config/environments/*.rb.
|
|
31
|
+
#
|
|
32
|
+
# In development, also record where each entry was logged, at every level: the file, line, and
|
|
33
|
+
# method of the code behind it. Assigned before the application's configuration is read, so that
|
|
34
|
+
# the application can choose another level, or turn it off, with
|
|
35
|
+
# `config.semantic_logger.call_site_level = nil` in config/application.rb,
|
|
36
|
+
# config/environments/development.rb, or an initializer.
|
|
37
|
+
config.before_configuration do
|
|
38
|
+
::SemanticLogger.backtrace_cleaner = ::Rails.backtrace_cleaner
|
|
39
|
+
RailsSemanticLogger.record_call_sites_in_development(::Rails.env)
|
|
40
|
+
end
|
|
41
|
+
|
|
24
42
|
# Initialize SemanticLogger. In a Rails environment it will automatically
|
|
25
43
|
# insert itself above the configured rails logger to add support for its
|
|
26
44
|
# additional features
|
|
@@ -190,6 +208,13 @@ module RailsSemanticLogger
|
|
|
190
208
|
end
|
|
191
209
|
end
|
|
192
210
|
|
|
211
|
+
# Redact the application's filter parameters from every log entry. Not before now, since
|
|
212
|
+
# config/initializers/filter_parameter_logging.rb adds to them. Registered before the hook below,
|
|
213
|
+
# so that it runs first.
|
|
214
|
+
config.after_initialize do
|
|
215
|
+
RailsSemanticLogger.redact_filter_parameters(Rails.application.config)
|
|
216
|
+
end
|
|
217
|
+
|
|
193
218
|
# After any initializers run, but after the gems have been loaded
|
|
194
219
|
config.after_initialize do
|
|
195
220
|
config = Rails.application.config
|
|
@@ -211,15 +236,8 @@ module RailsSemanticLogger
|
|
|
211
236
|
|
|
212
237
|
if config.rails_semantic_logger.semantic
|
|
213
238
|
# Active Job
|
|
214
|
-
if defined?(::ActiveJob
|
|
215
|
-
|
|
216
|
-
::ActiveJob::Logging::LogSubscriber,
|
|
217
|
-
RailsSemanticLogger::ActiveJob::LogSubscriber,
|
|
218
|
-
:active_job
|
|
219
|
-
)
|
|
220
|
-
end
|
|
221
|
-
|
|
222
|
-
if defined?(::ActiveJob::LogSubscriber)
|
|
239
|
+
if defined?(::ActiveJob)
|
|
240
|
+
require "active_job/log_subscriber"
|
|
223
241
|
RailsSemanticLogger.swap_subscriber(
|
|
224
242
|
::ActiveJob::LogSubscriber,
|
|
225
243
|
RailsSemanticLogger::ActiveJob::LogSubscriber,
|
|
@@ -310,8 +328,9 @@ module RailsSemanticLogger
|
|
|
310
328
|
# Add a stderr logger when running inside a Rails console unless one has already been added.
|
|
311
329
|
RailsSemanticLogger.add_console_appenders
|
|
312
330
|
|
|
313
|
-
# Include method
|
|
314
|
-
|
|
331
|
+
# Include where each entry was logged, its file, line, and method, on the entries logged in the
|
|
332
|
+
# console. Unlike a backtrace, this reads only the frames up to the code that logged the entry.
|
|
333
|
+
SemanticLogger.call_site_level = SemanticLogger.default_level
|
|
315
334
|
end
|
|
316
335
|
|
|
317
336
|
# Initialization is complete; the post-init settings have been consumed, so
|
|
@@ -1,11 +1,16 @@
|
|
|
1
|
-
# Log actual exceptions, not a string representation
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
1
|
+
# Log actual exceptions, not a string representation.
|
|
2
|
+
#
|
|
3
|
+
# Patched once Action Controller has loaded, because loading ActionController::Live also loads
|
|
4
|
+
# ActionDispatch::Response, and Rails 8.2+ reports that when it happens before the application
|
|
5
|
+
# has initialized.
|
|
6
|
+
ActiveSupport.on_load(:action_controller, run_once: true) do
|
|
7
|
+
ActionController::Live.module_eval do
|
|
6
8
|
undef_method :log_error
|
|
7
9
|
def log_error(exception)
|
|
8
|
-
logger.fatal(
|
|
10
|
+
logger.fatal(
|
|
11
|
+
exception: exception,
|
|
12
|
+
call_site: RailsSemanticLogger.exception_call_site(exception, Rails.backtrace_cleaner, logger, :fatal)
|
|
13
|
+
)
|
|
9
14
|
end
|
|
10
15
|
end
|
|
11
16
|
end
|
|
@@ -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
|
|
14
|
-
ActionController::Base.logger
|
|
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
|
|
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[
|
|
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
|
|
@@ -42,7 +42,8 @@ module RailsSemanticLogger
|
|
|
42
42
|
}
|
|
43
43
|
handle.start
|
|
44
44
|
|
|
45
|
-
|
|
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
|
|
@@ -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)
|