Class: LogSanity::Formatter

Inherits:
Logger::Formatter
  • Object
show all
Includes:
ActiveSupport::TaggedLogging::Formatter
Defined in:
lib/log_sanity/formatter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#string_formatterObject



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

def string_formatter
  return @string_formatter if defined?(@string_formatter)
  @string_formatter ||= ActiveSupport::Logger::SimpleFormatter.new
end

Instance Method Details

#call(severity, timestamp, progname, msg) ⇒ Object

tags are ignored when rendering as json however, tags are prepended when rendering with string_formatter



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/log_sanity/formatter.rb', line 16

def call(severity, timestamp, progname, msg)
  if msg.is_a? Hash
    msg.reverse_merge!('at' => timestamp) unless msg.key?('at')
  elsif msg.is_a? String
    if string_formatter
      msg = "#{tags_text}#{msg}" if current_tags.any?
      return string_formatter.call(severity, timestamp, progname, msg)
    else
      msg = {'at' => timestamp, 'message' => msg}
    end
  else
    msg = {'at' => timestamp, 'object' => msg.inspect}
  end
  msg['at'] = msg['at'].utc
  "#{msg.to_json}\n"
end