Class: Logging::Layouts::CouchDB
- Inherits:
-
Logging::Layout
- Object
- Logging::Layout
- Logging::Layouts::CouchDB
- Defined in:
- lib/logging/couch_db_layout.rb
Overview
This layout will produce log output in JSON format. This is different than the standard “Parseable” layout in that the JSON is tailored specifically for the CouchDB appender. Differences are mainly in the timestamp format and available options. The LogEvent data is only formatted into JSON and not inspect format or the standard to_s format.
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 in JSON format.
'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.
'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.
Constant Summary collapse
- DIRECTIVE_TABLE =
:stopdoc: Arguments to sprintf keyed to directive letters
{ 'logger' => 'event.logger', 'timestamp' => 'CouchDB.timestamp(event.time)', 'level' => 'event.level', 'message' => 'format_obj(event.data)', 'file' => 'event.file', 'line' => 'event.line', 'method' => 'event.method', 'pid' => 'Process.pid', 'thread_id' => 'Thread.current.object_id', 'thread' => 'Thread.current[:name]' }
- TIME_FMT =
'%Y-%m-%dT%H:%M:%S.%%06dZ'
- @@discriminator =
0
Instance Attribute Summary collapse
-
#items ⇒ Object
Returns the value of attribute items.
Class Method Summary collapse
-
.create_format_method(layout) ⇒ Object
call-seq: CouchDB.create_format_method( layout ).
-
.timestamp(time = nil) ⇒ Object
call-seq: CouchDB.timestamp.
Instance Method Summary collapse
-
#format_obj(obj) ⇒ Object
Take the given value and convert it into a format suitable for use in a JSON object structure.
-
#initialize(opts = {}) ⇒ CouchDB
constructor
call-seq: CouchDB.new( opts ).
Constructor Details
#initialize(opts = {}) ⇒ CouchDB
call-seq:
CouchDB.new( opts )
Creates a new CouchDB layout using the following options:
:items => %w[timestamp level logger message]
116 117 118 119 |
# File 'lib/logging/couch_db_layout.rb', line 116 def initialize( opts = {} ) super self.items = opts.getopt(:items, %w[timestamp level logger message]) end |
Instance Attribute Details
#items ⇒ Object
Returns the value of attribute items.
121 122 123 |
# File 'lib/logging/couch_db_layout.rb', line 121 def items @items end |
Class Method Details
.create_format_method(layout) ⇒ Object
call-seq:
CouchDB.create_format_method( layout )
This method will create the format
method in the given CouchDB layout based on the configured items for the layout instance.
72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/logging/couch_db_layout.rb', line 72 def self.create_format_method( layout ) code = "undef :format if method_defined? :format\n" code << "def format( event )\n{" code << layout.items.map {|name| "#{name.to_sym.inspect} => #{CouchDB::DIRECTIVE_TABLE[name]}" }.join(',') code << "\n}\nend" (class << layout; self end).class_eval(code, __FILE__, __LINE__) end |
Instance Method Details
#format_obj(obj) ⇒ Object
Take the given value and convert it into a format suitable for use in a JSON object structure.
141 142 143 144 145 146 147 148 149 150 |
# File 'lib/logging/couch_db_layout.rb', line 141 def format_obj( obj ) case obj when nil, Numeric, String, Array, Hash obj when Exception ary = ["<#{obj.class.name}> #{obj.}"] ary.concat(obj.backtrace) unless obj.backtrace.nil? ary else super(obj) end end |