lib/semantic_logger/appender/mongodb.rb in semantic_logger-0.0.2 vs lib/semantic_logger/appender/mongodb.rb in semantic_logger-0.1.0
- old
+ new
@@ -44,14 +44,13 @@
class MongoDB
attr_reader :db, :collection_name
attr_accessor :formatter, :host_name, :safe, :application
# Create a MongoDB Appender instance
- # SemanticLogger::Appender::MongoDB.new(
- # :db => Cache::Work.db
- # )
#
+ # SemanticLogger::Appender::MongoDB.new(:db => Mongo::Connection.new['database'])
+ #
# Parameters:
# :db [Mongo::Database]
# The MongoDB database connection to use, not the database name
#
# :collection_name [String]
@@ -126,93 +125,59 @@
# Return the collection being used to write the log document to
def collection
@collection ||= db[collection_name]
end
- # For JRuby include the Thread name rather than its id
- if defined? Java
- def self.thread_name
- Java::java.lang::Thread.current_thread.name
- end
- else
- def self.thread_name
- Thread.object_id
- end
+ # Flush all pending logs to disk.
+ # Waits for all sent documents to be writted to disk
+ def flush
+ db.get_last_error
end
# Default log formatter
# Replace this formatter by supplying a Block to the initializer
def default_formatter
- Proc.new do |level, name, message, hash, block|
+ Proc.new do |log|
document = {
- :time => Time.now,
- :host_name => host_name,
- :pid => $PID,
- :thread => SemanticLogger::Appender::MongoDB.thread_name,
- :name => name,
- :level => level,
+ :time => log.time,
+ :host_name => host_name,
+ :pid => $PID,
+ :thread_name => log.thread_name,
+ :name => log.name,
+ :level => log.level,
}
document[:application] = application if application
- document[:message] = self.class.strip_colorizing(message) if message
+ document[:message] = self.class.strip_colorizing(log.message) if log.message
+ document[:duration] = log.duration if log.duration
- SemanticLogger::Appender::MongoDB.populate(document, hash) if hash
- SemanticLogger::Appender::MongoDB.populate(document, block.call) if block
+ if log.payload
+ if log.payload.is_a?(Exception)
+ exception = log.payload
+ document[:payload] = {
+ :exception => exception.class.name,
+ :message => exception.message,
+ :backtrace => exception.backtrace
+ }
+ else
+ document[:payload] = log.payload
+ end
+ end
document
end
end
# Strip the standard Rails colorizing from the logged message
def self.strip_colorizing(message)
message.to_s.gsub(/(\e(\[([\d;]*[mz]?))?)?/, '').strip
end
- # Log the message
- def log(level, name, message, hash, &block)
+ # 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(level, name, message, hash, &block)
+ document = formatter.call(log)
collection.insert(document, :safe=>safe)
- end
-
- # Populate Log Hash
- def self.populate(document, message)
- case message
- when ::String
- if document[:message]
- document[:message] << " " << strip_colorizing(message)
- else
- document[:message] = strip_colorizing(message)
- end
- when ::Exception
- document[:exception] = {
- :class => message.class.name,
- :message => message.message,
- :backtrace => message.backtrace
- }
- when ::Hash
- # With a hash, the message can be an element of the hash itself
- if msg = message[:message]
- # Cannot change supplied hash
- hash = message.clone
- hash.delete(:message)
- if document[:message]
- document[:message] << " " << strip_colorizing(msg)
- else
- document[:message] = strip_colorizing(msg)
- end
- document[:metadata] = hash
- else
- document[:metadata] = message
- end
- else
- if document[:message]
- document[:message] << " " << msg.inspect
- else
- document[:message] = msg.inspect
- end
- end
- document
end
end
end
end
\ No newline at end of file