Class | Logging::Layouts::Parseable |
In: |
lib/logging/layouts/parseable.rb
|
Parent: | ::Logging::Layout |
This layout will produce parseable log output in either JSON or YAML format. This makes it much easier for machines to parse log files and perform analysis on those logs.
The information about the log event can be configured when the layout is created. Any or all of the following labels can be set as the items to log:
'logger' Used to output the name of the logger that generated the log event. 'timestamp' Used to output the timestamp of the log event. 'level' Used to output the level of the log event. 'message' Used to output the application supplied message associated with the log event. 'file' Used to output the file name where the logging request was issued. 'line' Used to output the line number where the logging request was issued. 'method' Used to output the method name where the logging request was issued. 'pid' Used to output the process ID of the currently running program. 'millis' Used to output the number of milliseconds elapsed from the construction of the Layout until creation of the log event. 'thread_id' Used to output the object ID of the thread that generated the log event. 'thread' Used to output the name of the thread that generated the log event. Name can be specified using Thread.current[:name] notation. Output empty string if name not specified. This option helps to create more human readable output for multithread application logs.
These items are supplied to the layout as an array of strings. The items ‘file’, ‘line’, and ‘method’ will only work if the Logger generating the events is configured to generate tracing information. If this is not the case these fields will always be empty.
When configured to output log events in YAML format, each log message will be formatted as a hash in it‘s own YAML document. The hash keys are the name of the item, and the value is what you would expect it to be. Therefore, for the default set of times log message would appear as follows:
--- timestamp: 2009-04-17T16:15:42 level: INFO logger: Foo::Bar message: this is a log message --- timestamp: 2009-04-17T16:15:43 level: ERROR logger: Foo message: <RuntimeError> Oooops!!
The output order of the fields is not guaranteed to be the same as the order specified in the items list. This is because Ruby hashes are not ordered by default (unless you‘re running this in Ruby 1.9).
When configured to output log events in JSON format, each log message will be formatted as an object (in the JSON sense of the word) on it‘s own line in the log output. Therefore, to parse the output you must read it line by line and parse the individual objects. Taking the same example above the JSON output would be:
{"timestamp":"2009-04-17T16:15:42","level":"INFO","logger":"Foo::Bar","message":"this is a log message"} {"timestamp":"2009-04-17T16:15:43","level":"ERROR","logger":"Foo","message":"<RuntimeError> Oooops!!"}
The output order of the fields is guaranteed to be the same as the order specified in the items list.
items | [R] |
Public: Take a given object and convert it into a format suitable for inclusion as a log message. The conversion allows the object to be more easily expressed in YAML or JSON form.
If the object is an Exception, then this method will return a Hash containing the exception class name, message, and backtrace (if any).
obj - The Object to format
Returns the formatted Object.