diff --git a/lib/bunyan.rb b/lib/bunyan.rb index 8e7b660..fb1fc34 100644 --- a/lib/bunyan.rb +++ b/lib/bunyan.rb @@ -1,27 +1,32 @@ 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? @@ -29,31 +34,11 @@ def configure(&block) 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) @@ -71,8 +56,9 @@ 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? @@ -80,20 +66,21 @@ def database_is_usable? 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 diff --git a/lib/bunyan/config.rb b/lib/bunyan/config.rb new file mode 100644 index 0000000..e1cfdbe --- /dev/null +++ b/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 diff --git a/spec/bunyan_spec.rb b/spec/bunyan_spec.rb index c93d785..c80fcfa 100644 --- a/spec/bunyan_spec.rb +++ b/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' @@ -37,7 +50,6 @@ config.collection 'collection_1' end end - end describe 'the required config options' do @@ -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 @@ -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 @@ -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 @@ -181,3 +140,4 @@ Bunyan::Logger.should be_configured end end + diff --git a/spec/config_spec.rb b/spec/config_spec.rb new file mode 100644 index 0000000..1c95b7c --- /dev/null +++ b/spec/config_spec.rb @@ -0,0 +1,98 @@ +require 'spec_helper' + +describe Bunyan::Logger::Config do + before do + @config = Bunyan::Logger::Config.new + end + + it 'should have a collection' do + @config.should respond_to :collection + end +end + +describe Bunyan::Logger::Config, 'collection size' do + it 'should default to 50 megabytes' do + # configure_test_db + config = Bunyan::Logger::Config.new + config.size.should == 52428800 + end + + it 'should all the user to set the collection size' do + Bunyan::Logger.configure do |c| + c.database 'bunyan_test_log' + c.collection 'configured_size' + c.size 100_000_000 + end + + Bunyan::Logger.config[:size].should == 100_000_000 + end +end + +describe Bunyan::Logger::Config, 'when setting the collection size' do + it 'should never set the size to nil' do + @config = Bunyan::Logger::Config.new + @config.size nil + @config.size.should == 52428800 + end + + it 'should override the default value' do + @config = Bunyan::Logger::Config.new + @config.size 1010 + @config.size.should == 1010 + end +end + +describe Bunyan::Logger::Config, 'when getting the collection size' do + it 'should return the collection size' +end + +describe Bunyan::Logger::Config, 'alternate method invocation syntax' do + it 'should act like a hash' do + config = Bunyan::Logger::Config.new + config.size 10 + config.should_receive(:size).and_return(10) + config[:size].should == 10 + 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.config.database.should == 'database2' + end + + it 'shoudl allow setting of the collection name' do + Bunyan::Logger.config.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 +