Loggability::Formatter::

Default class

The default sprintf-based log formatter class.

Constants

DEFAULT_DATETIME_FORMAT

The default sprintf pattern

FORMAT

The format to output unless debugging is turned on

Attributes

datetime_format RW

Strftime format for log messages

format RW

Main log sprintf format

Public Class Methods

new( logformat=FORMAT, datetime_format=DEFAULT_DATETIME_FORMAT )

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/default.rb, line 28
def initialize( logformat=FORMAT, datetime_format=DEFAULT_DATETIME_FORMAT )
        super()

        @format          = logformat.dup
        @datetime_format = datetime_format.dup
end

Public Instance Methods

call( severity, time, progname, message )

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

# File lib/loggability/formatter/default.rb, line 49
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.current.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 )

Format the specified msg for output to the log.

# File lib/loggability/formatter/default.rb, line 70
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