Class: SemanticLogger::Formatters::Base

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

Direct Known Subclasses

Default, Loki, Signalfx

Constant Summary collapse

CONTROL_CHAR_ESCAPES =

Printable escapes for the most common control characters. Any other control character is escaped to its hexadecimal \xHH form by #escape_control_characters.

{
  "\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]/
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) ⇒ 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.) in untrusted log data (message, tags, named tags, and exception message) 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. Default: false (preserve newlines and ANSI colors in text output)



60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/semantic_logger/formatters/base.rb', line 60

def initialize(time_format: nil,
               log_host: true,
               log_application: true,
               log_environment: true,
               precision: PRECISION,
               escape_control_chars: false)
  @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
end

Instance Attribute Details

#escape_control_charsObject

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

#logObject

Returns the value of attribute log.



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

def log
  @log
end

#log_applicationObject

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_environmentObject

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_hostObject

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

#loggerObject

Returns the value of attribute logger.



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

def logger
  @logger
end

#precisionObject

Returns the value of attribute precision.



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

def precision
  @precision
end

#time_formatObject

Returns the value of attribute time_format.



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

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)



80
81
82
# File 'lib/semantic_logger/formatters/base.rb', line 80

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

Instance Method Details

#pidObject

Process ID



90
91
92
# File 'lib/semantic_logger/formatters/base.rb', line 90

def pid
  $$
end

#timeObject

Date & time



85
86
87
# File 'lib/semantic_logger/formatters/base.rb', line 85

def time
  format_time(log.time) if time_format
end