class Yell::Formatter

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

Constants

LegacyTable

For standard formatted backwards compatibility

PatternMatcher
Table

Attributes

date_pattern[R]
pattern[R]

Public Class Methods

new( *args, &block ) click to toggle source

Initializes a new +Yell::Formatter+.

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

@example Blank formatter

Formatter.new

@example Formatter with a message pattern

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

@example Formatter with a message and date pattern

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

@example Formatter with a message modifier

Formatter.new do |f|
  f.modify(Hash) { |h| "Hash: #{h.inspect}" }
end
# File lib/yell/formatter.rb, line 107
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

Public Instance Methods

inspect() click to toggle source

Get a pretty string

# File lib/yell/formatter.rb, line 119
def inspect
  "#<#{self.class.name} pattern: #{@pattern.inspect}, date_pattern: #{@date_pattern.inspect}>"
end

Private Instance Methods

define_call_method!() click to toggle source

define a standard Logger backwards compatible call method for the formatter

# File lib/yell/formatter.rb, line 195
    def define_call_method!
      instance_eval "        def call(event, time = nil, progname = nil, msg = nil)
          event.is_a?(Yell::Event) ? #{to_sprintf(Table)} : #{to_sprintf(LegacyTable)}
        end
", __FILE__, __LINE__
    end
define_date_method!() click to toggle source
# File lib/yell/formatter.rb, line 179
    def define_date_method!
      buf = case @date_pattern
      when String then "t.strftime(@date_pattern)"
      when Symbol then respond_to?(@date_pattern, true) ? "#{@date_pattern}(t)" : "t.#{@date_pattern}"
      else t.iso8601
      end

      # define the method
      instance_eval "        def date(t = Time.now)
          #{buf}
        end
", __FILE__, __LINE__
    end
level( sev, length = nil ) click to toggle source
# File lib/yell/formatter.rb, line 221
def level( sev, length = nil )
  severity = case sev
  when Integer then Yell::Severities[sev] || 'ANY'
  else sev
  end

  length.nil? ? severity : severity[0, length]
end
message( messages ) click to toggle source
# File lib/yell/formatter.rb, line 230
def message( messages )
  @modifier.call(messages.is_a?(Array) && messages.size == 1 ? messages.first : messages)
end
noop() click to toggle source

do nothing

# File lib/yell/formatter.rb, line 235
def noop
  ''
end
to_sprintf( table ) click to toggle source
# File lib/yell/formatter.rb, line 203
def to_sprintf( table )
  buff, args, _pattern = "", [], @pattern.dup

  while true
    match = PatternMatcher.match(_pattern)

    buff << match[1] unless match[1].empty?
    break if match[2].nil?

    buff << match[2] + 's'
    args << table[ match[3] ]

    _pattern = match[4]
  end

  %Q{sprintf("#{buff.gsub(/"/, '\"')}", #{args.join(', ')})}
end