Class: Logsly::Logging182::Layouts::Parseable
- Inherits:
-
Logsly::Logging182::Layout
- Object
- Logsly::Logging182::Layout
- Logsly::Logging182::Layouts::Parseable
- Defined in:
- lib/logsly/logging182/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-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.
Constant Summary collapse
- DIRECTIVE_TABLE =
:stopdoc: Arguments to sprintf keyed to directive letters
{ 'logger' => 'event.logger'.freeze, 'timestamp' => 'iso8601_format(event.time)'.freeze, 'level' => '::Logsly::Logging182::LNAMES[event.level]'.freeze, 'message' => 'format_obj(event.data)'.freeze, 'file' => 'event.file'.freeze, 'line' => 'event.line'.freeze, 'method' => 'event.method'.freeze, 'pid' => 'Process.pid'.freeze, 'millis' => 'Integer((event.time-@created_at)*1000)'.freeze, 'thread_id' => 'Thread.current.object_id'.freeze, 'thread' => 'Thread.current[:name]'.freeze, 'mdc' => 'Logsly::Logging182::MappedDiagnosticContext.context'.freeze, 'ndc' => 'Logsly::Logging182::NestedDiagnosticContext.context'.freeze }
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
-
#format_obj(obj) ⇒ Object
Public: Take a given object and convert it into a format suitable for inclusion as a log message.
-
#initialize(opts = {}) ⇒ Parseable
constructor
call-seq: Parseable.new( opts ).
Methods inherited from Logsly::Logging182::Layout
#footer, #format, #header, #try_json, #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]
180 181 182 183 184 185 |
# File 'lib/logsly/logging182/layouts/parseable.rb', line 180 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.
187 188 189 |
# File 'lib/logsly/logging182/layouts/parseable.rb', line 187 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.
137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/logsly/logging182/layouts/parseable.rb', line 137 def self.create_json_format_method( layout ) code = "undef :format if method_defined? :format\n" code << "def format( event )\nh = {\n" code << layout.items.map {|name| "'#{name}' => #{Parseable::DIRECTIVE_TABLE[name]}" }.join(",\n") code << "\n}\nMultiJson.encode(h) << \"\\n\"\nend\n" (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.
119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/logsly/logging182/layouts/parseable.rb', line 119 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 using JSON style formatting. See the initializer documentation for available options.
156 157 158 159 |
# File 'lib/logsly/logging182/layouts/parseable.rb', line 156 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 using YAML style formatting. See the initializer documentation for available options.
167 168 169 170 |
# File 'lib/logsly/logging182/layouts/parseable.rb', line 167 def self.yaml( opts = {} ) opts[:style] = 'yaml' new(opts) end |
Instance Method Details
#format_obj(obj) ⇒ Object
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.
215 216 217 218 219 220 221 222 223 224 225 226 227 |
# File 'lib/logsly/logging182/layouts/parseable.rb', line 215 def format_obj( obj ) case obj when Exception h = { :class => obj.class.name, :message => obj. } h[:backtrace] = obj.backtrace if @backtrace && !obj.backtrace.nil? h when Time iso8601_format(obj) else obj end end |