Class: Logging::Appenders::CouchDB

Inherits:
Logging::Appender
  • Object
show all
Includes:
Buffering
Defined in:
lib/logging/couch_db_appender.rb

Overview

This class provides an Appender that sends log messages to a CouchDB instance.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, opts = {}) ⇒ CouchDB

call-seq:

CouchDB.new( name, :uri => 'http://localhost:5984',
                   :db_name => 'logging', :app_id => name )

Creates a new CouchDB appender that will format log events and send them to the CouchDB specified by the :uri and the :db_name. Your applications should specify a unique :app_id so that metrics about your application can be easily generated. If an app_id is not given, then the appender name is used as the app_id.

The CouchDB appender uses message buffering. Log events are saved in bulk to the CouchDB instance. You can specify the buffer size by setting :auto_flushing to the number of messages to buffer. Setting the auto_flushing to true will cause messages to be immediately sent to the CouchDB instance.

Communication with the CouchDB instance is asynchronous; calls to the appender sould return quickly. However, there will be some delay before the log events appear in the CouchDB instance. The communication thread must be woken up and scheduled in order for the events to be posted.



53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/logging/couch_db_appender.rb', line 53

def initialize( name, opts = {} )
  opts = opts.merge(:layout => ::Logging::Layouts::CouchDB.new)
  super(name, opts)

  # initialize the connection to the CouchDB instance
  uri = opts.getopt(:uri, 'http://localhost:5984')
  db_name = opts.getopt(:db_name, 'logging')
  self.app_id = opts.getopt(:app_id, name)

  @db_uri = uri + '/' + db_name + '/_bulk_docs'
  configure_buffering(opts)
  start_thread
end

Instance Attribute Details

#app_idObject

The string that uniquely identifies this application in the CouchDB messages.



28
29
30
# File 'lib/logging/couch_db_appender.rb', line 28

def app_id
  @app_id
end

Instance Method Details

#close(*args) ⇒ Object

Close the appender and wait for the internal writer thread to finish.



69
70
71
72
73
74
75
# File 'lib/logging/couch_db_appender.rb', line 69

def close( *args )
  super
  Thread.pass until @dispatcher.status == 'sleep' or !@dispatcher.status
  @dispatcher.wakeup if @dispatcher.status
  @dispatcher.join(60)
  self
end

#flushObject

Send all buffered log events to the CouchDB instance. If the messages cannot be saved the appender will be disabled, and all messages in the buffer and all subsequent messages will be lost.



81
82
83
84
85
86
# File 'lib/logging/couch_db_appender.rb', line 81

def flush
  return self if buffer.empty?
  @dispatch = true
  @dispatcher.wakeup if @dispatcher.status == 'sleep'
  self
end