class SemanticLoggerJournald::Formatter

Constants

KEY_JOIN
LEVEL_MAP

Public Class Methods

new(flatten_payload: KEY_JOIN, time_format: nil, **args) click to toggle source
Calls superclass method
# File lib/semantic_logger_journald/formatter.rb, line 14
def initialize(flatten_payload: KEY_JOIN, time_format: nil, **args)
  @flatten_payload = flatten_payload
  super(time_format: time_format, **args)
end

Public Instance Methods

level() click to toggle source
# File lib/semantic_logger_journald/formatter.rb, line 19
def level
  hash[:priority] = LEVEL_MAP.fetch(log.level)
end
named_tags() click to toggle source
# File lib/semantic_logger_journald/formatter.rb, line 36
def named_tags
  hash.merge!(log.named_tags) if log.named_tags&.any?
end
payload() click to toggle source
# File lib/semantic_logger_journald/formatter.rb, line 40
def payload
  return if !log.payload || log.payload.empty?

  payload = log.payload
  payload = flatten_hash(payload) if flatten_payload?
  hash.merge!(payload)
end
pid() click to toggle source

It's already logged by journald as _PID

# File lib/semantic_logger_journald/formatter.rb, line 28
def pid
  nil
end
tags() click to toggle source
# File lib/semantic_logger_journald/formatter.rb, line 32
def tags
  hash.merge!(log.tags.map { |t| [t, 1] }.to_h) if log.tags&.any?
end
time() click to toggle source
# File lib/semantic_logger_journald/formatter.rb, line 23
def time
  hash[:syslog_timestamp] = time_format ? format_time(log.time) : format_syslog_time(log.time)
end

Private Instance Methods

flatten_hash(nested_hash) click to toggle source
# File lib/semantic_logger_journald/formatter.rb, line 54
def flatten_hash(nested_hash)
  nested_hash.each_with_object({}) do |(key, val), h|
    if val.is_a?(Hash)
      flatten_hash(val).map { |nested_key, nested_val|
        h[join_keys(key, nested_key)] = nested_val
      }
    else
      h[key] = val
    end
  end
end
flatten_payload?() click to toggle source
# File lib/semantic_logger_journald/formatter.rb, line 50
def flatten_payload?
  log.payload&.any? && @flatten_payload
end
format_syslog_time(time) click to toggle source

from syslog_protocol gem

# File lib/semantic_logger_journald/formatter.rb, line 71
def format_syslog_time(time)
  # The timestamp format requires that a day with fewer than 2 digits have
  # what would normally be a preceding zero, be instead an extra space.
  day = time.strftime("%d")
  day = day.sub(/^0/, " ") if day =~ /^0\d/
  time.strftime("%b #{day} %H:%M:%S")
end
join_keys(*args) click to toggle source
# File lib/semantic_logger_journald/formatter.rb, line 66
def join_keys(*args)
  args.join(@flatten_payload).to_sym
end