Module: SemanticLogger::BacktraceCleaning

Defined in:
lib/semantic_logger/backtrace_cleaning.rb

Overview

Asks an application's backtrace cleaner, such as Rails.backtrace_cleaner, to clean a backtrace.

A backtrace cleaner is any object that responds to #clean, as ActiveSupport::BacktraceCleaner does, or to #call, such as a Proc. It is given the backtrace as an Array of Strings, and returns the lines to log in its place: it can leave lines out, and shorten or rewrite them.

Internal: Log caches the result for each of its exceptions; see Log#backtrace_of.

Class Method Summary collapse

Class Method Details

.clean(backtrace, cleaner) ⇒ Object

Returns [Array] the backtrace as the cleaner cleans it, or nil when there is none. The Array and its lines are frozen copies, so that every appender can share them.

Returns a copy of the supplied backtrace instead when the cleaner leaves out every line, so that the only clue to where an exception came from is never lost, for example an exception raised entirely within gems that a cleaner silences. Also when the cleaner does not return an Array of lines, or raises a StandardError, such as for an object that is not a cleaner, so that a cleaner cannot stop a log entry being written. The error is reported to the internal logger, since nothing else would show that the cleaner is broken.

The cleaner is given a copy of the backtrace, which it may change, since the supplied one belongs to the exception, and other appenders, such as error services, also read it.



36
37
38
39
40
# File 'lib/semantic_logger/backtrace_cleaning.rb', line 36

def self.clean(backtrace, cleaner)
  return if backtrace.nil?

  cleaned_lines(backtrace, cleaner) || frozen_copy(backtrace)
end

.cleaner?(object) ⇒ Boolean

Returns [true|false] whether the object can clean a backtrace.

Returns:

  • (Boolean)


11
12
13
# File 'lib/semantic_logger/backtrace_cleaning.rb', line 11

def self.cleaner?(object)
  object.respond_to?(:clean) || object.respond_to?(:call)
end

.validate!(cleaner, expected: "a backtrace cleaner") ⇒ Object

Returns the cleaner, for a setting that holds one. Raises ArgumentError when it cannot clean a backtrace (see .cleaner?), naming what the setting expects, since it can accept other values too.

Raises:

  • (ArgumentError)


18
19
20
21
22
# File 'lib/semantic_logger/backtrace_cleaning.rb', line 18

def self.validate!(cleaner, expected: "a backtrace cleaner")
  return cleaner if cleaner?(cleaner)

  raise(ArgumentError, "Expected #{expected} that responds to #clean or #call, not #{cleaner.inspect}")
end