Class: Loggability::Formatter::Default
- Inherits:
-
Loggability::Formatter
- Object
- Loggability::Formatter
- Loggability::Formatter::Default
- Defined in:
- lib/loggability/formatter/default.rb
Overview
The default sprintf-based log formatter class.
Constant Summary collapse
- DEFAULT_DATETIME_FORMAT =
The default sprintf pattern
'%Y-%m-%d %H:%M:%S'
- FORMAT =
The format to output unless debugging is turned on
"[%1$s.%2$06d %3$d/%4$s] %5$5s {%6$s} -- %7$s\n"
Instance Attribute Summary collapse
-
#datetime_format ⇒ Object
Strftime format for log messages.
-
#format ⇒ Object
Main log sprintf format.
Attributes inherited from Loggability::Formatter
Instance Method Summary collapse
-
#call(severity, time, progname, message) ⇒ Object
Create a log message from the given
severity
,time
,progname
, andmessage
and return it. -
#initialize(logformat = FORMAT, datetime_format = DEFAULT_DATETIME_FORMAT) ⇒ Default
constructor
Initialize a new Loggability::Formatter.
Methods inherited from Loggability::Formatter
Constructor Details
#initialize(logformat = FORMAT, datetime_format = DEFAULT_DATETIME_FORMAT) ⇒ Default
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
29 30 31 32 33 34 |
# File 'lib/loggability/formatter/default.rb', line 29 def initialize( logformat=FORMAT, datetime_format=DEFAULT_DATETIME_FORMAT ) super() @format = logformat.dup @datetime_format = datetime_format.dup end |
Instance Attribute Details
#datetime_format ⇒ Object
Strftime format for log messages
45 46 47 |
# File 'lib/loggability/formatter/default.rb', line 45 def datetime_format @datetime_format end |
#format ⇒ Object
Main log sprintf format
42 43 44 |
# File 'lib/loggability/formatter/default.rb', line 42 def format @format end |
Instance Method Details
#call(severity, time, progname, message) ⇒ Object
Create a log message from the given severity
, time
, progname
, and message
and return it.
50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/loggability/formatter/default.rb', line 50 def call( severity, time, progname, ) 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(, severity) # %7$s ] return self.format % args end |