Class: Incline::JsonLogFormatter

Inherits:
Logger::Formatter
  • Object
show all
Defined in:
lib/incline/json_log_formatter.rb

Overview

A log formatter that writes entries in JSON format (each line is a valid JSON object).

Constant Summary collapse

AUTO_DEBUG_PATTERNS =

Regular expressions used to auto-classify any matching message as a debug message.

[
    /^rendered\s/i,
    /started\sget\s"\/assets/i
]

Instance Method Summary collapse

Instance Method Details

#call(sev, time, _, msg) ⇒ Object

Overrides the default formatter behavior to log a JSON line.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/incline/json_log_formatter.rb', line 17

def call(sev, time, _, msg) #:nodoc:
  level = ({
      Logger::DEBUG   => 'DEBUG',
      Logger::INFO    => 'INFO',
      Logger::WARN    => 'WARN',
      Logger::ERROR   => 'ERROR',
      Logger::FATAL   => 'FATAL',
  }[sev] || sev.to_s).upcase

  if msg.present? && AUTO_DEBUG_PATTERNS.find{|pattern| msg =~ pattern}
    return '' if debug_skip?
    level = 'DEBUG'
  end

  if msg.present?
    # And we'll expand exceptions so we get as much info as possible.
    # If you just want the message, make sure you just pass the message.
    if msg.is_a?(::Exception)
      msg = "#{msg.message} (#{msg.class})\n#{(msg.backtrace || []).join("\n")}"
    elsif !msg.is_a?(::String)
      msg = msg.inspect
    end

    msg = rm_fmt msg

    {
        level: level,
        time: time.strftime('%Y-%m-%d %H:%M:%S'),
        message: msg,
        app_name: app_name,
        app_version: app_version,
        process_id: Process.pid,
    }.to_json + "\r\n"
  else
    ''
  end
end