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.
- checksums.yaml +4 -4
- data/README.md +5 -5
- 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 +51 -11
- data/lib/rails_semantic_logger/extensions/action_cable/tagged_logger_proxy.rb +69 -58
- 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 +79 -3
- 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 +7 -6
|
@@ -17,16 +17,27 @@ require "active_job"
|
|
|
17
17
|
# The Continuation handlers are defined unconditionally. On Rails < 8.1 those notifications are
|
|
18
18
|
# never emitted, so the extra methods are simply never invoked.
|
|
19
19
|
#
|
|
20
|
+
# When `verbose_enqueue_logs` is on, Rails logs a "↳ source" line, the first frame of the call stack
|
|
21
|
+
# that the backtrace cleaner keeps, after every info and error line of its subscriber. This
|
|
22
|
+
# subscriber adds it to the payload of the same entries, as `source`. For an enqueue it is the code
|
|
23
|
+
# that enqueued the job. A job performed by a worker has no application frame on its call stack
|
|
24
|
+
# outside the job itself, so its perform entries get none, while its Continuation step entries
|
|
25
|
+
# (Rails 8.1+), instrumented inside the job's own perform, get the job's line. A job performed
|
|
26
|
+
# inline, such as with `perform_now`, gets the code that performed it.
|
|
27
|
+
#
|
|
20
28
|
module RailsSemanticLogger
|
|
21
29
|
module ActiveJob
|
|
22
30
|
class LogSubscriber < ::ActiveSupport::LogSubscriber
|
|
31
|
+
# The application's, as Rails gives its own Active Job log subscriber.
|
|
32
|
+
class_attribute :backtrace_cleaner, default: ::Rails.backtrace_cleaner
|
|
33
|
+
|
|
23
34
|
def enqueue(event)
|
|
24
35
|
ex = enqueue_error(event)
|
|
25
36
|
|
|
26
37
|
if ex
|
|
27
38
|
log_with_formatter level: :error, event: event do |fmt|
|
|
28
39
|
{
|
|
29
|
-
message: "Failed enqueuing #{fmt.job_info}
|
|
40
|
+
message: "Failed enqueuing #{fmt.job_info}: #{ex.class} (#{ex.message})",
|
|
30
41
|
exception: ex
|
|
31
42
|
}
|
|
32
43
|
end
|
|
@@ -47,7 +58,7 @@ module RailsSemanticLogger
|
|
|
47
58
|
if ex
|
|
48
59
|
log_with_formatter level: :error, event: event do |fmt|
|
|
49
60
|
{
|
|
50
|
-
message: "Failed enqueuing #{fmt.job_info}
|
|
61
|
+
message: "Failed enqueuing #{fmt.job_info}: #{ex.class} (#{ex.message})",
|
|
51
62
|
exception: ex
|
|
52
63
|
}
|
|
53
64
|
end
|
|
@@ -231,17 +242,20 @@ module RailsSemanticLogger
|
|
|
231
242
|
"Failed enqueuing #{failed_count} #{'job'.pluralize(failed_count)} to #{adapter_name}"
|
|
232
243
|
end
|
|
233
244
|
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
+
payload = {
|
|
246
|
+
event_name: event.name,
|
|
247
|
+
adapter: adapter_name,
|
|
248
|
+
enqueued_count: enqueued_count,
|
|
249
|
+
total_count: jobs.size,
|
|
250
|
+
job_classes: jobs.map { |job| job.class.name }.tally
|
|
251
|
+
}
|
|
252
|
+
frame = application_frame(:info)
|
|
253
|
+
source = frame&.source if enqueue_source?(:info)
|
|
254
|
+
payload[:source] = source if source
|
|
255
|
+
# No call site when no application code enqueued the jobs, rather than Active Support's frame.
|
|
256
|
+
call_site = frame&.call_site || false if RailsSemanticLogger.call_site?(logger, :info)
|
|
257
|
+
|
|
258
|
+
logger.info(message: message, metric: "rails.job.enqueue_all", payload: payload, call_site: call_site)
|
|
245
259
|
end
|
|
246
260
|
|
|
247
261
|
private
|
|
@@ -356,7 +370,41 @@ module RailsSemanticLogger
|
|
|
356
370
|
# Emit a metric for every info/warn/error entry, named after the notification
|
|
357
371
|
# (e.g. "enqueue.active_job" -> "rails.job.enqueue"). Debug entries are excluded.
|
|
358
372
|
msg[:metric] ||= "rails.job.#{kw_args[:event].name.split('.').first}" unless level == :debug
|
|
359
|
-
|
|
373
|
+
frame = application_frame(level)
|
|
374
|
+
source = frame&.source if enqueue_source?(level)
|
|
375
|
+
extra = extra.merge(source: source) if source
|
|
376
|
+
logger.public_send(
|
|
377
|
+
level,
|
|
378
|
+
**msg,
|
|
379
|
+
payload: fmt.payload.merge(extra),
|
|
380
|
+
call_site: call_site(frame, kw_args[:event], level)
|
|
381
|
+
)
|
|
382
|
+
end
|
|
383
|
+
|
|
384
|
+
# Returns [ApplicationFrame|nil] the application code behind an entry logged at the level, such as
|
|
385
|
+
# the code that enqueued or performed the job, when the entry needs it: as its source when
|
|
386
|
+
# `verbose_enqueue_logs` is on, or as its call site. nil when it needs neither.
|
|
387
|
+
def application_frame(level)
|
|
388
|
+
return unless enqueue_source?(level) || RailsSemanticLogger.call_site?(logger, level)
|
|
389
|
+
|
|
390
|
+
RailsSemanticLogger::ApplicationFrame.on_stack(backtrace_cleaner, for_source: enqueue_source?(level))
|
|
391
|
+
end
|
|
392
|
+
|
|
393
|
+
# Whether an entry logged at the level has a source: `verbose_enqueue_logs` is on, and Rails logs
|
|
394
|
+
# its "↳ source" below the entry, which it does for info and error lines, and the logger logs it.
|
|
395
|
+
def enqueue_source?(level)
|
|
396
|
+
::ActiveJob.verbose_enqueue_logs && %i[info error].include?(level) && logger.public_send(:"#{level}?")
|
|
397
|
+
end
|
|
398
|
+
|
|
399
|
+
# Returns the call site of an entry logged at the level about the event's job, or nil when it
|
|
400
|
+
# records none: the application code behind it, such as the code that called perform_now, else
|
|
401
|
+
# the job's #perform, as for a job performed from its queue, else false, so that the entry records
|
|
402
|
+
# no call site rather than Active Support's frame.
|
|
403
|
+
def call_site(frame, event, level)
|
|
404
|
+
return unless RailsSemanticLogger.call_site?(logger, level)
|
|
405
|
+
|
|
406
|
+
job = event.payload[:job]
|
|
407
|
+
frame&.call_site || (job && RailsSemanticLogger.method_call_site(job.class, :perform)) || false
|
|
360
408
|
end
|
|
361
409
|
|
|
362
410
|
def logger
|
|
@@ -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,10 +21,49 @@ 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
|
|
27
45
|
|
|
46
|
+
# Register this gem's deprecator with the application so that the standard Rails
|
|
47
|
+
# deprecation settings govern it: `config.active_support.report_deprecations`,
|
|
48
|
+
# `config.active_support.deprecation`, and
|
|
49
|
+
# `Rails.application.deprecators[:rails_semantic_logger]`. Rails' own
|
|
50
|
+
# `active_support.deprecation_behavior` initializer only reaches deprecators in
|
|
51
|
+
# that collection.
|
|
52
|
+
#
|
|
53
|
+
# Runs before the environment file is read so that an application can also reach
|
|
54
|
+
# the deprecator by name from `config/environments/*.rb`.
|
|
55
|
+
initializer "rails_semantic_logger.deprecator", before: :load_environment_config do |app|
|
|
56
|
+
app.deprecators[:rails_semantic_logger] = RailsSemanticLogger.deprecator
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
# The deprecated appender options are set from `config/application.rb` and
|
|
60
|
+
# `config/environments/*.rb`, which Rails reads before it applies the deprecation
|
|
61
|
+
# settings above, so their warnings are recorded rather than emitted. Now that
|
|
62
|
+
# the settings are in place, emit them.
|
|
63
|
+
initializer "rails_semantic_logger.flush_deprecations", after: "active_support.deprecation_behavior" do |app|
|
|
64
|
+
app.config.rails_semantic_logger.flush_deprecations!
|
|
65
|
+
end
|
|
66
|
+
|
|
28
67
|
# Replace Rails logger initializer
|
|
29
68
|
Rails::Application::Bootstrap.initializers.delete_if { |i| i.name == :initialize_logger }
|
|
30
69
|
|
|
@@ -169,6 +208,13 @@ module RailsSemanticLogger
|
|
|
169
208
|
end
|
|
170
209
|
end
|
|
171
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
|
+
|
|
172
218
|
# After any initializers run, but after the gems have been loaded
|
|
173
219
|
config.after_initialize do
|
|
174
220
|
config = Rails.application.config
|
|
@@ -190,15 +236,8 @@ module RailsSemanticLogger
|
|
|
190
236
|
|
|
191
237
|
if config.rails_semantic_logger.semantic
|
|
192
238
|
# Active Job
|
|
193
|
-
if defined?(::ActiveJob
|
|
194
|
-
|
|
195
|
-
::ActiveJob::Logging::LogSubscriber,
|
|
196
|
-
RailsSemanticLogger::ActiveJob::LogSubscriber,
|
|
197
|
-
:active_job
|
|
198
|
-
)
|
|
199
|
-
end
|
|
200
|
-
|
|
201
|
-
if defined?(::ActiveJob::LogSubscriber)
|
|
239
|
+
if defined?(::ActiveJob)
|
|
240
|
+
require "active_job/log_subscriber"
|
|
202
241
|
RailsSemanticLogger.swap_subscriber(
|
|
203
242
|
::ActiveJob::LogSubscriber,
|
|
204
243
|
RailsSemanticLogger::ActiveJob::LogSubscriber,
|
|
@@ -289,8 +328,9 @@ module RailsSemanticLogger
|
|
|
289
328
|
# Add a stderr logger when running inside a Rails console unless one has already been added.
|
|
290
329
|
RailsSemanticLogger.add_console_appenders
|
|
291
330
|
|
|
292
|
-
# Include method
|
|
293
|
-
|
|
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
|
|
294
334
|
end
|
|
295
335
|
|
|
296
336
|
# Initialization is complete; the post-init settings have been consumed, so
|
|
@@ -1,70 +1,81 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
# Upstream source (identical across supported versions):
|
|
1
|
+
# Upstream source (identical across supported versions, only the namespace differs):
|
|
4
2
|
# https://github.com/rails/rails/blob/v7.2.2/actioncable/lib/action_cable/connection/tagged_logger_proxy.rb
|
|
5
3
|
# https://github.com/rails/rails/blob/v8.0.2/actioncable/lib/action_cable/connection/tagged_logger_proxy.rb
|
|
6
4
|
# https://github.com/rails/rails/blob/v8.1.3/actioncable/lib/action_cable/connection/tagged_logger_proxy.rb
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
logger.tagged(*current_tags, &)
|
|
21
|
-
else
|
|
22
|
-
yield
|
|
23
|
-
end
|
|
5
|
+
# https://github.com/rails/rails/blob/main/actioncable/lib/action_cable/server/tagged_logger_proxy.rb
|
|
6
|
+
module RailsSemanticLogger
|
|
7
|
+
module ActionCable
|
|
8
|
+
# Rails 8.2 moved the proxy from ActionCable::Connection to ActionCable::Server, as part of
|
|
9
|
+
# the ActionCable::Server::Socket refactor, without leaving a back-compat alias behind.
|
|
10
|
+
# Resolve whichever constant this version of ActionCable ships rather than hard-coding a
|
|
11
|
+
# namespace, so that a `require` of the old path cannot fail the app's boot. See #326.
|
|
12
|
+
TaggedLoggerProxy =
|
|
13
|
+
if defined?(::ActionCable::Server::TaggedLoggerProxy)
|
|
14
|
+
::ActionCable::Server::TaggedLoggerProxy
|
|
15
|
+
else
|
|
16
|
+
require "action_cable/connection/tagged_logger_proxy"
|
|
17
|
+
::ActionCable::Connection::TaggedLoggerProxy
|
|
24
18
|
end
|
|
19
|
+
end
|
|
20
|
+
end
|
|
25
21
|
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
22
|
+
RailsSemanticLogger::ActionCable::TaggedLoggerProxy.class_eval do
|
|
23
|
+
# Mirrors upstream #tag, including the `respond_to?(:tagged)` guard so a
|
|
24
|
+
# target logger without tagging support (e.g. when handed a non-tagged
|
|
25
|
+
# ActiveRecord::Base.logger by the worker pool) simply yields. Diverges in
|
|
26
|
+
# one place: upstream reads the already-applied tags via
|
|
27
|
+
# `logger.formatter.current_tags` (ActiveSupport::TaggedLogging), but
|
|
28
|
+
# Semantic Logger exposes them via `#tags`, so resolve from whichever the
|
|
29
|
+
# target logger supports. See #220.
|
|
30
|
+
def tag(logger, &)
|
|
31
|
+
if logger.respond_to?(:tagged)
|
|
32
|
+
current_tags = tags - applied_tags_for(logger)
|
|
33
|
+
logger.tagged(*current_tags, &)
|
|
34
|
+
else
|
|
35
|
+
yield
|
|
36
|
+
end
|
|
37
|
+
end
|
|
36
38
|
|
|
37
|
-
|
|
39
|
+
# Upstream defines the severity methods with a single-arg signature
|
|
40
|
+
# (`message = nil`), which discards Semantic Logger's richer payload and
|
|
41
|
+
# exception arguments and raises ArgumentError when they are supplied.
|
|
42
|
+
# Redefine them to forward the full Semantic Logger signature down to the
|
|
43
|
+
# wrapped logger, while still applying the connection's tags. See #220.
|
|
44
|
+
%i[debug info warn error fatal unknown].each do |severity|
|
|
45
|
+
define_method(severity) do |message = nil, payload = nil, exception = nil, &block|
|
|
46
|
+
tag(@logger) { forward(severity, message, payload, exception, &block) }
|
|
47
|
+
end
|
|
48
|
+
end
|
|
38
49
|
|
|
39
|
-
|
|
40
|
-
# method can accept them. A standard Ruby Logger (e.g. the one wrapped by
|
|
41
|
-
# ActionCable::Connection::TestCase) only accepts a single `progname` argument
|
|
42
|
-
# and raises ArgumentError given more, so fall back to the single-arg call. See #317.
|
|
43
|
-
def forward(severity, message, payload, exception, &)
|
|
44
|
-
if extended_signature?(@logger, severity)
|
|
45
|
-
@logger.public_send(severity, message, payload, exception, &)
|
|
46
|
-
else
|
|
47
|
-
@logger.public_send(severity, message, &)
|
|
48
|
-
end
|
|
49
|
-
end
|
|
50
|
+
private
|
|
50
51
|
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
52
|
+
# Only forward the extra payload/exception arguments when the wrapped logger's
|
|
53
|
+
# method can accept them. A standard Ruby Logger (e.g. the one wrapped by
|
|
54
|
+
# ActionCable::Connection::TestCase) only accepts a single `progname` argument
|
|
55
|
+
# and raises ArgumentError given more, so fall back to the single-arg call. See #317.
|
|
56
|
+
def forward(severity, message, payload, exception, &)
|
|
57
|
+
if extended_signature?(@logger, severity)
|
|
58
|
+
@logger.public_send(severity, message, payload, exception, &)
|
|
59
|
+
else
|
|
60
|
+
@logger.public_send(severity, message, &)
|
|
61
|
+
end
|
|
62
|
+
end
|
|
55
63
|
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
64
|
+
def extended_signature?(logger, severity)
|
|
65
|
+
params = logger.method(severity).parameters
|
|
66
|
+
params.any? { |type, _| type == :rest } || params.count { |type, _| %i[req opt].include?(type) } >= 3
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
# Tags already applied to the target logger, so they are not duplicated.
|
|
70
|
+
# Semantic Logger exposes them via `#tags`; ActiveSupport::TaggedLogging
|
|
71
|
+
# via `formatter.current_tags`. Fall back to none when neither is present.
|
|
72
|
+
def applied_tags_for(logger)
|
|
73
|
+
if logger.respond_to?(:tags)
|
|
74
|
+
Array(logger.tags)
|
|
75
|
+
elsif logger.respond_to?(:formatter) && logger.formatter.respond_to?(:current_tags)
|
|
76
|
+
logger.formatter.current_tags
|
|
77
|
+
else
|
|
78
|
+
[]
|
|
68
79
|
end
|
|
69
80
|
end
|
|
70
81
|
end
|
|
@@ -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
|