Class: SemanticLogger::Formatters::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/semantic_logger/formatters/base.rb

Direct Known Subclasses

Default, Loki, OpenTelemetry, Signalfx

Constant Summary collapse

CONTROL_CHAR_ESCAPES =

Printable escapes for the most common control characters. Any other ASCII control character is escaped to its hexadecimal \xHH form by #escape_control_characters, and a Unicode one (see UNICODE_CONTROL_CHARS) to its \uXXXX form.

Utils.make_shareable(
  {
    "\n" => "\\n",
    "\r" => "\\r",
    "\t" => "\\t",
    "\e" => "\\e"
  }.freeze
)
CONTROL_CHARS =

Matches C0 control characters (including newlines and the ANSI escape) and DEL.

/[\x00-\x1f\x7f]/
UNICODE_CONTROL_CHARS =

Matches the characters of CONTROL_CHARS, and the Unicode characters that control how text is shown: the C1 control characters (U+0080 to U+009F, including the next line U+0085), which some terminals act on, the line and paragraph separators (U+2028, U+2029), at which editors, browsers, and some log viewers start a new line, and the bidirectional controls (U+202A to U+202E, U+2066 to U+2069), which can make logged text read in a different order than it was written. Escaped in the \uXXXX form.

/[\x00-\x1f\x7f\u0080-\u009f\u2028\u2029\u202a-\u202e\u2066-\u2069]/
PRECISION =

Time precision varies by Ruby interpreter JRuby 9.1.8.0 supports microseconds

if defined?(JRuby)
  if JRUBY_VERSION.to_f >= 9.1
    maint = JRUBY_VERSION.match(/\A\d+\.\d+\.(\d+)\./)[1].to_i
    (maint >= 8) || (JRUBY_VERSION.to_f > 9.1) ? 6 : 3
  else
    3
  end
else
  6
end

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(time_format: nil, log_host: true, log_application: true, log_environment: true, precision: PRECISION, escape_control_chars: false, log_exception_details: default_log_exception_details, backtrace_cleaner: default_backtrace_cleaner) ⇒ Base

Parameters time_format: [String|Symbol|nil] See Time#strftime for the format of this string. :iso_8601 Outputs an ISO8601 Formatted timestamp. :ms Output in miliseconds since epoch. :notime Returns an empty string for time ( no time is output ). Default: '%Y-%m-%d %H:%M:%S.%N' log_host: [Boolean] Whether or not to include hostname in logs Default: true log_application: [Boolean] Whether or not to include application name in logs Default: true precision: [Integer] How many fractional digits to log times with. Default: PRECISION (6, except on older JRuby, where 3) escape_control_chars: [Boolean] Replace control characters (newlines, the ANSI escape, etc., and the Unicode characters that control how text is shown, such as U+2028 and U+202E) in untrusted log data (logger name, thread name, message, payload, tags, named tags, and the message and backtrace lines of the exception and of each exception that caused it) with a printable escaped form, e.g. "\n". This prevents log forging and terminal escape-sequence injection when logging data that may contain attacker-controlled content. Note: Has no effect on structured formatters such as :json, which already escape control characters via JSON encoding, except that :loki and :open_telemetry, which write the exception's stack trace as text, escape its backtrace lines and the messages of its causes. Default: false (preserve newlines and ANSI colors in text output) log_exception_details: [true|false|:with_source] Whether to log the detail that Ruby's Exception#detailed_message adds to an exception's message, such as did_you_mean suggestions ("Did you mean? upcase") or the detail an exception class supplies by overriding #detailed_message. :with_source also logs error_highlight's snippet of the failing source line. It is about 100 times slower, since it re-parses that source file, and it copies source code into the log. On Ruby 3.2 a NameError or NoMethodError builds its message from receiver.inspect each time it is asked, and logging the details asks for it twice more, so turn this off when inspecting the receivers of logged exceptions is slow. Default: true for the Default, Color, and Pattern text formatters, false otherwise. backtrace_cleaner: [true|false|Object] Cleans the backtrace of the logged exception, and of each exception that caused it, before it is written, leaving out or shortening lines. Only this formatter's output changes: the exceptions are not. true: Use the application's cleaner, SemanticLogger.backtrace_cleaner, when one is set. false: Write the full backtrace. Or a cleaner of this formatter's own, which responds to #clean or #call (see SemanticLogger.backtrace_cleaner=). Default: true for the text formatters (Default and those built on it) and the Json formatters, false otherwise.



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/semantic_logger/formatters/base.rb', line 96

