Class: QAT::Logger::Formatter
- Inherits:
-
Log4r::Formatter
- Object
- Log4r::Formatter
- QAT::Logger::Formatter
- Defined in:
- lib/qat/logger/formatter.rb
Overview
This class represents a formatter used by the logging outputters instances
See QAT::Logger::ConsoleOutputter See QAT::Logger::RemoteOutputter
Constant Summary collapse
- LEVEL_COLORS =
Level to color mapping
Hash.new(:to_s).merge({ 'INFO' => :green, 'WARN' => :purple, 'ERROR' => :red, 'FATAL' => :red })
Instance Attribute Summary collapse
-
#colors ⇒ Object
Accessors for formatter properties.
-
#date_pattern ⇒ Object
Accessors for formatter properties.
Instance Method Summary collapse
-
#format(event) ⇒ Object
Formats the
event
to be logged. -
#initialize(opts = {}) ⇒ Formatter
constructor
Creates a new formatter with an user defined hash of
options
.
Constructor Details
#initialize(opts = {}) ⇒ Formatter
Creates a new formatter with an user defined hash of options
.
Options are:
-
‘colors’ [Boolean] Default is true.
-
‘date_pattern’ [String] Default is ‘%Y-%m-%d %H:%M:%S,%L’.
35 36 37 38 39 40 |
# File 'lib/qat/logger/formatter.rb', line 35 def initialize(opts={}) Log4r::Logger.log_internal { "Starting new formatter with options" } Log4r::Logger.log_internal { opts.ai } @colors = opts['colors'].nil? ? true : opts['colors'] @date_pattern = opts['date_pattern'] || '%Y-%m-%d %H:%M:%S,%L' end |
Instance Attribute Details
#colors ⇒ Object
Accessors for formatter properties
27 28 29 |
# File 'lib/qat/logger/formatter.rb', line 27 def colors @colors end |
#date_pattern ⇒ Object
Accessors for formatter properties
27 28 29 |
# File 'lib/qat/logger/formatter.rb', line 27 def date_pattern @date_pattern end |
Instance Method Details
#format(event) ⇒ Object
Formats the event
to be logged.
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/qat/logger/formatter.rb', line 44 def format(event) date = Time.now.strftime(@date_pattern.to_s) level_name = Log4r::LNAMES[event.level] level = sprintf "%-5s", level_name channel_info = if event.tracer event.tracer[0].split(File::SEPARATOR)[-1].match(/(.+:\d+)(?::in `.+')/).captures.first else event.fullname end logged_object = case event.data when Exception "#{event.data.} (#{event.data.class}) #{event.data.backtrace.ai plain: true, index: false, indent: 2}" when String event.data else event.data.ai plain: true, indent: 2 end color_method = @colors ? LEVEL_COLORS[level_name] : :to_s "#{date} [#{level}] #{channel_info}: #{logged_object}".send(color_method) + "\n" end |