Class: Log4r::BasicFormatter

Inherits:
SimpleFormatter show all
Defined in:
lib/log4r-color/formatter/formatter.rb

Overview

BasicFormatter produces output like this:

WARN loggername: I dropped my Wookie!

Or like this if trace is on:

WARN loggername(file.rb at 12): Hot potato!

Also, it will pretty-print any Exception it gets and inspect everything else.

Hash arguments include:

depth

How many lines of the stacktrace to display.

Constant Summary collapse

@@basicformat =
"%*s %s"

Instance Method Summary collapse

Constructor Details

#initialize(hash = {}) ⇒ BasicFormatter

Returns a new instance of BasicFormatter.



51
52
53
# File 'lib/log4r-color/formatter/formatter.rb', line 51

def initialize(hash={})
  @depth = (hash[:depth] or hash['depth'] or 7).to_i
end

Instance Method Details

#format(event) ⇒ Object



55
56
57
58
59
60
61
# File 'lib/log4r-color/formatter/formatter.rb', line 55

def format(event)
  buff = sprintf(@@basicformat, MaxLevelLength, LNAMES[event.level],
         event.name)
  buff << (event.tracer.nil? ? "" : "(#{event.tracer[0]})") + ": "
  buff << format_object(event.data) + "\n"
  buff
end

#format_object(obj) ⇒ Object

Formats data according to its class:

String

Prints it out as normal.

Exception

Produces output similar to command-line exceptions.

Object

Prints the type of object, then the output of inspect. An example – Array: [1, 2, 3]



70
71
72
73
74
75
76
77
78
79
# File 'lib/log4r-color/formatter/formatter.rb', line 70

def format_object(obj)
  if obj.kind_of? Exception
    return "Caught #{obj.class}: #{obj.message}\n\t" +\
           obj.backtrace[0...@depth].join("\n\t")
  elsif obj.kind_of? String
    return obj
  else # inspect the object
    return "#{obj.class}: #{obj.inspect}"
  end
end