Object
An abstract base class for Loggability log formatters.
The default sprintf pattern
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 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
Initialize a new Loggability::Formatter. The specified logformat should be a sprintf pattern with positional placeholders:
Time (pre-formatted using strftime with the datetime_format)
Time microseconds
PID
Thread ID
Severity
Object/Program Name
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
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
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
Generated with the Darkfish Rdoc Generator 2.