Class: Yell::Formatter

Inherits:
Object
  • Object
show all
Defined in:
lib/yell/formatter.rb

Overview

The Formatter provides a handle to configure your log message style.

Defined Under Namespace

Classes: Builder, Modifier

Constant Summary collapse

Table =
{
  "m" => "message(event.messages)",    # Message
  "l" => "level(event.level, 1)",      # Level (short), e.g.'I', 'W'
  "L" => "level(event.level)",         # Level, e.g. 'INFO', 'WARN'
  "d" => "date(event.time)",           # ISO8601 Timestamp
  "h" => "event.hostname",             # Hostname
  "p" => "event.pid",                  # PID
  "P" => "event.progname",             # Progname
  "t" => "event.thread_id",            # Thread ID
  "F" => "event.file",                 # Path with filename where the logger was called
  "f" => "File.basename(event.file)",  # Filename where the loger was called
  "M" => "event.method",               # Method name where the logger was called
  "n" => "event.line",                 # Line where the logger was called
  "N" => "event.name"                  # Name of the logger
}
LegacyTable =

For standard formatted backwards compatibility

Hash[ Table.keys.map { |k| [k, 'noop'] } ].merge(
  'm' => 'message(msg)',
  'l' => 'level(event, 1)',
  'L' => 'level(event)',
  'd' => 'date(time)',
  "p" => "$$",
  'P' => 'progname'
)
PatternMatcher =
/([^%]*)(%\d*)?(#{Table.keys.join('|')})?(.*)/m

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args, &block) ⇒ Formatter

Initializes a new Yell::Formatter.

Upon initialization it defines a format method. ‘format` takes a Event instance as agument in order to apply for desired log message formatting.

Examples:

Blank formatter

Formatter.new

Formatter with a message pattern

Formatter.new("%d [%5L] %p : %m")

Formatter with a message and date pattern

Formatter.new("%d [%5L] %p : %m", "%D %H:%M:%S.%L")

Formatter with a message modifier

Formatter.new do |f|
  f.modify(Hash) { |h| "Hash: #{h.inspect}" }
end


108
109
110
111
112
113
114
115
116
117
# File 'lib/yell/formatter.rb', line 108

def initialize( *args, &block )
  builder = Builder.new(*args, &block)

  @pattern = builder.pattern
  @date_pattern = builder.date_pattern
  @modifier = builder.modifier

  define_date_method!
  define_call_method!
end

Instance Attribute Details

#date_patternObject (readonly)

Returns the value of attribute date_pattern.



86
87
88
# File 'lib/yell/formatter.rb', line 86

def date_pattern
  @date_pattern
end

#patternObject (readonly)

Returns the value of attribute pattern.



86
87
88
# File 'lib/yell/formatter.rb', line 86

def pattern
  @pattern
end

Instance Method Details

#inspectObject

Get a pretty string



120
121
122
# File 'lib/yell/formatter.rb', line 120

def inspect
  "#<#{self.class.name} pattern: #{@pattern.inspect}, date_pattern: #{@date_pattern.inspect}>"
end