Class: Leveret::LogFormatter

Inherits:
Logger::Formatter
  • Object
show all
Defined in:
lib/leveret/log_formatter.rb

Overview

Prettier logging than the default

Constant Summary collapse

SEVERITY_TO_COLOR_MAP =

ANSI colour codes for different message types

{ 'DEBUG' => '0;37', 'INFO' => '32', 'WARN' => '33', 'ERROR' => '31', 'FATAL' => '31',
'UNKNOWN' => '37' }.freeze

Instance Method Summary collapse

Instance Method Details

#call(severity, datetime, _progname, msg) ⇒ String

Build a pretty formatted log line

Parameters:

  • severity (String)

    Log level, one of debug, info, warn, error, fatal or unknown

  • datetime (Time)

    Timestamp of log event

  • _progname (String)

    (Unused) the name of the progname set in the logger

  • msg (String)

    Body of the log message

Returns:

  • (String)

    Formatted and coloured log message in the format: “YYYY-MM-DD HH:MM:SS:USEC [SEVERITY] MESSAGE (pid:Process ID)”



17
18
19
20
21
22
# File 'lib/leveret/log_formatter.rb', line 17

def call(severity, datetime, _progname, msg)
  formatted_time = datetime.strftime("%Y-%m-%d %H:%M:%S") << datetime.usec.to_s[0..2].rjust(3)
  color = SEVERITY_TO_COLOR_MAP[severity]

  "\033[0;37m#{formatted_time}\033[0m [\033[#{color}m#{severity}\033[0m] #{msg2str(msg)} (pid:#{Process.pid})\n"
end