class SemanticLogger::Formatters::Base

Constants

PRECISION

Time precision varies by Ruby interpreter JRuby 9.1.8.0 supports microseconds

Attributes

log[RW]
log_application[RW]
log_environment[RW]
log_host[RW]
logger[RW]
precision[RW]
time_format[RW]

Public Class Methods

build_time_format(precision = PRECISION) click to toggle source

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)
# File lib/semantic_logger/formatters/base.rb, line 55
def self.build_time_format(precision = PRECISION)
  "%Y-%m-%d %H:%M:%S.%#{precision}N"
end
new(time_format: nil, log_host: true, log_application: true, log_environment: true, precision: PRECISION) click to toggle source

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.
  nil:      Returns Empty string for time ( no time is output ).
  Default: '%Y-%m-%d %H:%M:%S.%<precision>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)
# File lib/semantic_logger/formatters/base.rb, line 37
def initialize(time_format: nil,
               log_host: true,
               log_application: true,
               log_environment: true,
               precision: PRECISION)
  @time_format     = time_format || self.class.build_time_format(precision)
  @log_host        = log_host
  @log_application = log_application
  @log_environment = log_environment
  @precision       = precision
end

Public Instance Methods

pid() click to toggle source

Process ID

# File lib/semantic_logger/formatters/base.rb, line 65
def pid
  $$
end
time() click to toggle source

Date & time

# File lib/semantic_logger/formatters/base.rb, line 60
def time
  format_time(log.time) if time_format
end

Private Instance Methods

format_time(time) click to toggle source

Return the Time as a formatted string

# File lib/semantic_logger/formatters/base.rb, line 72
def format_time(time)
  case time_format
  when :rfc_3339
    time.utc.to_datetime.rfc3339
  when :iso_8601
    time.utc.iso8601(precision)
  when :ms
    (time.to_f * 1_000).to_i
  when :none
    time
  when :seconds
    time.to_f
  when nil
    ""
  else
    time.strftime(time_format)
  end
end