lib/semantic_logger/appender/mongodb.rb in semantic_logger-0.5.3 vs lib/semantic_logger/appender/mongodb.rb in semantic_logger-0.6.0
- old
+ new
@@ -1,5 +1,6 @@
+require 'socket'
module SemanticLogger
module Appender
# The Mongo Appender for the SemanticLogger
# clarity_log Schema:
# _id : ObjectId("4d9cbcbf7abb3abdaf9679cd"),
@@ -39,13 +40,13 @@
# duration: 'ms'
# http_verb: 'get|post|..'
# params: Hash
#
# tracking_number: 'user defined tracking number'
- class MongoDB
+ class MongoDB < SemanticLogger::Base
attr_reader :db, :collection_name
- attr_accessor :formatter, :host_name, :safe, :application
+ attr_accessor :host_name, :safe, :application
# Create a MongoDB Appender instance
#
# SemanticLogger::Appender::MongoDB.new(:db => Mongo::Connection.new['database'])
#
@@ -82,10 +83,13 @@
# Release: 4GB
#
# :collection_max [Integer]
# 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
+ #
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
@@ -93,15 +97,15 @@
# 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]
- # Set the formatter to the supplied block
- @formatter = block || self.default_formatter
-
# Create the collection and necessary indexes
create_indexes
+
+ # Set the log level and formatter
+ super(params[:level], &block)
end
# Create the required capped collection
# Features of capped collection:
# * No indexes by default (not even on _id)
@@ -187,11 +191,10 @@
# 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
- document = formatter.call(log)
- collection.insert(document, :safe=>safe)
+ collection.insert(formatter.call(log), :safe=>safe) if level_index <= (log.level_index || 0)
end
end
end
end
\ No newline at end of file