Class: NRSER::Log::Formatters::Color

Inherits:
SemanticLogger::Formatters::Color
  • Object
show all
Includes:
Mixin
Defined in:
lib/nrser/log/formatters/color.rb

Overview

Definitions

Constant Summary collapse

ANSI_ESC_DARK_GRAY =

ANSI escape sequence to start “Dark Gray” color.

Returns:

"\e[1;30m"

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Mixin

#body, #body=, #header, #header=

Constructor Details

#initialize(ap: {multiline: true }, color_map: self.class.default_color_map, time_format: ::SemanticLogger::Formatters::Base::TIME_FORMAT, log_host: false, log_application: false) ⇒ Color

Instantiate a new ‘ColorFormatter`.



98
99
100
101
102
103
104
105
106
107
108
# File 'lib/nrser/log/formatters/color.rb', line 98

def initialize  ap: {multiline: true }, # raw: true},
                color_map: self.class.default_color_map,
                time_format: ::SemanticLogger::Formatters::Base::TIME_FORMAT,
                log_host: false,
                log_application: false
  super ap: ap,
        color_map: color_map,
        time_format: time_format,
        log_host: log_host,
        log_application: log_application
end

Class Method Details

.colorize(string, type) ⇒ Object



85
86
87
# File 'lib/nrser/log/formatters/color.rb', line 85

def self.colorize string, type
  ap_formatter.colorize string, type
end

.default_color_mapSemanticLogger::Formatters::Color::ColorMap

TODO:

Document default_color_map method.

Returns:

  • (SemanticLogger::Formatters::Color::ColorMap)


56
57
58
59
60
61
62
# File 'lib/nrser/log/formatters/color.rb', line 56

def self.default_color_map
  SemanticLogger::Formatters::Color::ColorMap.new(
    debug: SemanticLogger::AnsiColors::MAGENTA,
    trace: ANSI_ESC_DARK_GRAY,
    warn: SemanticLogger::AnsiColors::YELLOW,
  )
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

[SemanticLogger::Formatters::Color#call]: github.com/rocketjob/semantic_logger/blob/v4.2.0/lib/semantic_logger/formatters/color.rb#L98

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

[SemanticLogger::Formatters::Default#call]: github.com/rocketjob/semantic_logger/blob/v4.2.0/lib/semantic_logger/formatters/default.rb#L64

which does most the real assembly.

Parameters:

Returns:

  • (return_type)

    @todo Document return value.



176
177
178
179
180
181
182
183
184
185
186
# File 'lib/nrser/log/formatters/color.rb', line 176

def call log, logger
  # SemanticLogger::Formatters::Color code
  self.color = color_map[log.level]
  
  # SemanticLogger::Formatters::Default code
  self.log    = log
  self.logger = logger
  
  render_log
  
end

#levelString

Upcase the log level.

Returns:



123
124
125
# File 'lib/nrser/log/formatters/color.rb', line 123

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

#nameObject

Instance Methods



114
115
116
# File 'lib/nrser/log/formatters/color.rb', line 114

def name
  self.class.colorize log.name, :class
end

#timeObject



128
129
130
# File 'lib/nrser/log/formatters/color.rb', line 128

def time
  "#{ color }#{ super() }#{ color_map.clear }"
end