Parent

Loggability::Formatter

An abstract base class for Loggability log formatters.

Constants

DEFAULT_DATETIME_FORMAT

The default sprintf pattern

Attributes

derivatives[R]
datetime_format[RW]

Strftime format for log messages

format[RW]

Main log sprintf format

Public Class Methods

create( type, *args ) click to toggle source

Create a formatter of the specified type, loading it if it hasn't already been loaded.

# File lib/loggability/formatter.rb, line 31
def self::create( type, *args )
        require "loggability/formatter/#{type}"
        type = type.to_sym

        if self.derivatives.key?( type )
                return self.derivatives[ type ].new( *args )
        else
                raise LoadError,
                        "require of %s formatter succeeded (%p), but it didn't load a class named %p::%s" %
                        [ type, self.derivatives, self, type.to_s.capitalize ]
        end
end
inherited( subclass ) click to toggle source

Inherited hook -- add subclasses to the ::derivatives Array.

# File lib/loggability/formatter.rb, line 22
def self::inherited( subclass )
        super
        classname = subclass.name.sub( /.*::/, '' ).downcase.to_sym
        Loggability::Formatter.derivatives[ classname ] = subclass
end
new( logformat, datetime_format=DEFAULT_DATETIME_FORMAT ) click to toggle source

Initialize a new Loggability::Formatter. The specified logformat should be a sprintf pattern with positional placeholders:

%1$s

Time (pre-formatted using strftime with the datetime_format)

%2$d

Time microseconds

%3$d

PID

%4$s

Thread ID

%5$s

Severity

%6$s

Object/Program Name

%7$s

Message

# File lib/loggability/formatter.rb, line 57
def initialize( logformat, datetime_format=DEFAULT_DATETIME_FORMAT )
        @format          = logformat.dup
        @datetime_format = datetime_format.dup
end

Public Instance Methods

call( severity, time, progname, message ) click to toggle source

Create a log message from the given severity, time, progname, and message and return it.

# File lib/loggability/formatter.rb, line 76
def call( severity, time, progname, message )
        timeformat = self.datetime_format
        args = [
                time.strftime( timeformat ),                                  # %1$s
                time.usec,                                                    # %2$d
                Process.pid,                                                  # %3$d
                Thread.current == Thread.main ? 'main' : Thread.object_id,    # %4$s
                severity.downcase,                                            # %5$s
                progname,                                                     # %6$s
                self.msg2str(message, severity)                               # %7$s
        ]

        return self.format % args
end

Protected Instance Methods

msg2str( msg, severity ) click to toggle source

Format the specified msg for output to the log.

# File lib/loggability/formatter.rb, line 97
def msg2str( msg, severity )
        case msg
        when String
                return msg
        when Exception
                bt = severity == 'DEBUG' ? msg.backtrace.join("\n") : msg.backtrace.first
                return "%p: %s from %s" % [ msg.class, msg.message, bt ]
        else
                return msg.inspect
        end
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.