Class: Logging::Layouts::Parseable
- Inherits:
-
Logging::Layout
- Object
- Logging::Layout
- Logging::Layouts::Parseable
- Defined in:
- lib/logging/layouts/parseable.rb
Overview
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-17 16:15:42
level: INFO
logger: Foo::Bar
message: this is a log message
---
timestamp: 2009-04-17 16: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 your 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 work) 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-17 16:15:42","level":"INFO","logger":"Foo::Bar","message":"this is a log message"}
{"timestamp":"2009-04-17 16: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.
Constant Summary collapse
- DIRECTIVE_TABLE =
:stopdoc: Arguments to sprintf keyed to directive letters
{ 'logger' => 'event.logger', 'timestamp' => 'Time.now.strftime(Pattern::ISO8601)', 'level' => '::Logging::LNAMES[event.level]', 'message' => 'format_obj(event.data)', 'file' => 'event.file', 'line' => 'event.line', 'method' => 'event.method', 'pid' => 'Process.pid', 'millis' => 'Integer((Time.now-@created_at)*1000)', 'thread_id' => 'Thread.current.object_id', 'thread' => 'Thread.current[:name]' }
Instance Attribute Summary collapse
-
#items ⇒ Object
Returns the value of attribute items.
Class Method Summary collapse
-
.create_json_format_method(layout) ⇒ Object
call-seq: Pattern.create_json_format_methods( layout ).
-
.create_yaml_format_method(layout) ⇒ Object
call-seq: Pattern.create_yaml_format_methods( layout ).
-
.json(opts = {}) ⇒ Object
call-seq: Parseable.json( opts ).
-
.yaml(opts = {}) ⇒ Object
call-seq: Parseable.yaml( opts ).
Instance Method Summary collapse
-
#initialize(opts = {}) ⇒ Parseable
constructor
call-seq: Parseable.new( opts ).
Methods inherited from Logging::Layout
#footer, #format, #format_obj, #header, #try_yaml
Constructor Details
#initialize(opts = {}) ⇒ Parseable
call-seq:
Parseable.new( opts )
Creates a new Parseable layout using the following options:
:style => :json or :yaml
:items => %w[timestamp level logger message]
162 163 164 165 166 167 |
# File 'lib/logging/layouts/parseable.rb', line 162 def initialize( opts = {} ) super @created_at = Time.now @style = opts.getopt(:style, 'json').to_s.intern self.items = opts.getopt(:items, %w[timestamp level logger message]) end |
Instance Attribute Details
#items ⇒ Object
Returns the value of attribute items.
169 170 171 |
# File 'lib/logging/layouts/parseable.rb', line 169 def items @items end |
Class Method Details
.create_json_format_method(layout) ⇒ Object
call-seq:
Pattern.create_json_format_methods( layout )
This method will create the format
method in the given Parseable layout based on the configured items for the layout instance.
117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/logging/layouts/parseable.rb', line 117 def self.create_json_format_method( layout ) code = "undef :format if method_defined? :format\n" code << "def format( event )\n\"{" args = [] code << layout.items.map {|name| args << "format_as_json(#{Parseable::DIRECTIVE_TABLE[name]})" "\\\"#{name}\\\":%s" }.join(',') code << "}\\n\" % [#{args.join(', ')}]\nend" (class << layout; self end).class_eval(code, __FILE__, __LINE__) end |
.create_yaml_format_method(layout) ⇒ Object
call-seq:
Pattern.create_yaml_format_methods( layout )
This method will create the format
method in the given Parseable layout based on the configured items for the layout instance.
99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/logging/layouts/parseable.rb', line 99 def self.create_yaml_format_method( layout ) code = "undef :format if method_defined? :format\n" code << "def format( event )\nstr = {\n" code << layout.items.map {|name| "'#{name}' => #{Parseable::DIRECTIVE_TABLE[name]}" }.join(",\n") code << "\n}.to_yaml\nreturn str\nend\n" (class << layout; self end).class_eval(code, __FILE__, __LINE__) end |
.json(opts = {}) ⇒ Object
call-seq:
Parseable.json( opts )
Create a new Parseable layout that outputs log events usig JSON style formatting. See the initializer documentation for available options.
138 139 140 141 |
# File 'lib/logging/layouts/parseable.rb', line 138 def self.json( opts = {} ) opts[:style] = 'json' new(opts) end |
.yaml(opts = {}) ⇒ Object
call-seq:
Parseable.yaml( opts )
Create a new Parseable layout that outputs log events usig YAML style formatting. See the initializer documentation for available options.
149 150 151 152 |
# File 'lib/logging/layouts/parseable.rb', line 149 def self.yaml( opts = {} ) opts[:style] = 'yaml' new(opts) end |