def initialize(time_format: nil,
               log_host: true,
               log_application: true,
               log_environment: true,
               precision: PRECISION,
               escape_control_chars: false,
               log_exception_details: default_log_exception_details,
               backtrace_cleaner: default_backtrace_cleaner)
  self.time_format      = time_format || self.class.build_time_format(precision)
  @log_host             = log_host
  @log_application      = log_application
  @log_environment      = log_environment
  @precision            = precision
  @escape_control_chars  = escape_control_chars
  @log_exception_details = log_exception_details
  self.backtrace_cleaner = backtrace_cleaner
end

Instance Attribute Details

#backtrace_cleaner ⇒ Object

Returns [true|false|Object] the backtrace_cleaner option. See #initialize.



127
128
129
# File 'lib/semantic_logger/formatters/base.rb', line 127

def backtrace_cleaner
  @backtrace_cleaner
end

#escape_control_chars ⇒ Object

Returns the value of attribute escape_control_chars.



5
6
7
# File 'lib/semantic_logger/formatters/base.rb', line 5

def escape_control_chars
  @escape_control_chars
end

#log ⇒ Object

Returns the value of attribute log.



5
6
7
# File 'lib/semantic_logger/formatters/base.rb', line 5

def log
  @log
end

#log_application ⇒ Object

Returns the value of attribute log_application.



5
6
7
# File 'lib/semantic_logger/formatters/base.rb', line 5

def log_application
  @log_application
end

#log_environment ⇒ Object

Returns the value of attribute log_environment.



5
6
7
# File 'lib/semantic_logger/formatters/base.rb', line 5

def log_environment
  @log_environment
end

#log_exception_details ⇒ Object

Returns the value of attribute log_exception_details.



5
6
7
# File 'lib/semantic_logger/formatters/base.rb', line 5

def log_exception_details
  @log_exception_details
end

#log_host ⇒ Object

Returns the value of attribute log_host.



5
6
7
# File 'lib/semantic_logger/formatters/base.rb', line 5

def log_host
  @log_host
end

#logger ⇒ Object

Returns the value of attribute logger.



5
6
7
# File 'lib/semantic_logger/formatters/base.rb', line 5

def logger
  @logger
end

#precision ⇒ Object

Returns the value of attribute precision.



5
6
7
# File 'lib/semantic_logger/formatters/base.rb', line 5

def precision
  @precision
end

#time_format ⇒ Object

Returns [String|Symbol] the time_format option. See #initialize.



115
116
117
# File 'lib/semantic_logger/formatters/base.rb', line 115

def time_format
  @time_format
end

Class Method Details

.build_time_format(precision = PRECISION) ⇒ Object

Return default time format string

Parameters precision: [Integer] How many fractional digits to log times with. Default: PRECISION (6, except on older JRuby, where 3)



148
149
150
# File 'lib/semantic_logger/formatters/base.rb', line 148

def self.build_time_format(precision = PRECISION)
  "%Y-%m-%d %H:%M:%S.%#{precision}N"
end

Instance Method Details

#pid ⇒ Object

Process ID



158
159
160
# File 'lib/semantic_logger/formatters/base.rb', line 158

def pid
  $$
end

#time ⇒ Object

Date & time



153
154
155
# File 'lib/semantic_logger/formatters/base.rb', line 153

def time
  format_time(log.time) if time_format
end