Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Provide ability for objects to easily log

  • Loading branch information...
commit 1c62de07d0a85901e163bd33dc75afcc82756c9d 1 parent 41e0c4c
Durran Jordan durran authored
1  lib/mongo.rb
View
@@ -5,6 +5,7 @@
require 'mongo/collection'
require 'mongo/database'
require 'mongo/event'
+require 'mongo/loggable'
require 'mongo/logger'
require 'mongo/pool'
require 'mongo/protocol'
50 lib/mongo/loggable.rb
View
@@ -0,0 +1,50 @@
+# Copyright (C) 2009-2014 MongoDB, Inc.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+module Mongo
+
+ # Allows objects to easily log operations.
+ #
+ # @since 3.0.0
+ module Loggable
+
+ # Log the operations. If a block is provided it will be yielded to,
+ # otherwise only the logging will take place.
+ #
+ # @example Log a query operation.
+ # loggable.log(:debug, "MONGO.query", operations)
+ #
+ # @param [ Symbol ] level The log level.
+ # @param [ String ] prefix The prefix for the log line.
+ # @param [ Array<Object> ] operations The operations to log. The must
+ # respond to #log_message.
+ #
+ # @return [ Object ] The result of the block or nil if no block given.
+ #
+ # @since 3.0.0
+ def log(level, prefix, operations)
+ started = Time.new
+ begin
+ yield if block_given?
+ rescue Exception => e
+ raise e
+ ensure
+ runtime = ("%.4fms" % (1000 * (Time.now.to_f - started.to_f)))
+ operations.each do |operation|
+ Logger.send(level, prefix, operation.log_message, runtime)
+ end
+ end
+ end
+ end
+end
30 lib/mongo/logger.rb
View
@@ -27,12 +27,12 @@ class << self
# Logger.debug('mongo', 'message', '10ms')
#
# @param [ String ] prefix The category prefix.
- # @param [ String ] payload The log message.
+ # @param [ String ] message The log message.
# @param [ String ] runtime The time of the operation.
#
# @since 3.0.0
- def debug(prefix, payload, runtime)
- logger.debug("#{prefix} | #{payload} | runtime: #{runtime}")
+ def debug(prefix, message, runtime)
+ logger.debug("#{prefix} | #{message} | runtime: #{runtime}")
end
# Log a error level message.
@@ -41,12 +41,12 @@ def debug(prefix, payload, runtime)
# Logger.error('mongo', 'message', '10ms')
#
# @param [ String ] prefix The category prefix.
- # @param [ String ] payload The log message.
+ # @param [ String ] message The log message.
# @param [ String ] runtime The time of the operation.
#
# @since 3.0.0
- def error(prefix, payload, runtime)
- logger.error("#{prefix} | #{payload} | runtime: #{runtime}")
+ def error(prefix, message, runtime)
+ logger.error("#{prefix} | #{message} | runtime: #{runtime}")
end
# Log a fatal level message.
@@ -55,12 +55,12 @@ def error(prefix, payload, runtime)
# Logger.fatal('mongo', 'message', '10ms')
#
# @param [ String ] prefix The category prefix.
- # @param [ String ] payload The log message.
+ # @param [ String ] message The log message.
# @param [ String ] runtime The time of the operation.
#
# @since 3.0.0
- def fatal(prefix, payload, runtime)
- logger.fatal("#{prefix} | #{payload} | runtime: #{runtime}")
+ def fatal(prefix, message, runtime)
+ logger.fatal("#{prefix} | #{message} | runtime: #{runtime}")
end
# Log a info level message.
@@ -69,12 +69,12 @@ def fatal(prefix, payload, runtime)
# Logger.info('mongo', 'message', '10ms')
#
# @param [ String ] prefix The category prefix.
- # @param [ String ] payload The log message.
+ # @param [ String ] message The log message.
# @param [ String ] runtime The time of the operation.
#
# @since 3.0.0
- def info(prefix, payload, runtime)
- logger.info("#{prefix} | #{payload} | runtime: #{runtime}")
+ def info(prefix, message, runtime)
+ logger.info("#{prefix} | #{message} | runtime: #{runtime}")
end
# Log a warn level message.
@@ -83,12 +83,12 @@ def info(prefix, payload, runtime)
# Logger.warn('mongo', 'message', '10ms')
#
# @param [ String ] prefix The category prefix.
- # @param [ String ] payload The log message.
+ # @param [ String ] message The log message.
# @param [ String ] runtime The time of the operation.
#
# @since 3.0.0
- def warn(prefix, payload, runtime)
- logger.warn("#{prefix} | #{payload} | runtime: #{runtime}")
+ def warn(prefix, message, runtime)
+ logger.warn("#{prefix} | #{message} | runtime: #{runtime}")
end
# Get the wrapped logger. If none was set will return a default debug
62 spec/mongo/loggable_spec.rb
View
@@ -0,0 +1,62 @@
+require 'spec_helper'
+
+describe Mongo::Loggable do
+
+ let(:operation) do
+ Class.new do
+ def log_message
+ 'test'
+ end
+ end.new
+ end
+
+ describe '#log' do
+
+ let(:loggable) do
+ Class.new do
+ include Mongo::Loggable
+ end.new
+ end
+
+ let(:operation) do
+ double('operation')
+ end
+
+ before do
+ expect(operation).to receive(:log_message).and_return('test')
+ expect(Mongo::Logger).to receive(:debug).with('MONGO', 'test', anything())
+ end
+
+ context 'when a block is provided' do
+
+ context 'when an exception occurs' do
+
+ it 'logs the message' do
+ expect {
+ loggable.log(:debug, 'MONGO', [ operation ]) do
+ raise RuntimeError
+ end
+ }.to raise_error(RuntimeError)
+ end
+ end
+
+ context 'when no exception occurs' do
+
+ it 'executes the block and logs the message' do
+ expect(
+ loggable.log(:debug, 'MONGO', [ operation ]) do
+ 'testing'
+ end
+ ).to eq('testing')
+ end
+ end
+ end
+
+ context 'when no block is provided' do
+
+ it 'logs the message' do
+ expect(loggable.log(:debug, 'MONGO', [ operation ])).to be_nil
+ end
+ end
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.