Class: Lumberjack::Formatter

Inherits:
Object
  • Object
show all
Defined in:
lib/lumberjack/formatter.rb,
lib/lumberjack/formatter/id_formatter.rb,
lib/lumberjack/formatter/strip_formatter.rb,
lib/lumberjack/formatter/object_formatter.rb,
lib/lumberjack/formatter/string_formatter.rb,
lib/lumberjack/formatter/inspect_formatter.rb,
lib/lumberjack/formatter/date_time_formatter.rb,
lib/lumberjack/formatter/exception_formatter.rb,
lib/lumberjack/formatter/structured_formatter.rb,
lib/lumberjack/formatter/pretty_print_formatter.rb

Overview

This class controls the conversion of log entry messages into a loggable format. This allows you to log any object you want and have the logging system deal with converting it into a string.

Formats are added to a Formatter by associating them with a class using the add method. Formats are any object that responds to the call method.

By default, all object will be converted to strings using their inspect method except for Strings and Exceptions. Strings are not converted and Exceptions are converted using the ExceptionFormatter.

Enumerable objects (including Hash and Array) will call the formatter recursively for each element.

Direct Known Subclasses

TaggedLoggerSupport::Formatter

Defined Under Namespace

Classes: DateTimeFormatter, ExceptionFormatter, IdFormatter, InspectFormatter, ObjectFormatter, PrettyPrintFormatter, StringFormatter, StripFormatter, StructuredFormatter

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeFormatter

Returns a new instance of Formatter.



34
35
36
37
38
39
40
41
42
# File 'lib/lumberjack/formatter.rb', line 34

def initialize
  @class_formatters = {}
  @module_formatters = {}
  structured_formatter = StructuredFormatter.new(self)
  add([String, Numeric, TrueClass, FalseClass], :object)
  add(Object, InspectFormatter.new)
  add(Exception, :exception)
  add(Enumerable, structured_formatter)
end

Class Method Details

.emptyObject

Returns a new empty formatter with no mapping. For historical reasons, a formatter is initialized with mappings to help output objects as strings. This will return one without the default mappings.



29
30
31
# File 'lib/lumberjack/formatter.rb', line 29

def empty
  new.clear
end

Instance Method Details

#add(klass, formatter = nil, &block) ⇒ Object

Add a formatter for a class. The formatter can be specified as either an object that responds to the call method or as a symbol representing one of the predefined formatters, or as a block to the method call.

The predefined formatters are: :inspect, :string, :exception, and :pretty_print.

You can add multiple classes at once by passing an array of classes.

You can also pass class names as strings instead of the classes themselves. This can help avoid loading dependency issues. This applies only to classes; modules cannot be passed in as strings.

Examples

# Use a predefined formatter
formatter.add(MyClass, :pretty_print)

# Pass in a formatter object
formatter.add(MyClass, Lumberjack::Formatter::PrettyPrintFormatter.new)

# Use a block
formatter.add(MyClass){|obj| obj.humanize}

# Add statements can be chained together
formatter.add(MyClass, :pretty_print).add(YourClass){|obj| obj.humanize}


69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/lumberjack/formatter.rb', line 69

def add(klass, formatter = nil, &block)
  formatter ||= block
  if formatter.nil?
    remove(klass)
  else
    if formatter.is_a?(Symbol)
      formatter_class_name = "#{formatter.to_s.gsub(/(^|_)([a-z])/){|m| $~[2].upcase}}Formatter"
      formatter = Formatter.const_get(formatter_class_name).new
    end
    
    Array(klass).each do |k|
      if k.class == Module
        @module_formatters[k] = formatter
      else
        k = k.name if k.is_a?(Class)
        @class_formatters[k] = formatter
      end
    end
  end
  self
end

#call(severity, timestamp, progname, msg) ⇒ Object

Compatibility with the Logger::Formatter signature. This method will just convert the message object to a string and ignores the other parameters.



129
130
131
# File 'lib/lumberjack/formatter.rb', line 129

def call(severity, timestamp, progname, msg)
  "#{format(msg)}#{Lumberjack::LINE_SEPARATOR}"
end

#clearObject

Remove all formatters including the default formatter. Can be chained to add method calls.



111
112
113
114
115
# File 'lib/lumberjack/formatter.rb', line 111

def clear
  @class_formatters.clear
  @module_formatters.clear
  self
end

#format(message) ⇒ Object

Format a message object as a string.



118
119
120
121
122
123
124
125
# File 'lib/lumberjack/formatter.rb', line 118

def format(message)
  formatter = formatter_for(message.class)
  if formatter && formatter.respond_to?(:call)
    formatter.call(message)
  else
    message
  end
end

#remove(klass) ⇒ Object

Remove the formatter associated with a class. Remove statements can be chained together.

You can remove multiple classes at once by passing an array of classes.

You can also pass class names as strings instead of the classes themselves. This can help avoid loading dependency issues. This applies only to classes; modules cannot be passed in as strings.



98
99
100
101
102
103
104
105
106
107
108
# File 'lib/lumberjack/formatter.rb', line 98

def remove(klass)
  Array(klass).each do |k|
    if k.class == Module
      @module_formatters.delete(k)
    else
      k = k.name if k.is_a?(Class)
      @class_formatters.delete(k)
    end
  end
  self
end