Class: QB::Util::Logging::Formatters::Color

Inherits:
SemanticLogger::Formatters::Color
  • Object
show all
Defined in:
lib/qb/util/logging.rb

Overview

Custom tweaked color formatter (for CLI output).

  • Turns on multiline output in Awesome Print by default.

Instance Method Summary collapse

Constructor Details

#initialize(**options) ⇒ Color

Instantiate a new ColorFormatter.



63
64
65
66
67
68
69
# File 'lib/qb/util/logging.rb', line 63

def initialize **options
  super ap: { multiline: true },
        color_map: SemanticLogger::Formatters::Color::ColorMap.new(
          debug: SemanticLogger::AnsiColors::MAGENTA,
        ),
        **options
end

Instance Method Details

#call(log, logger) ⇒ return_type

Create the log entry text. Overridden to customize appearance - generally reduce amount of info and put payload on it's own line.

We need to replace two super functions, the first being SemanticLogger::Formatters::Color#call:

def call(log, logger)
  self.color = color_map[log.level]
  super(log, logger)
end

which doesn't do all too much, and the next being it's super-method, SemanticLogger::Formatters::Default#call:

# Default text log format
#  Generates logs of the form:
#    2011-07-19 14:36:15.660235 D [1149:ScriptThreadProcess] Rails -- Hello World
def call(log, logger)
  self.log    = log
  self.logger = logger

  [time, level, process_info, tags, named_tags, duration, name, message, payload, exception].compact.join(' ')
end

which does most the real assembly.

Parameters:

Returns:

  • (return_type)

    @todo Document return value.



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/qb/util/logging.rb', line 128

def call log, logger
  # SemanticLogger::Formatters::Color code
  self.color = color_map[log.level]
  
  # SemanticLogger::Formatters::Default code
  self.log    = log
  self.logger = logger
  
  [
    # time, annoyingly noisy and don't really need for local CLI app
    level,
    process_info,
    tags,
    named_tags,
    duration,
    name,
  ].compact.join( ' ' ) + 
  "\n" +
  [
    message,
    payload,
    exception,
  ].compact.join(' ') + 
  "\n" # I like extra newline to space shit out
  
end

#levelString

Upcase the log level.

Returns:

  • (String)


80
81
82
# File 'lib/qb/util/logging.rb', line 80

def level
  "#{ color }#{ log.level.upcase }#{ color_map.clear }"
end