Class: Yummi::Formatter::LogFormatter

Inherits:
Logger::Formatter
  • Object
show all
Includes:
BlockHandler
Defined in:
lib/yummi/logger.rb

Overview

A colorful log formatter

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from BlockHandler

block_call

Constructor Details

#initialize(colors = {}, &block) ⇒ LogFormatter

Creates a new formatter with the colors (assuming Linux default terminal colors):

  • DEBUG: blue

  • INFO: white

  • WARN: yellow

  • ERROR: red

  • FATAL: red (intense)

  • ANY: black (intense)

If a block is passed, it will be used to format the message. The block can use the following variables: severity, time, program_name and message.



59
60
61
62
63
64
65
66
67
68
69
# File 'lib/yummi/logger.rb', line 59

def initialize colors = {}, &block
  @colors = {
    :debug => :blue,
    :info => :white,
    :warn => :yellow,
    :error => :red,
    :fatal => :intense_red,
    :any => :intense_black
  }.merge! colors
  @format_block = block
end

Instance Attribute Details

#colorsObject

Colors for each severity:

  • :debug

  • :info

  • :warn

  • :error

  • :fatal

  • :any



44
45
46
# File 'lib/yummi/logger.rb', line 44

def colors
  @colors
end

Instance Method Details

#call(severity, time, program_name, message) ⇒ Object



73
74
75
76
# File 'lib/yummi/logger.rb', line 73

def call(severity, time, program_name, message)
  color = @colors[severity.downcase.to_sym]
  Yummi::colorize output(severity, time, program_name, message), color
end

#output(severity, time, program_name, message) ⇒ Object

Formats the message, override this method instead of #call



79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/yummi/logger.rb', line 79

def output severity, time, program_name, message
  if @format_block
    context = {
      :severity => severity,
      :time => time,
      :program_name => program_name,
      :message => message
    }
    block_call(context, &@format_block) << $/
  else
    super_call severity, time, program_name, message
  end
end

#super_callObject



71
# File 'lib/yummi/logger.rb', line 71

alias_method :super_call, :call