class Lumberjack::Formatter

Enumerable objects (including Hash and Array) will call the formatter recursively for each element.
and Exceptions. Strings are not converted and Exceptions are converted using the ExceptionFormatter.
By default, all object will be converted to strings using their inspect method except for Strings
are any object that responds to the call method.
Formats are added to a Formatter by associating them with a class using the add method. Formats
to log any object you want and have the logging system deal with converting it into a string.
This class controls the conversion of log entry messages into a loggable format. This allows you

def add(klass, formatter = nil, *args, &block)

Returns:
  • (self) - Returns itself so that add statements can be chained together.

Other tags:
    Yieldreturn: - The formatted string.

Other tags:
    Yieldparam: obj - The object to format.

Other tags:
    Yield: - A block that will be used as the formatter for the class.

Parameters:
  • args (Array) -- Arguments to pass to the formatter when it is initialized.
  • formatter (Symbol, Class, String, #call) -- The formatter to use for the class.
  • klass (Class, Module, String, Array) -- The class or module to add a formatter for.
def add(klass, formatter = nil, *args, &block)
  formatter ||= block
  if formatter.nil?
    remove(klass)
  else
    formatter_class_name = nil
    if formatter.is_a?(Symbol)
      formatter_class_name = "#{formatter.to_s.gsub(/(^|_)([a-z])/) { |m| $~[2].upcase }}Formatter"
    elsif formatter.is_a?(String)
      formatter_class_name = formatter
    end
    if formatter_class_name
      formatter = Formatter.const_get(formatter_class_name)
    end
    if formatter.is_a?(Class)
      formatter = formatter.new(*args)
    end
    Array(klass).each do |k|
      if k.instance_of?(Module)
        @module_formatters[k] = formatter
      else
        k = k.name if k.is_a?(Class)
        @class_formatters[k] = formatter
      end
    end
  end
  self
end

def call(severity, timestamp, progname, msg)

Parameters:
  • msg (Object) -- The message object to format.
  • progname (String) -- The name of the program logging the message.
  • timestamp (Time) -- The time the message was logged.
  • severity (Integer, String, Symbol) -- The severity of the message.
def call(severity, timestamp, progname, msg)
  "#{format(msg)}#{Lumberjack::LINE_SEPARATOR}"
end

def clear

Returns:
  • (self) - Returns itself so that clear statements can be chained together.
def clear
  @class_formatters.clear
  @module_formatters.clear
  self
end

def empty

Returns:
  • (Lumberjack::Formatter) - a new empty formatter
def empty
  new.clear
end

def format(message)

Returns:
  • (Object) - The formatted object.

Parameters:
  • message (Object) -- The message object to format.
def format(message)
  formatter = formatter_for(message.class)
  if formatter&.respond_to?(:call)
    formatter.call(message)
  else
    message
  end
end

def formatter_for(klass) # :nodoc:

:nodoc:
Find the formatter for a class by looking it up using the class hierarchy.
def formatter_for(klass) # :nodoc:
  check_modules = true
  until klass.nil?
    formatter = @class_formatters[klass.name]
    return formatter if formatter
    if check_modules
      _, formatter = @module_formatters.detect { |mod, f| klass.include?(mod) }
      check_modules = false
      return formatter if formatter
    end
    klass = klass.superclass
  end
end

def initialize

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

def remove(klass)

Returns:
  • (self) - Returns itself so that remove statements can be chained together.

Parameters:
  • klass (Class, Module, String, Array) -- The class or module to remove the formatters for.
def remove(klass)
  Array(klass).each do |k|
    if k.instance_of?(Module)
      @module_formatters.delete(k)
    else
      k = k.name if k.is_a?(Class)
      @class_formatters.delete(k)
    end
  end
  self
end