lib/semantic_logger/appender/mongodb.rb in semantic_logger-2.7.0 vs lib/semantic_logger/appender/mongodb.rb in semantic_logger-2.8.0
- old
+ new
@@ -29,11 +29,11 @@
# stack_trace: []
# }
#
class MongoDB < SemanticLogger::Appender::Base
attr_reader :db, :collection_name
- attr_accessor :host_name, :safe, :application
+ attr_accessor :host_name, :write_concern, :application
# Create a MongoDB Appender instance
#
# SemanticLogger::Appender::MongoDB.new(:db => Mongo::Connection.new['database'])
#
@@ -47,15 +47,14 @@
#
# :host_name [String]
# host_name to include in the document logged to Mongo
# Default: first part of host name extracted from Socket
#
- # :safe [Boolean]
- # Whether to use safe write for logging
- # Not recommended to change this value except to diagnose connection
- # issues or when log entries are not being written to Mongo
- # Default: false
+ # :write_concern [Integer]
+ # Write concern to use
+ # see: http://docs.mongodb.org/manual/reference/write-concern/
+ # Default: 0
#
# :application [String]
# Name of the application to include in the document written to mongo
# Default: nil (None)
#
@@ -73,26 +72,32 @@
# Maximum number of log entries that the capped collection will hold
#
# :level [Symbol]
# Only allow log entries of this level or higher to be written to MongoDB
#
+ # :filter [Regexp|Proc]
+ # RegExp: Only include log messages where the class name matches the supplied
+ # regular expression. All other messages will be ignored
+ # Proc: Only include log messages where the supplied Proc returns true
+ # The Proc must return true or false
def initialize(params={}, &block)
@db = params[:db] || raise('Missing mandatory parameter :db')
@collection_name = params[:collection_name] || 'semantic_logger'
@host_name = params[:host_name] || Socket.gethostname.split('.').first
- @safe = params[:safe] || false
+ @write_concern = params[:write_concern] || 0
@application = params[:application]
+ filter = params[:filter]
# Create a collection that will hold the lesser of 1GB space or 10K documents
@collection_size = params[:collection_size] || 1024**3
@collection_max = params[:collection_max]
# Create the collection and necessary indexes
create_indexes
# Set the log level and formatter
- super(params[:level], &block)
+ super(params[:level], filter, &block)
end
# Create the required capped collection
# Features of capped collection:
# * No indexes by default (not even on _id)
@@ -101,11 +106,13 @@
# * Documents are always stored in insertion order
# * A find will always return the documents in their insertion order
#
# Creates an index based on tags to support faster lookups
def create_indexes
- db.create_collection(collection_name, {:capped => true, :size => @collection_size, :max => @collection_max})
+ options = {:capped => true, :size => @collection_size}
+ options[:max] = @collection_max if @collection_max
+ db.create_collection(collection_name, options)
collection.ensure_index('tags')
end
# Purge all data from the capped collection by dropping the collection
# and recreating it.
@@ -170,12 +177,10 @@
end
# Log the message to MongoDB
def log(log)
# Insert log entry into Mongo
- # Use safe=>false so that we do not wait for it to be written to disk, or
- # for the response from the MongoDB server
- collection.insert(formatter.call(log), :safe=>safe) if level_index <= (log.level_index || 0)
+ collection.insert(formatter.call(log), :w=>@write_concern) if level_index <= (log.level_index || 0)
end
end
end
end
\ No newline at end of file