Class: SemanticLoggerJournald::Formatter

Inherits:
SemanticLogger::Formatters::Raw
  • Object
show all
Defined in:
lib/semantic_logger_journald/formatter.rb

Constant Summary collapse

LEVEL_MAP =
{
  trace: Journald::LOG_DEBUG,
  debug: Journald::LOG_DEBUG,
  info: Journald::LOG_INFO,
  warn: Journald::LOG_WARNING,
  error: Journald::LOG_ERR,
  fatal: Journald::LOG_CRIT
}.freeze
KEY_JOIN =
"__"

Instance Method Summary collapse

Constructor Details

#initialize(flatten_payload: KEY_JOIN, time_format: nil, **args) ⇒ Formatter

Returns a new instance of Formatter.



14
15
16
17
# 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

Instance Method Details

#levelObject



19
20
21
# File 'lib/semantic_logger_journald/formatter.rb', line 19

def level
  hash[:priority] = LEVEL_MAP.fetch(log.level)
end

#named_tagsObject



36
37
38
# File 'lib/semantic_logger_journald/formatter.rb', line 36

def named_tags
  hash.merge!(log.named_tags) if log.named_tags&.any?
end

#payloadObject



40
41
42
43
44
45
46
# 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

#pidObject

It’s already logged by journald as _PID



28
29
30
# File 'lib/semantic_logger_journald/formatter.rb', line 28

def pid
  nil
end

#tagsObject



32
33
34
# 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

#timeObject



23
24
25
# 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