Skip to content

Commit

Permalink
moved configuration logic into its own class
Browse files Browse the repository at this point in the history
  • Loading branch information
ajsharp committed Mar 21, 2010
1 parent 759842a commit f9d609a
Show file tree
Hide file tree
Showing 4 changed files with 190 additions and 99 deletions.
53 changes: 20 additions & 33 deletions lib/bunyan.rb
@@ -1,59 +1,44 @@
require 'rubygems'

#gem 'mongo_ext'

require 'mongo'
require 'singleton'

$LOAD_PATH.unshift(File.expand_path(File.dirname(__FILE__)))

require 'bunyan/config'

module Bunyan
class Logger
include Singleton

class InvalidConfigurationError < RuntimeError; end

attr_reader :db, :connection, :config, :configured
attr_reader :db, :connection, :collection, :config

# Bunyan::Logger.configure do |config|
# # required options
# config.database 'bunyan_logger'
# config.collection 'development_log'
#
# # optional options
# config.disabled true
# config.size 52428800 # 50.megabytes in Rails
# end
def configure(&block)
@config = {}
@config = Logger::Config.new

yield self
yield @config

ensure_required_options_exist
initialize_connection unless disabled?
@configured = true
end

def configured?
!!@configured
end

# First time called sets the database name.
# Otherwise, returns the database name.
def database(db_name = nil)
@config[:database] ||= db_name
@configured
end
alias_method :database=, :database

# First time called sets the collection name.
# Otherwise, returns the collection name.
# For the actual collection object returned by Mongo, see #db.
def collection(coll = nil)
@config[:collection] ||= coll
end
alias_method :collection=, :collection

def disabled(dis = nil)
@config[:disabled] ||= dis
end
alias_method :disabled=, :disabled

def disabled?
!!disabled
!!config.disabled
end

def method_missing(method, *args, &block)
Expand All @@ -71,29 +56,31 @@ def self.method_missing(method, *args, &block)

private
def initialize_connection
@connection = Mongo::Connection.new.db(database)
@db = retrieve_or_initialize_collection(collection)
@db = Mongo::Connection.new.db(config.database)
@connection = @db.connection
@collection = retrieve_or_initialize_collection(config.collection)
end

def database_is_usable?
configured? && !disabled?
end

def ensure_required_options_exist
raise InvalidConfigurationError, 'Error! Please provide a database name.' unless database
raise InvalidConfigurationError, 'Error! Please provide a collection name.' unless collection
raise InvalidConfigurationError, 'Error! Please provide a database name.' unless config.database
raise InvalidConfigurationError, 'Error! Please provide a collection name.' unless config.collection
end

def retrieve_or_initialize_collection(collection_name)
if collection_exists?(collection_name)
connection.collection(collection_name)
else
connection.create_collection(collection_name, :capped => true)
db.create_collection(collection_name, :capped => true, :size => config.size)
end
end

def collection_exists?(collection_name)
connection.collection_names.include? collection_name
end

end
end
46 changes: 46 additions & 0 deletions lib/bunyan/config.rb
@@ -0,0 +1,46 @@

module Bunyan
class Logger

class Config
# used to hold all user-defined configuration options
attr_accessor :collection, :database, :disabled

def initialize
@size = 52428800
@disabled = false
end

def [](meth)
send(meth)
end

# First time called sets the database name.
# Otherwise, returns the database name.
def database(db_name = nil)
@database ||= db_name
end
alias_method :database=, :database

# First time called sets the collection name.
# Otherwise, returns the collection name.
# For the actual collection object returned by Mongo, see #db.
def collection(coll = nil)
@collection ||= coll
end
alias_method :collection=, :collection

def disabled(dis = nil)
@disabled ||= dis
end
alias_method :disabled=, :disabled

# default size is 50 megabytes
def size(new_size = nil)
new_size.nil? ? @size : @size = new_size
end
alias_method :size=, :size
end

