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 +13 -12
- data/Rakefile +15 -4
- data/docs/api.md +196 -19
- data/docs/appenders.md +579 -68
- data/docs/config.md +647 -38
- data/docs/index.md +31 -5
- data/docs/log.md +56 -17
- data/docs/metrics.md +144 -30
- data/docs/operations.md +339 -24
- data/docs/rails.md +396 -8
- data/docs/security.md +222 -27
- data/docs/testing.md +28 -3
- data/docs/upgrading.md +938 -7
- data/lib/semantic_logger/appender/async.rb +42 -11
- data/lib/semantic_logger/appender/bugsnag.rb +7 -2
- data/lib/semantic_logger/appender/cloudwatch_logs.rb +168 -33
- data/lib/semantic_logger/appender/elasticsearch_base.rb +221 -28
- data/lib/semantic_logger/appender/elasticsearch_http.rb +23 -6
- data/lib/semantic_logger/appender/file.rb +160 -37
- data/lib/semantic_logger/appender/graylog.rb +101 -11
- data/lib/semantic_logger/appender/honeybadger.rb +7 -1
- data/lib/semantic_logger/appender/honeybadger_insights.rb +8 -2
- data/lib/semantic_logger/appender/http.rb +308 -46
- data/lib/semantic_logger/appender/io.rb +11 -6
- data/lib/semantic_logger/appender/kafka.rb +204 -44
- data/lib/semantic_logger/appender/loki.rb +55 -9
- data/lib/semantic_logger/appender/mongodb.rb +109 -5
- data/lib/semantic_logger/appender/new_relic.rb +3 -4
- data/lib/semantic_logger/appender/new_relic_logs.rb +35 -15
- data/lib/semantic_logger/appender/open_telemetry.rb +37 -35
- data/lib/semantic_logger/appender/rabbitmq.rb +246 -17
- data/lib/semantic_logger/appender/sentry.rb +3 -4
- data/lib/semantic_logger/appender/sentry_ruby.rb +159 -18
- data/lib/semantic_logger/appender/splunk.rb +11 -2
- data/lib/semantic_logger/appender/splunk_http.rb +92 -1
- data/lib/semantic_logger/appender/syslog.rb +149 -32
- data/lib/semantic_logger/appender/tcp.rb +73 -98
- data/lib/semantic_logger/appender/text_output.rb +65 -0
- data/lib/semantic_logger/appender/udp.rb +140 -18
- data/lib/semantic_logger/appender/wrapper.rb +10 -7
- data/lib/semantic_logger/appender.rb +115 -10
- data/lib/semantic_logger/appenders.rb +145 -5
- data/lib/semantic_logger/backtrace_cleaning.rb +77 -0
- data/lib/semantic_logger/base.rb +115 -81
- data/lib/semantic_logger/call_site.rb +235 -0
- data/lib/semantic_logger/context.rb +222 -0
- data/lib/semantic_logger/core_ext/process.rb +20 -5
- data/lib/semantic_logger/core_ext/warning.rb +70 -0
- data/lib/semantic_logger/deprecation.rb +139 -0
- data/lib/semantic_logger/exception_details.rb +51 -0
- data/lib/semantic_logger/formatters/base.rb +186 -20
- data/lib/semantic_logger/formatters/color.rb +18 -3
- data/lib/semantic_logger/formatters/default.rb +53 -8
- data/lib/semantic_logger/formatters/ecs.rb +9 -4
- data/lib/semantic_logger/formatters/json.rb +9 -0
- data/lib/semantic_logger/formatters/logfmt.rb +155 -21
- data/lib/semantic_logger/formatters/loki/labels.rb +282 -0
- data/lib/semantic_logger/formatters/loki.rb +430 -38
- data/lib/semantic_logger/formatters/new_relic_logs.rb +15 -13
- data/lib/semantic_logger/formatters/one_line.rb +101 -3
- data/lib/semantic_logger/formatters/open_telemetry.rb +191 -35
- data/lib/semantic_logger/formatters/pattern.rb +68 -12
- data/lib/semantic_logger/formatters/raw.rb +40 -11
- data/lib/semantic_logger/formatters/signalfx.rb +55 -15
- data/lib/semantic_logger/formatters/syslog.rb +114 -20
- data/lib/semantic_logger/formatters/syslog_cee.rb +38 -19
- data/lib/semantic_logger/formatters/syslog_header.rb +49 -0
- data/lib/semantic_logger/formatters.rb +1 -0
- data/lib/semantic_logger/levels.rb +10 -1
- data/lib/semantic_logger/log.rb +384 -82
- data/lib/semantic_logger/loggable.rb +56 -73
- data/lib/semantic_logger/logger.rb +31 -5
- data/lib/semantic_logger/metric/new_relic.rb +33 -18
- data/lib/semantic_logger/metric/signalfx.rb +29 -18
- data/lib/semantic_logger/metric/statsd.rb +41 -15
- data/lib/semantic_logger/processor.rb +194 -5
- data/lib/semantic_logger/queue_processor/backoff.rb +140 -0
- data/lib/semantic_logger/queue_processor/batch_failure.rb +69 -0
- data/lib/semantic_logger/queue_processor/held_write.rb +62 -0
- data/lib/semantic_logger/queue_processor/inbox.rb +97 -0
- data/lib/semantic_logger/queue_processor/reopener.rb +56 -0
- data/lib/semantic_logger/queue_processor/reporter.rb +113 -0
- data/lib/semantic_logger/queue_processor/request.rb +93 -0
- data/lib/semantic_logger/queue_processor/throttled_counter.rb +89 -0
- data/lib/semantic_logger/queue_processor.rb +519 -170
- data/lib/semantic_logger/ractor_copy.rb +120 -0
- data/lib/semantic_logger/ractor_processor.rb +82 -0
- data/lib/semantic_logger/ractor_receiver.rb +282 -0
- data/lib/semantic_logger/ractor_support.rb +28 -0
- data/lib/semantic_logger/redactor.rb +310 -0
- data/lib/semantic_logger/semantic_logger.rb +328 -81
- data/lib/semantic_logger/subscriber.rb +250 -10
- data/lib/semantic_logger/sync_processor.rb +237 -26
- data/lib/semantic_logger/test/capture_log_events.rb +26 -5
- data/lib/semantic_logger/test/minitest.rb +2 -1
- data/lib/semantic_logger/test/rspec.rb +1 -1
- data/lib/semantic_logger/trap_safe.rb +113 -0
- data/lib/semantic_logger/utils.rb +482 -67
- data/lib/semantic_logger/version.rb +1 -1
- data/lib/semantic_logger.rb +36 -3
- metadata +41 -5
- data/lib/semantic_logger/core_ext/thread.rb +0 -30
data/lib/semantic_logger/base.rb
CHANGED
|
@@ -15,11 +15,11 @@ module SemanticLogger
|
|
|
15
15
|
# the log level in any logging instance or the default log level
|
|
16
16
|
# SemanticLogger.default_level
|
|
17
17
|
#
|
|
18
|
-
# Must be one of the values in SemanticLogger::LEVELS, or
|
|
19
|
-
#
|
|
18
|
+
# Must be one of the values in SemanticLogger::LEVELS, or nil to use this instance's fallback
|
|
19
|
+
# level (see #fallback_level).
|
|
20
20
|
def level=(level)
|
|
21
21
|
if level.nil?
|
|
22
|
-
# Use the
|
|
22
|
+
# Use the fallback level for this instance (see #fallback_level)
|
|
23
23
|
@level_index = nil
|
|
24
24
|
@level = nil
|
|
25
25
|
else
|
|
@@ -28,23 +28,27 @@ module SemanticLogger
|
|
|
28
28
|
end
|
|
29
29
|
end
|
|
30
30
|
|
|
31
|
-
# Returns the
|
|
32
|
-
#
|
|
31
|
+
# Returns the level set by an enclosing `#with_level` block on the current fiber,
|
|
32
|
+
# otherwise the level set for this instance, otherwise its fallback level (see #fallback_level).
|
|
33
33
|
def level
|
|
34
|
-
@
|
|
34
|
+
index = @with_level_used && with_level_index
|
|
35
|
+
return Levels.level(index) if index
|
|
36
|
+
|
|
37
|
+
@level || fallback_level
|
|
35
38
|
end
|
|
36
39
|
|
|
37
|
-
#
|
|
40
|
+
# Change the log level of this instance while the block runs, on the current fiber only.
|
|
38
41
|
#
|
|
39
|
-
#
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
42
|
+
# Other threads and fibers that share this instance keep logging at its own level.
|
|
43
|
+
# The level set with `#level=` is left untouched, so an instance that follows the
|
|
44
|
+
# global default level keeps following it after the block. Blocks can be nested.
|
|
45
|
+
#
|
|
46
|
+
# Matches Ruby's `Logger#with_level`, which Sidekiq calls to apply a job's `log_level`.
|
|
47
|
+
# Accepts the same levels as `#level=`. Returns the result of the block.
|
|
48
|
+
def with_level(level, &)
|
|
49
|
+
index = Levels.index(level)
|
|
50
|
+
@with_level_used = true
|
|
51
|
+
Context.with_level(self, index, &)
|
|
48
52
|
end
|
|
49
53
|
|
|
50
54
|
# Implement the log level calls
|
|
@@ -72,7 +76,7 @@ module SemanticLogger
|
|
|
72
76
|
# Examples:
|
|
73
77
|
# require 'semantic_logger'
|
|
74
78
|
#
|
|
75
|
-
# #
|
|
79
|
+
# # Log :info and above
|
|
76
80
|
# SemanticLogger.default_level = :info
|
|
77
81
|
#
|
|
78
82
|
# # Log to screen
|
|
@@ -143,18 +147,11 @@ module SemanticLogger
|
|
|
143
147
|
payload: nil,
|
|
144
148
|
metric: nil,
|
|
145
149
|
metric_amount: nil)
|
|
146
|
-
|
|
150
|
+
# The entry has the name, tags, and named tags of the thread whose backtrace it logs.
|
|
151
|
+
log = new_log(level, nil, thread)
|
|
147
152
|
return false unless meets_log_level?(log)
|
|
148
153
|
|
|
149
|
-
backtrace =
|
|
150
|
-
if thread == Thread.current
|
|
151
|
-
Utils.extract_backtrace(caller)
|
|
152
|
-
else
|
|
153
|
-
log.thread_name = thread.name
|
|
154
|
-
log.tags = (thread[:semantic_logger_tags] || []).clone
|
|
155
|
-
log.named_tags = (thread[:semantic_logger_named_tags] || {}).clone
|
|
156
|
-
thread.backtrace
|
|
157
|
-
end
|
|
154
|
+
backtrace = thread == Thread.current ? Utils.extract_backtrace(caller) : thread.backtrace
|
|
158
155
|
if backtrace
|
|
159
156
|
message += "\n"
|
|
160
157
|
message << backtrace.join("\n")
|
|
@@ -319,8 +316,12 @@ module SemanticLogger
|
|
|
319
316
|
# (empty) instance tags allocate no garbage. Child loggers replace them via #tagged_child.
|
|
320
317
|
@instance_tags = [].freeze
|
|
321
318
|
@instance_named_tags = {}.freeze
|
|
319
|
+
# Set the first time `#with_level` is called on this instance, so that instances that
|
|
320
|
+
# never use it skip the fiber-local lookup when checking their level. Only the fiber
|
|
321
|
+
# that sets an override needs to see the flag, and that fiber set it itself.
|
|
322
|
+
@with_level_used = false
|
|
322
323
|
if level.nil?
|
|
323
|
-
#
|
|
324
|
+
# Use the fallback level for this instance (see #fallback_level)
|
|
324
325
|
@level_index = nil
|
|
325
326
|
@level = nil
|
|
326
327
|
else
|
|
@@ -328,18 +329,59 @@ module SemanticLogger
|
|
|
328
329
|
end
|
|
329
330
|
end
|
|
330
331
|
|
|
331
|
-
# Return the level index for fast comparisons
|
|
332
|
-
# Returns the global default level index if the level has not been explicitly
|
|
333
|
-
# set for this instance
|
|
332
|
+
# Return the level index for fast comparisons, resolved in the same order as `#level`.
|
|
334
333
|
def level_index
|
|
335
|
-
@level_index ||
|
|
334
|
+
(@with_level_used && with_level_index) || @level_index || fallback_level_index
|
|
335
|
+
end
|
|
336
|
+
|
|
337
|
+
# Level to use when no level has been set for this instance: the global default level.
|
|
338
|
+
def fallback_level
|
|
339
|
+
SemanticLogger.default_level
|
|
340
|
+
end
|
|
341
|
+
|
|
342
|
+
# Level index to use when no level has been set for this instance. Unlike
|
|
343
|
+
# `#fallback_level`, it honors `SemanticLogger.silence`.
|
|
344
|
+
def fallback_level_index
|
|
345
|
+
SemanticLogger.default_level_index
|
|
346
|
+
end
|
|
347
|
+
|
|
348
|
+
# Level index set for this instance by an enclosing `#with_level` block on the current fiber.
|
|
349
|
+
def with_level_index
|
|
350
|
+
Context.with_level_index(self)
|
|
336
351
|
end
|
|
337
352
|
|
|
338
353
|
# Whether to log the supplied message based on the current filter if any
|
|
339
354
|
def filtered?(log)
|
|
340
355
|
return false if @filter.nil?
|
|
341
356
|
|
|
342
|
-
@filter.is_a?(Regexp) ? (
|
|
357
|
+
@filter.is_a?(Regexp) ? !name_matches_filter?(log.name) : @filter.call(log) != true
|
|
358
|
+
end
|
|
359
|
+
|
|
360
|
+
# Whether the name matches the Regexp filter. A name that is not valid in its encoding, or is
|
|
361
|
+
# not ASCII-compatible, which matching raises for, is matched converted to valid UTF-8 (see
|
|
362
|
+
# Utils.encode_utf8_string), since a log call must not raise.
|
|
363
|
+
def name_matches_filter?(name)
|
|
364
|
+
@filter.match?(name)
|
|
365
|
+
rescue ArgumentError, EncodingError
|
|
366
|
+
utf8_name_matches_filter?(Utils.encode_utf8_string(name.to_s))
|
|
367
|
+
end
|
|
368
|
+
|
|
369
|
+
# Whether the Regexp filter matches the name, which is valid UTF-8. A Regexp fixed to another
|
|
370
|
+
# encoding, such as one that holds characters outside ASCII written in a source file in
|
|
371
|
+
# ISO-8859-1, cannot match UTF-8 text, so it matches the name in its own encoding: as bytes for
|
|
372
|
+
# a binary Regexp, otherwise converted, with "?" for a character the encoding does not have. A
|
|
373
|
+
# name that it still cannot match, such as for an encoding with no converter from UTF-8, does
|
|
374
|
+
# not match.
|
|
375
|
+
def utf8_name_matches_filter?(name)
|
|
376
|
+
@filter.match?(name)
|
|
377
|
+
rescue ArgumentError, EncodingError
|
|
378
|
+
encoding = @filter.encoding
|
|
379
|
+
begin
|
|
380
|
+
text = encoding == Encoding::BINARY ? name.b : name.encode(encoding, invalid: :replace, undef: :replace)
|
|
381
|
+
@filter.match?(text)
|
|
382
|
+
rescue ArgumentError, EncodingError
|
|
383
|
+
false
|
|
384
|
+
end
|
|
343
385
|
end
|
|
344
386
|
|
|
345
387
|
# Ensure minimum log level is met
|
|
@@ -348,9 +390,9 @@ module SemanticLogger
|
|
|
348
390
|
end
|
|
349
391
|
|
|
350
392
|
# Build a new Log entry, layering this logger's instance tags on top of the
|
|
351
|
-
#
|
|
352
|
-
def new_log(level, index = nil)
|
|
353
|
-
apply_instance_tags(Log.new(name, level, index))
|
|
393
|
+
# context of the thread, by default the current one.
|
|
394
|
+
def new_log(level, index = nil, thread = Thread.current)
|
|
395
|
+
apply_instance_tags(Log.new(name, level, index, thread))
|
|
354
396
|
end
|
|
355
397
|
|
|
356
398
|
# Merge this logger's instance tags into the supplied Log entry.
|
|
@@ -403,7 +445,9 @@ module SemanticLogger
|
|
|
403
445
|
exception = payload
|
|
404
446
|
payload = nil
|
|
405
447
|
elsif payload && !payload.is_a?(Hash)
|
|
406
|
-
|
|
448
|
+
# Joined so that texts in incompatible encodings, such as a UTF-8 message and a binary
|
|
449
|
+
# response body, cannot make the log call raise (see Utils.join_texts).
|
|
450
|
+
message = message.nil? ? payload : Utils.join_texts(message, " -- ", payload)
|
|
407
451
|
payload = nil
|
|
408
452
|
end
|
|
409
453
|
|
|
@@ -433,17 +477,23 @@ module SemanticLogger
|
|
|
433
477
|
# arguments. Lambdas enforce their arity, so yielding the log to a zero-arity lambda would
|
|
434
478
|
# raise ArgumentError. Blocks that accept an argument still receive the log so they can read
|
|
435
479
|
# or mutate it.
|
|
480
|
+
#
|
|
481
|
+
# A String result is joined to the message with Utils.join_texts, so that texts in incompatible
|
|
482
|
+
# encodings, such as a UTF-8 message and a binary block result, cannot make the log call raise.
|
|
436
483
|
def apply_block_result(log, &block)
|
|
437
484
|
result = block.arity.zero? ? block.call : block.call(log)
|
|
438
485
|
case result
|
|
439
486
|
when String
|
|
440
|
-
log.message = log.message.nil? ? result :
|
|
487
|
+
log.message = log.message.nil? ? result : Utils.join_texts(log.message, " -- ", result)
|
|
441
488
|
when Hash
|
|
442
489
|
log.assign_hash(result)
|
|
443
490
|
end
|
|
444
491
|
end
|
|
445
492
|
|
|
446
493
|
# Measure the supplied block and log the message
|
|
494
|
+
#
|
|
495
|
+
# The options can hold untrusted data, so an option with a value that is not valid for it is
|
|
496
|
+
# ignored (see Log.argument?) and its default is used instead.
|
|
447
497
|
def measure_internal(level, index, message, params)
|
|
448
498
|
exception = nil
|
|
449
499
|
# Single parameter is a hash
|
|
@@ -454,9 +504,7 @@ module SemanticLogger
|
|
|
454
504
|
start = Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
|
455
505
|
begin
|
|
456
506
|
if block_given?
|
|
457
|
-
if (silence_level = params
|
|
458
|
-
# In case someone accidentally sets `silence: true` instead of `silence: :error`
|
|
459
|
-
silence_level = :error if silence_level == true
|
|
507
|
+
if (silence_level = measure_silence_level(params))
|
|
460
508
|
silence(silence_level) { yield(params) }
|
|
461
509
|
else
|
|
462
510
|
yield(params)
|
|
@@ -467,13 +515,15 @@ module SemanticLogger
|
|
|
467
515
|
ensure
|
|
468
516
|
# Must use ensure block otherwise a `return` in the yield above will skip the log entry
|
|
469
517
|
log = new_log(level, index)
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
518
|
+
# Only an Exception, since it is re-raised below
|
|
519
|
+
exception ||= params[:exception] if params[:exception].is_a?(Exception)
|
|
520
|
+
message = measure_option(params, :message) || message
|
|
521
|
+
duration =
|
|
473
522
|
if block_given?
|
|
474
523
|
1_000.0 * (Process.clock_gettime(Process::CLOCK_MONOTONIC) - start)
|
|
475
524
|
else
|
|
476
|
-
params
|
|
525
|
+
measure_option(params, :duration) ||
|
|
526
|
+
raise("Mandatory block missing when :duration option is not supplied")
|
|
477
527
|
end
|
|
478
528
|
|
|
479
529
|
# Extract options after block completes so that block can modify any of the options
|
|
@@ -483,13 +533,15 @@ module SemanticLogger
|
|
|
483
533
|
should_log = log.assign(
|
|
484
534
|
message: message,
|
|
485
535
|
payload: payload,
|
|
486
|
-
min_duration: params
|
|
536
|
+
min_duration: measure_option(params, :min_duration) || 0.0,
|
|
487
537
|
exception: exception,
|
|
488
|
-
metric: params
|
|
489
|
-
metric_amount: params
|
|
538
|
+
metric: measure_option(params, :metric),
|
|
539
|
+
metric_amount: measure_option(params, :metric_amount),
|
|
540
|
+
dimensions: measure_option(params, :dimensions),
|
|
490
541
|
duration: duration,
|
|
491
|
-
log_exception: params
|
|
492
|
-
on_exception_level: params
|
|
542
|
+
log_exception: measure_option(params, :log_exception) || :partial,
|
|
543
|
+
on_exception_level: measure_option(params, :on_exception_level),
|
|
544
|
+
call_site: measure_option(params, :call_site)
|
|
493
545
|
)
|
|
494
546
|
|
|
495
547
|
# Log level may change during assign due to :on_exception_level
|
|
@@ -498,38 +550,20 @@ module SemanticLogger
|
|
|
498
550
|
end
|
|
499
551
|
end
|
|
500
552
|
|
|
501
|
-
#
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
log_exception:,
|
|
508
|
-
on_exception_level:)
|
|
509
|
-
# Ignores filter, silence, payload
|
|
510
|
-
exception = nil
|
|
511
|
-
start = Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
|
512
|
-
begin
|
|
513
|
-
yield
|
|
514
|
-
rescue Exception => e
|
|
515
|
-
exception = e
|
|
516
|
-
ensure
|
|
517
|
-
log = new_log(level, index)
|
|
518
|
-
# May return false due to elastic logging
|
|
519
|
-
should_log = log.assign(
|
|
520
|
-
message: message,
|
|
521
|
-
min_duration: min_duration,
|
|
522
|
-
exception: exception,
|
|
523
|
-
metric: metric,
|
|
524
|
-
duration: 1_000.0 * (Process.clock_gettime(Process::CLOCK_MONOTONIC) - start),
|
|
525
|
-
log_exception: log_exception,
|
|
526
|
-
on_exception_level: on_exception_level
|
|
527
|
-
)
|
|
553
|
+
# Returns the value of the measure option that is also a log argument, or nil when it is not
|
|
554
|
+
# supplied or its value is not valid for that argument.
|
|
555
|
+
def measure_option(params, key)
|
|
556
|
+
value = params[key]
|
|
557
|
+
value if Log.argument?(key, value)
|
|
558
|
+
end
|
|
528
559
|
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
560
|
+
# Returns the level to silence other log messages to within a measure block, or nil when none
|
|
561
|
+
# is supplied or it is not a valid level.
|
|
562
|
+
def measure_silence_level(params)
|
|
563
|
+
silence_level = params[:silence]
|
|
564
|
+
# In case someone accidentally sets `silence: true` instead of `silence: :error`
|
|
565
|
+
silence_level = :error if silence_level == true
|
|
566
|
+
silence_level if Levels.valid?(silence_level)
|
|
533
567
|
end
|
|
534
568
|
end
|
|
535
569
|
end
|
|
@@ -0,0 +1,235 @@
|
|
|
1
|
+
module SemanticLogger
|
|
2
|
+
# Where a log entry was logged: the path and line of the code that made the log call, and the
|
|
3
|
+
# label of its frame, usually the method, such as "Order#submit".
|
|
4
|
+
#
|
|
5
|
+
# Log#assign sets it when the entry's level is at or above SemanticLogger.call_site_level. When the
|
|
6
|
+
# log call supplies a backtrace, the call site is its first line. Otherwise it is the first frame of
|
|
7
|
+
# the calling thread's stack outside Semantic Logger and SemanticLogger.call_site_skip_paths, which
|
|
8
|
+
# costs far less than capturing a backtrace, since only the frames up to it are read.
|
|
9
|
+
#
|
|
10
|
+
# A call site is frozen and holds only frozen Strings and an Integer, so that a log entry logged in
|
|
11
|
+
# another Ractor can be sent to the main one.
|
|
12
|
+
#
|
|
13
|
+
# Attributes:
|
|
14
|
+
# path: [String]
|
|
15
|
+
# The file, as in a backtrace, such as "/app/models/order.rb".
|
|
16
|
+
# line: [Integer]
|
|
17
|
+
# The line number in the file.
|
|
18
|
+
# label: [String|nil]
|
|
19
|
+
# The label of the frame, as in a backtrace. On Ruby 3.4 and later it includes the class, such
|
|
20
|
+
# as "Order#submit" or "Order.find_open", on Ruby 3.2 and 3.3 only the method, such as "submit".
|
|
21
|
+
# A block's label starts with "block in", and code outside a method has a label such as
|
|
22
|
+
# "<main>" or "<class:Order>".
|
|
23
|
+
CallSite = Data.define(:path, :line, :label)
|
|
24
|
+
|
|
25
|
+
class CallSite
|
|
26
|
+
# The number of frames read from the stack at a time. Semantic Logger's own frames, which are
|
|
27
|
+
# skipped, are usually fewer than this.
|
|
28
|
+
WINDOW = 8
|
|
29
|
+
private_constant :WINDOW
|
|
30
|
+
|
|
31
|
+
# Paths of the frames of Ruby's own methods written in Ruby, such as "<internal:kernel>", which are
|
|
32
|
+
# never the code that logged the entry. Which methods these are depends on the version of Ruby.
|
|
33
|
+
INTERNAL_PATH = "<internal:".freeze
|
|
34
|
+
private_constant :INTERNAL_PATH
|
|
35
|
+
|
|
36
|
+
# Labels of Ruby's methods that call a method for their caller, such as
|
|
37
|
+
# `logger.public_send(level, message)`, and add a frame of their own with the caller's path and
|
|
38
|
+
# line.
|
|
39
|
+
#
|
|
40
|
+
# CRuby 3.4 and later include the class in the label, which tells these methods apart from an
|
|
41
|
+
# application's own methods of the same name. JRuby, and CRuby before 3.4, do not, so there an
|
|
42
|
+
# application's own `call` method is also skipped when it was called from the same line of the
|
|
43
|
+
# same file, such as by recursion or a one-line definition. The path and line stay the same; only
|
|
44
|
+
# the label becomes the caller's. JRuby labels the frame "block in public_send" when it is called
|
|
45
|
+
# in a block. "block in call" is left out, since it is also the label of a block in an
|
|
46
|
+
# application's own `call` method.
|
|
47
|
+
DISPATCH_LABELS = Utils.make_shareable(
|
|
48
|
+
if RUBY_ENGINE == "ruby" && RUBY_VERSION >= "3.4"
|
|
49
|
+
%w[Kernel#public_send Method#call UnboundMethod#bind_call].freeze
|
|
50
|
+
else
|
|
51
|
+
["public_send", "call", "bind_call", "block in public_send", "block in bind_call"].freeze
|
|
52
|
+
end
|
|
53
|
+
)
|
|
54
|
+
private_constant :DISPATCH_LABELS
|
|
55
|
+
|
|
56
|
+
# The line number and the start of the label in a backtrace line, such as ":12:in 'Order#submit'",
|
|
57
|
+
# or as Ruby 3.3 and earlier write it, ":12:in `submit'". Without a leading `.*`, so that matching an
|
|
58
|
+
# untrusted line takes linear time on every Ruby, including JRuby, which has no backtracking limits.
|
|
59
|
+
# A line number has at most 10 digits, since a longer one, which only a forged line has, would be
|
|
60
|
+
# slow to convert to an Integer and back.
|
|
61
|
+
LINE_AND_LABEL = /:(\d{1,10}):in [`']/
|
|
62
|
+
private_constant :LINE_AND_LABEL
|
|
63
|
+
|
|
64
|
+
# A backtrace line without a label, such as "app.rb:12".
|
|
65
|
+
UNLABELED_LINE = /\A(.+):(\d{1,10})(?!\d)/
|
|
66
|
+
private_constant :UNLABELED_LINE
|
|
67
|
+
|
|
68
|
+
# A line break, which only a forged backtrace line holds.
|
|
69
|
+
LINE_BREAK = "\n".freeze
|
|
70
|
+
private_constant :LINE_BREAK
|
|
71
|
+
|
|
72
|
+
# A label's closing quote.
|
|
73
|
+
QUOTE = "'".freeze
|
|
74
|
+
private_constant :QUOTE
|
|
75
|
+
|
|
76
|
+
# Returns [CallSite|nil] where a log entry was logged, from what the log call supplied:
|
|
77
|
+
# call_site: [CallSite|Thread::Backtrace::Location|false|nil]
|
|
78
|
+
# The call site, or the frame it is made from. false: the entry has no call site.
|
|
79
|
+
# backtrace: [Array<String>|nil]
|
|
80
|
+
# Without a call site, its first line is the call site.
|
|
81
|
+
# Without either (or with an empty backtrace), the first frame of the calling thread's stack
|
|
82
|
+
# outside Semantic Logger (see .capture).
|
|
83
|
+
def self.locate(call_site: nil, backtrace: nil)
|
|
84
|
+
return if false.equal?(call_site)
|
|
85
|
+
return from(call_site) if call_site
|
|
86
|
+
|
|
87
|
+
backtrace.nil? || backtrace.empty? ? capture : parse(backtrace.first)
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
# Returns [CallSite] the call site, or the call site of a Thread::Backtrace::Location. A call site
|
|
91
|
+
# whose Strings are frozen is returned as it is; otherwise its Strings are copied, frozen and
|
|
92
|
+
# deduplicated, since the same few paths and labels are logged again and again.
|
|
93
|
+
def self.from(call_site)
|
|
94
|
+
is_call_site = CallSite === call_site # rubocop:disable Style/CaseEquality
|
|
95
|
+
return call_site if is_call_site && call_site.path.frozen? && (call_site.label.nil? || call_site.label.frozen?)
|
|
96
|
+
|
|
97
|
+
label = call_site.label
|
|
98
|
+
new(frozen_copy(call_site.path), is_call_site ? call_site.line : call_site.lineno, label && frozen_copy(label))
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
# Returns [String] a frozen, deduplicated copy of the String, or the String when it is already one.
|
|
102
|
+
# A subclass of String is copied to a String first, since JRuby freezes it in place instead.
|
|
103
|
+
def self.frozen_copy(string)
|
|
104
|
+
string.instance_of?(String) ? -string : -String.new(string)
|
|
105
|
+
end
|
|
106
|
+
private_class_method :frozen_copy
|
|
107
|
+
|
|
108
|
+
# Whether the value can be supplied as a log entry's call site (see .locate). A value that parsed
|
|
109
|
+
# data, such as JSON, can hold is not, other than false, so that untrusted data cannot set the
|
|
110
|
+
# location of an entry. Never calls a method of the value, which can be any object in a payload.
|
|
111
|
+
def self.supplied?(value)
|
|
112
|
+
return true if false.equal?(value) || ::Thread::Backtrace::Location === value # rubocop:disable Style/CaseEquality
|
|
113
|
+
|
|
114
|
+
CallSite === value && value.well_formed? # rubocop:disable Style/CaseEquality
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
# Returns [CallSite|nil] the first frame of the calling thread's stack outside Semantic Logger, Ruby's
|
|
118
|
+
# own methods, and SemanticLogger.call_site_skip_paths, and other than the frame of a method such
|
|
119
|
+
# as public_send that called the logger for it. nil when every frame is skipped.
|
|
120
|
+
def self.capture
|
|
121
|
+
skip_paths = SemanticLogger.call_site_skip_paths
|
|
122
|
+
# A Ractor other than the main one cannot use the main Ractor's cache.
|
|
123
|
+
cache = RactorSupport.other_ractor? ? nil : skip_cache(skip_paths)
|
|
124
|
+
start = 1
|
|
125
|
+
# One frame more than the window, which is only read as the caller of the frame before it.
|
|
126
|
+
# caller_locations returns nil, or on JRuby an empty Array, once start is past the end of the stack.
|
|
127
|
+
while (locations = caller_locations(start, WINDOW + 1)) && !locations.empty?
|
|
128
|
+
locations.each_with_index do |location, index|
|
|
129
|
+
break if index == WINDOW
|
|
130
|
+
|
|
131
|
+
path = location.path
|
|
132
|
+
next if skip?(path, skip_paths, cache)
|
|
133
|
+
|
|
134
|
+
label = location.label
|
|
135
|
+
next if dispatch?(location, label, locations[index + 1])
|
|
136
|
+
|
|
137
|
+
return new(-path, location.lineno, label && -label)
|
|
138
|
+
end
|
|
139
|
+
start += WINDOW
|
|
140
|
+
end
|
|
141
|
+
end
|
|
142
|
+
|
|
143
|
+
# Returns [CallSite|nil] the call site in a line of a backtrace, such as
|
|
144
|
+
# "app.rb:12:in 'Order#submit'". nil when the line is not a String with a path and line number,
|
|
145
|
+
# or is not text that Ruby can match, such as invalid UTF-8.
|
|
146
|
+
def self.parse(line)
|
|
147
|
+
return unless line.is_a?(String) && line.encoding.ascii_compatible? && line.valid_encoding?
|
|
148
|
+
return if line.include?(LINE_BREAK)
|
|
149
|
+
|
|
150
|
+
# The first ":12:in '" is the end of the path, since a label can hold anything. The label's
|
|
151
|
+
# closing quote must follow its opening one.
|
|
152
|
+
match = LINE_AND_LABEL.match(line) if line.end_with?(QUOTE)
|
|
153
|
+
if match&.begin(0)&.positive? && match.end(0) < line.size
|
|
154
|
+
return new(-match.pre_match, match[1].to_i, -line[match.end(0)...-1])
|
|
155
|
+
end
|
|
156
|
+
|
|
157
|
+
match = UNLABELED_LINE.match(line)
|
|
158
|
+
new(-match[1], match[2].to_i, nil) if match
|
|
159
|
+
end
|
|
160
|
+
|
|
161
|
+
# Whether a frame in the file at this path is skipped when capturing a call site.
|
|
162
|
+
#
|
|
163
|
+
# The frames of log calls come from a few files, Semantic Logger's own and the application's, so
|
|
164
|
+
# the answer is remembered in the cache for each path, which is faster than matching the path
|
|
165
|
+
# again. Without a cache, the path is matched each time.
|
|
166
|
+
#
|
|
167
|
+
# In a signal trap, the answer for a path not yet in the cache is not remembered, since writing to
|
|
168
|
+
# the cache locks a Mutex on CRuby, which Ruby does not allow there.
|
|
169
|
+
def self.skip?(path, skip_paths, cache)
|
|
170
|
+
return skip_path?(path, skip_paths) unless cache
|
|
171
|
+
|
|
172
|
+
skipped = cache[path]
|
|
173
|
+
return skipped unless skipped.nil?
|
|
174
|
+
|
|
175
|
+
skipped = skip_path?(path, skip_paths)
|
|
176
|
+
return skipped if TrapSafe.in_trap?
|
|
177
|
+
|
|
178
|
+
# Bounded, since code evaluated at runtime can have a path of its own each time.
|
|
179
|
+
cache.clear if cache.size >= MAX_CACHED_PATHS
|
|
180
|
+
cache[path] = skipped
|
|
181
|
+
end
|
|
182
|
+
private_class_method :skip?
|
|
183
|
+
|
|
184
|
+
# The most paths whose answer .skip? remembers.
|
|
185
|
+
MAX_CACHED_PATHS = 1_000
|
|
186
|
+
private_constant :MAX_CACHED_PATHS
|
|
187
|
+
|
|
188
|
+
# Returns [Concurrent::Map] whether each path is skipped, for these skip paths. A new one when the
|
|
189
|
+
# skip paths have changed.
|
|
190
|
+
def self.skip_cache(skip_paths)
|
|
191
|
+
cache = @skip_cache
|
|
192
|
+
return cache.last if cache&.first.equal?(skip_paths)
|
|
193
|
+
|
|
194
|
+
(@skip_cache = [skip_paths, Concurrent::Map.new]).last
|
|
195
|
+
end
|
|
196
|
+
private_class_method :skip_cache
|
|
197
|
+
|
|
198
|
+
# Whether the frame is that of a method such as public_send, called by the frame after it, which
|
|
199
|
+
# has the same path and line. Then the frame after it made the log call.
|
|
200
|
+
def self.dispatch?(location, label, caller_location)
|
|
201
|
+
DISPATCH_LABELS.include?(label) && !caller_location.nil? &&
|
|
202
|
+
caller_location.lineno == location.lineno && caller_location.path == location.path
|
|
203
|
+
end
|
|
204
|
+
private_class_method :dispatch?
|
|
205
|
+
|
|
206
|
+
def self.skip_path?(path, skip_paths)
|
|
207
|
+
return true if path.start_with?(INTERNAL_PATH) || Utils.extract_path?(path)
|
|
208
|
+
|
|
209
|
+
skip_paths.any? { |skip_path| path.include?(skip_path) }
|
|
210
|
+
end
|
|
211
|
+
private_class_method :skip_path?
|
|
212
|
+
|
|
213
|
+
# Whether the path is a String, the line an Integer, and the label a String or nil, as the
|
|
214
|
+
# formatters need. A call site that Semantic Logger makes always is; one that a log call supplies
|
|
215
|
+
# is checked, and otherwise logged as part of the payload, like any other value that is not valid
|
|
216
|
+
# for its argument.
|
|
217
|
+
#
|
|
218
|
+
# Like .supplied?, calls no method of the values, which can be any objects.
|
|
219
|
+
def well_formed?
|
|
220
|
+
String === path && Integer === line && (nil.equal?(label) || String === label) # rubocop:disable Style/CaseEquality
|
|
221
|
+
end
|
|
222
|
+
|
|
223
|
+
# Returns [Array<String, Integer>] the file name and line number of the call site. With short_name
|
|
224
|
+
# the file name has no directory, such as "order.rb".
|
|
225
|
+
def file_name_and_line(short_name: false)
|
|
226
|
+
[short_name ? File.basename(path) : path, line]
|
|
227
|
+
end
|
|
228
|
+
|
|
229
|
+
# Returns [String] the call site as Ruby 3.4 and later write it in a backtrace, such as
|
|
230
|
+
# "/app/models/order.rb:42:in 'Order#submit'".
|
|
231
|
+
def to_s
|
|
232
|
+
label ? "#{path}:#{line}:in '#{label}'" : "#{path}:#{line}"
|
|
233
|
+
end
|
|
234
|
+
end
|
|
235
|
+
end
|