Class: Logging::Layouts::CouchDB

Inherits:
Logging::Layout
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

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

#itemsObject

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

.timestamp(time = nil) ⇒ Object

call-seq:

CouchDB.timestamp

Returns a timestamp that can be sorted in CouchDB.



90
91
92
93
# File 'lib/logging/couch_db_layout.rb', line 90

def self.timestamp( time = nil )
  utc = (time || Time.now).utc
  utc.strftime(TIME_FMT) % utc.usec
end

Instance Method Details

#format_obj(value) ⇒ 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
# File 'lib/logging/couch_db_layout.rb', line 141

def format_obj( value )
  case value
  when nil, Numeric, String, Array, Hash; value
  when Exception
    ary = ["<#{obj.class.name}> #{obj.message}"]
    ary.concat(obj.backtrace) unless obj.backtrace.nil?
    ary
  else super(value) end
end