end
end
92 changes: 26 additions & 66 deletions spec/bunyan_spec.rb
@@ -1,27 +1,40 @@
require 'spec/spec_helper'
require 'spec_helper'

describe Bunyan::Logger do

before do
@conn = mock_mongo_connection
@logger = Bunyan::Logger.instance
@logger.stub!(:connection).and_return(@conn)
@mock_database = nil
Mongo::Connection.unstub!(:new)
end

it 'should have a connection' do
@logger.should respond_to :connection
configure_test_db
@logger.connection.should be_instance_of Mongo::Connection
end

it 'should have a collection' do
@logger.should respond_to :collection
it 'should have a reference to the mongo db object' do
@logger.db.should be_instance_of Mongo::DB
end

it 'should have a config hash' do
@logger.config.should be_a Hash
@logger.config.should respond_to :[]
end


it 'should use the mongo c extension' do
defined?(CBson::VERSION).should_not be_nil
end
end

describe 'when initializing the collection' do
before do
@conn = mock_mongo_connection
@logger = Bunyan::Logger.instance
@logger.stub!(:connection).and_return(@conn)
end

it 'should create a new capped collection if the collection does not already exist' do
@conn.should_receive(:create_collection).with('collection_1', :capped => true)
@conn.should_receive(:create_collection).with('collection_1', :capped => true, :size => 52428800)
@conn.stub!(:collection_names).and_return([])
Bunyan::Logger.configure do |config|
config.database 'database_1'
Expand All @@ -37,7 +50,6 @@
config.collection 'collection_1'
end
end

end

describe 'the required config options' do
Expand All @@ -58,46 +70,6 @@
end
end

describe 'bunyan logger configuration' do
describe 'setting config values' do
before do
Bunyan::Logger.configure do |c|
c.database 'database2'
c.collection 'collection2'
end
end

it 'should allow setting of the database' do
Bunyan::Logger.database.should == 'database2'
end

it 'shoudl allow setting of the collection name' do
Bunyan::Logger.collection.should == 'collection2'
end
end

describe 'the optional config options' do
it 'should allow the user to mark bunyan as disabled' do
Bunyan::Logger.configure do |c|
c.database 'test_db'
c.collection 'test_collection'
c.disabled true
end
Bunyan::Logger.should be_disabled
end
end

describe "when the disabled flag is set" do
it 'should not create a new logger instance' do
Bunyan::Logger.should_not_receive(:initialize_connection)
Bunyan::Logger.configure do |c|
c.database 'test_db'
c.collection 'test_collection'
c.disabled true
end
end
end
end

describe Bunyan::Logger, "#disabled?" do
it "should return false if nothing is set" do
Expand All @@ -109,20 +81,10 @@
end
end

describe 'the database getter' do
it 'should allow setting of the database' do
Bunyan::Logger.configure do |config|
config.database 'my_database'
config.collection 'my_collection'
end
Bunyan::Logger.instance.database.should == 'my_database'
end
end

describe 'mongodb instance methods passed to a logger instance' do
it 'should be passed through to the collection' do
configure_test_db
Bunyan::Logger.db.should_receive(:count)
Bunyan::Logger.collection.should_receive(:count)
Bunyan::Logger.count
end
end
Expand All @@ -138,18 +100,15 @@
end

describe 'when bunyan is disabled' do
before do
it "should not send messages to the mongo collection" do
@conn = mock_mongo_connection
Bunyan::Logger.configure do |config|
config.database 'bunyan_test'
config.collection 'bunyan_test_log'
config.disabled true
end
end

it "should not send messages to the mongo collection" do
%w(insert count find).each do |command|
Bunyan::Logger.db.should_not_receive(command)
Bunyan::Logger.collection.should_not_receive(command)
Bunyan::Logger.send command
end
end
Expand Down Expand Up @@ -181,3 +140,4 @@
Bunyan::Logger.should be_configured
end
end

0 comments on commit f9d609a

Please sign in to comment.