Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
216 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
# 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 | ||
|
||
# Provides ability to log messages. | ||
# | ||
# @since 3.0.0 | ||
class Logger | ||
|
||
class << self | ||
|
||
# Log a debug level message. | ||
# | ||
# @example Log a debug level message. | ||
# Logger.debug('mongo', 'message', '10ms') | ||
# | ||
# @param [ String ] prefix The category prefix. | ||
# @param [ String ] payload 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}") | ||
end | ||
|
||
# Log a error level message. | ||
# | ||
# @example Log a error level message. | ||
# Logger.error('mongo', 'message', '10ms') | ||
# | ||
# @param [ String ] prefix The category prefix. | ||
# @param [ String ] payload 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}") | ||
end | ||
|
||
# Log a fatal level message. | ||
# | ||
# @example Log a fatal level message. | ||
# Logger.fatal('mongo', 'message', '10ms') | ||
# | ||
# @param [ String ] prefix The category prefix. | ||
# @param [ String ] payload 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}") | ||
end | ||
|
||
# Log a info level message. | ||
# | ||
# @example Log a info level message. | ||
# Logger.info('mongo', 'message', '10ms') | ||
# | ||
# @param [ String ] prefix The category prefix. | ||
# @param [ String ] payload 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}") | ||
end | ||
|
||
# Log a warn level message. | ||
# | ||
# @example Log a warn level message. | ||
# Logger.warn('mongo', 'message', '10ms') | ||
# | ||
# @param [ String ] prefix The category prefix. | ||
# @param [ String ] payload 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}") | ||
end | ||
|
||
# Get the wrapped logger. If none was set will return a default debug | ||
# level logger. | ||
# | ||
# @example Get the wrapped logger. | ||
# Mongo::Logger.logger | ||
# | ||
# @return [ ::Logger ] The wrapped logger. | ||
# | ||
# @since 3.0.0 | ||
def logger | ||
@logger ||= default_logger | ||
end | ||
|
||
# Set the logger. | ||
# | ||
# @example Set the wrapped logger. | ||
# Mongo::Logger.logger = logger | ||
# | ||
# @param [ ::Logger ] other The logger to set. | ||
# | ||
# @return [ ::Logger ] The wrapped logger. | ||
# | ||
# @since 3.0.0 | ||
def logger=(other) | ||
@logger = other | ||
end | ||
|
||
private | ||
|
||
def default_logger | ||
logger = ::Logger.new($stdout) | ||
logger.level = ::Logger::DEBUG | ||
logger | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
require 'spec_helper' | ||
|
||
describe Mongo::Logger do | ||
|
||
let(:logger) do | ||
described_class.logger | ||
end | ||
|
||
describe '.debug' do | ||
|
||
it 'logs a debug message' do | ||
expect(logger).to receive(:debug).with("mongo.query | message | runtime: 10ms") | ||
described_class.debug('mongo.query', 'message', '10ms') | ||
end | ||
end | ||
|
||
describe '.error' do | ||
|
||
it 'logs a error message' do | ||
expect(logger).to receive(:error).with("mongo.query | message | runtime: 10ms") | ||
described_class.error('mongo.query', 'message', '10ms') | ||
end | ||
end | ||
|
||
describe '.fatal' do | ||
|
||
it 'logs a fatal message' do | ||
expect(logger).to receive(:fatal).with("mongo.query | message | runtime: 10ms") | ||
described_class.fatal('mongo.query', 'message', '10ms') | ||
end | ||
end | ||
|
||
describe '.info' do | ||
|
||
it 'logs a info message' do | ||
expect(logger).to receive(:info).with("mongo.query | message | runtime: 10ms") | ||
described_class.info('mongo.query', 'message', '10ms') | ||
end | ||
end | ||
|
||
describe '.logger' do | ||
|
||
context 'when no logger has been set' do | ||
|
||
it 'returns the default logger' do | ||
expect(logger.level).to eq(Logger::DEBUG) | ||
end | ||
end | ||
|
||
context 'when a logger has been set' do | ||
|
||
let(:info) do | ||
Logger.new($stdout).tap do |log| | ||
log.level = Logger::INFO | ||
end | ||
end | ||
|
||
let(:debug) do | ||
Logger.new($stdout).tap do |log| | ||
log.level = Logger::DEBUG | ||
end | ||
end | ||
|
||
before do | ||
described_class.logger = info | ||
end | ||
|
||
after do | ||
described_class.logger = debug | ||
end | ||
|
||
it 'returns the provided logger' do | ||
expect(logger.level).to eq(Logger::INFO) | ||
end | ||
end | ||
end | ||
|
||
describe '.warn' do | ||
|
||
it 'logs a warn message' do | ||
expect(logger).to receive(:warn).with("mongo.query | message | runtime: 10ms") | ||
described_class.warn('mongo.query', 'message', '10ms') | ||
end | ||
end | ||
end |