bunyan 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -1 +1,3 @@
1
1
  .bundle
2
+ coverage
3
+ pkg
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ Version 0.2.0
2
+ =============
3
+ * Moved all configuration-related stuff to the Config class
4
+ * Added ability to set the collection size in configuration block
5
+ * Bunyan attributes (:connection, :collection, :db) now map directly to mongo counterparts
data/README.md CHANGED
@@ -13,7 +13,7 @@ Configure
13
13
  =========
14
14
  The only configuration options required are the database and collection name.
15
15
 
16
- For rails apps, puts the config block in an initializer.
16
+ For rails apps, put the following config block in an initializer.
17
17
 
18
18
  # config/initializers/bunyan.rb
19
19
  Bunyan::Logger.configure do |config|
@@ -58,3 +58,4 @@ TODO
58
58
  ====
59
59
  * Ability to limit bunyan to only run in certain environments
60
60
  * Add middleware client for easy drop-in to rails/rack apps
61
+ * <del>Ability to configure size of capped collection</del>
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.0
1
+ 0.2.0
data/bunyan.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{bunyan}
8
- s.version = "0.1.0"
8
+ s.version = "0.2.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Alex Sharp"]
12
- s.date = %q{2010-03-17}
12
+ s.date = %q{2010-03-20}
13
13
  s.description = %q{Bunyan is a thin ruby wrapper around a MongoDB capped collection, created with high-performance, flexible logging in mind.}
14
14
  s.email = %q{ajsharp@gmail.com}
15
15
  s.extra_rdoc_files = [
@@ -17,13 +17,17 @@ Gem::Specification.new do |s|
17
17
  ]
18
18
  s.files = [
19
19
  ".gitignore",
20
+ "CHANGELOG.md",
20
21
  "Gemfile",
21
22
  "README.md",
22
23
  "Rakefile",
23
24
  "VERSION",
24
25
  "bunyan.gemspec",
26
+ "examples/rails.rb",
25
27
  "lib/bunyan.rb",
28
+ "lib/bunyan/config.rb",
26
29
  "spec/bunyan_spec.rb",
30
+ "spec/config_spec.rb",
27
31
  "spec/spec.opts",
28
32
  "spec/spec_helper.rb"
29
33
  ]
@@ -34,7 +38,10 @@ Gem::Specification.new do |s|
34
38
  s.summary = %q{A MongoDB-based logging solution.}
35
39
  s.test_files = [
36
40
  "spec/bunyan_spec.rb",
37
- "spec/spec_helper.rb"
41
+ "spec/config_spec.rb",
42
+ "spec/integration/non_rails_spec.rb",
43
+ "spec/spec_helper.rb",
44
+ "examples/rails.rb"
38
45
  ]
39
46
 
40
47
  if s.respond_to? :specification_version then
data/examples/rails.rb ADDED
@@ -0,0 +1,11 @@
1
+
2
+ # put in config/initializers/bunyan.rb
3
+ Bunyan::Logger.configure do |config|
4
+ # required options
5
+ config.database "bunyan_logger"
6
+ config.collection "#{RAILS_ENV}_log"
7
+
8
+ # optional
9
+ config.size 75.megabytes
10
+ end
11
+
data/lib/bunyan.rb CHANGED
@@ -1,59 +1,49 @@
1
1
  require 'rubygems'
2
-
3
- #gem 'mongo_ext'
4
-
5
2
  require 'mongo'
6
3
  require 'singleton'
7
4
 
5
+ $LOAD_PATH.unshift(File.expand_path(File.dirname(__FILE__)))
6
+
7
+ require 'bunyan/config'
8
+
8
9
  module Bunyan
9
10
  class Logger
10
11
  include Singleton
11
12
 
12
13
  class InvalidConfigurationError < RuntimeError; end
13
14
 
14
- attr_reader :db, :connection, :config
15
+ attr_reader :db, :connection, :collection, :config
15
16
 
16
17
  # Bunyan::Logger.configure do |config|
17
18
  # # required options
18
19
  # config.database 'bunyan_logger'
19
20
  # config.collection 'development_log'
21
+ #
22
+ # # optional options
23
+ # config.disabled true
24
+ # config.size 52428800 # 50.megabytes in Rails
20
25
  # end
21
26
  def configure(&block)
22
- @config = {}
27
+ @config = Logger::Config.new
23
28
 
24
- yield self
29
+ yield @config
25
30
 
26
31
  ensure_required_options_exist
27
32
  initialize_connection unless disabled?
33
+ @configured = true
28
34
  end
29
35
 
30
- # First time called sets the database name.
31
- # Otherwise, returns the database name.
32
- def database(db_name = nil)
33
- @config[:database] ||= db_name
34
- end
35
- alias_method :database=, :database
36
-
37
- # First time called sets the collection name.
38
- # Otherwise, returns the collection name.
39
- # For the actual collection object returned by Mongo, see #db.
40
- def collection(coll = nil)
41
- @config[:collection] ||= coll
36
+ def configured?
37
+ @configured
42
38
  end
43
- alias_method :collection=, :collection
44
-
45
- def disabled(dis = nil)
46
- @config[:disabled] ||= dis
47
- end
48
- alias_method :disabled=, :disabled
49
39
 
50
40
  def disabled?
51
- !!disabled
41
+ !!config.disabled
52
42
  end
53
43
 
54
44
  def method_missing(method, *args, &block)
55
45
  begin
56
- db.send(method, *args)
46
+ collection.send(method, *args) if database_is_usable?
57
47
  rescue
58
48
  super(method, *args, &block)
59
49
  end
@@ -66,25 +56,31 @@ module Bunyan
66
56
 
67
57
  private
68
58
  def initialize_connection
69
- @connection = Mongo::Connection.new.db(database)
70
- @db = retrieve_or_initialize_collection(collection)
59
+ @db = Mongo::Connection.new.db(config.database)
60
+ @connection = @db.connection
61
+ @collection = retrieve_or_initialize_collection(config.collection)
62
+ end
63
+
64
+ def database_is_usable?
65
+ configured? && !disabled?
71
66
  end
72
67
 
73
68
  def ensure_required_options_exist
74
- raise InvalidConfigurationError, 'Error! Please provide a database name.' unless database
75
- raise InvalidConfigurationError, 'Error! Please provide a collection name.' unless collection
69
+ raise InvalidConfigurationError, 'Error! Please provide a database name.' unless config.database
70
+ raise InvalidConfigurationError, 'Error! Please provide a collection name.' unless config.collection
76
71
  end
77
72
 
78
73
  def retrieve_or_initialize_collection(collection_name)
79
74
  if collection_exists?(collection_name)
80
- connection.collection(collection_name)
75
+ db.collection(collection_name)
81
76
  else
82
- connection.create_collection(collection_name, :capped => true)
77
+ db.create_collection(collection_name, :capped => true, :size => config.size)
83
78
  end
84
79
  end
85
80
 
86
81
  def collection_exists?(collection_name)
87
- connection.collection_names.include? collection_name
82
+ db.collection_names.include? collection_name
88
83
  end
84
+
89
85
  end
90
86
  end
@@ -0,0 +1,46 @@
1
+
2
+ module Bunyan
3
+ class Logger
4
+
5
+ class Config
6
+ # used to hold all user-defined configuration options
7
+ attr_accessor :collection, :database, :disabled
8
+
9
+ def initialize
10
+ @size = 52428800
11
+ @disabled = false
12
+ end
13
+
14
+ def [](meth)
15
+ send(meth)
16
+ end
17
+
18
+ # First time called sets the database name.
19
+ # Otherwise, returns the database name.
20
+ def database(db_name = nil)
21
+ @database ||= db_name
22
+ end
23
+ alias_method :database=, :database
24
+
25
+ # First time called sets the collection name.
26
+ # Otherwise, returns the collection name.
27
+ # For the actual collection object returned by Mongo, see #db.
28
+ def collection(coll = nil)
29
+ @collection ||= coll
30
+ end
31
+ alias_method :collection=, :collection
32
+
33
+ def disabled(dis = nil)
34
+ @disabled ||= dis
35
+ end
36
+ alias_method :disabled=, :disabled
37
+
38
+ # default size is 50 megabytes
39
+ def size(new_size = nil)
40
+ new_size.nil? ? @size : @size = new_size
41
+ end
42
+ alias_method :size=, :size
43
+ end
44
+
45
+ end
46
+ end
data/spec/bunyan_spec.rb CHANGED
@@ -1,27 +1,40 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Bunyan::Logger do
4
-
5
4
  before do
6
- @conn = mock_mongo_connection
7
5
  @logger = Bunyan::Logger.instance
8
- @logger.stub!(:connection).and_return(@conn)
6
+ @mock_database = nil
7
+ Mongo::Connection.unstub!(:new)
9
8
  end
10
9
 
11
10
  it 'should have a connection' do
12
- @logger.should respond_to :connection
11
+ configure_test_db
12
+ @logger.connection.should be_instance_of Mongo::Connection
13
13
  end
14
14
 
15
- it 'should have a collection' do
16
- @logger.should respond_to :collection
15
+ it 'should have a reference to the mongo db object' do
16
+ @logger.db.should be_instance_of Mongo::DB
17
17
  end
18
18
 
19
19
  it 'should have a config hash' do
20
- @logger.config.should be_a Hash
20
+ @logger.config.should respond_to :[]
21
+ end
22
+
23
+
24
+ it 'should use the mongo c extension' do
25
+ defined?(CBson::VERSION).should_not be_nil
26
+ end
27
+ end
28
+
29
+ describe 'when initializing the collection' do
30
+ before do
31
+ @conn = mock_mongo_connection
32
+ @logger = Bunyan::Logger.instance
33
+ @logger.stub!(:connection).and_return(@conn)
21
34
  end
22
35
 
23
36
  it 'should create a new capped collection if the collection does not already exist' do
24
- @conn.should_receive(:create_collection).with('collection_1', :capped => true)
37
+ @conn.should_receive(:create_collection).with('collection_1', :capped => true, :size => 52428800)
25
38
  @conn.stub!(:collection_names).and_return([])
26
39
  Bunyan::Logger.configure do |config|
27
40
  config.database 'database_1'
@@ -37,7 +50,6 @@ describe Bunyan::Logger do
37
50
  config.collection 'collection_1'
38
51
  end
39
52
  end
40
-
41
53
  end
42
54
 
43
55
  describe 'the required config options' do
@@ -58,46 +70,6 @@ describe 'the required config options' do
58
70
  end
59
71
  end
60
72
 
61
- describe 'bunyan logger configuration' do
62
- describe 'setting config values' do
63
- before do
64
- Bunyan::Logger.configure do |c|
65
- c.database 'database2'
66
- c.collection 'collection2'
67
- end
68
- end
69
-
70
- it 'should allow setting of the database' do
71
- Bunyan::Logger.database.should == 'database2'
72
- end
73
-
74
- it 'shoudl allow setting of the collection name' do
75
- Bunyan::Logger.collection.should == 'collection2'
76
- end
77
- end
78
-
79
- describe 'the optional config options' do
80
- it 'should allow the user to mark bunyan as disabled' do
81
- Bunyan::Logger.configure do |c|
82
- c.database 'test_db'
83
- c.collection 'test_collection'
84
- c.disabled true
85
- end
86
- Bunyan::Logger.should be_disabled
87
- end
88
- end
89
-
90
- describe "when the disabled flag is set" do
91
- it 'should not create a new logger instance' do
92
- Bunyan::Logger.should_not_receive(:initialize_connection)
93
- Bunyan::Logger.configure do |c|
94
- c.database 'test_db'
95
- c.collection 'test_collection'
96
- c.disabled true
97
- end
98
- end
99
- end
100
- end
101
73
 
102
74
  describe Bunyan::Logger, "#disabled?" do
103
75
  it "should return false if nothing is set" do
@@ -109,19 +81,10 @@ describe Bunyan::Logger, "#disabled?" do
109
81
  end
110
82
  end
111
83
 
112
- describe 'the database getter' do
113
- it 'should allow setting of the database' do
114
- Bunyan::Logger.configure do |config|
115
- config.database 'my_database'
116
- config.collection 'my_collection'
117
- end
118
- Bunyan::Logger.instance.database.should == 'my_database'
119
- end
120
- end
121
-
122
84
  describe 'mongodb instance methods passed to a logger instance' do
123
85
  it 'should be passed through to the collection' do
124
- Bunyan::Logger.db.should_receive(:count)
86
+ configure_test_db
87
+ Bunyan::Logger.collection.should_receive(:count)
125
88
  Bunyan::Logger.count
126
89
  end
127
90
  end
@@ -135,3 +98,31 @@ describe 'alternate configuration syntax' do
135
98
  Bunyan::Logger.config[:database].should == 'some_database'
136
99
  end
137
100
  end
101
+
102
+ describe 'when bunyan is disabled' do
103
+ it "should not send messages to the mongo collection" do
104
+ @conn = mock_mongo_connection
105
+ Bunyan::Logger.configure do |config|
106
+ config.database 'bunyan_test'
107
+ config.collection 'bunyan_test_log'
108
+ config.disabled true
109
+ end
110
+ %w(insert count find).each do |command|
111
+ Bunyan::Logger.collection.should_not_receive(command)
112
+ Bunyan::Logger.send command
113
+ end
114
+ end
115
+ end
116
+
117
+ describe 'when bunyan is not configured' do
118
+ it 'should not try to send messages to mongo' do
119
+ Bunyan::Logger.instance.stub!(:configured?).and_return(false)
120
+ Bunyan::Logger.should_not be_configured
121
+ Bunyan::Logger.should_not be_disabled
122
+ %w(insert count find).each do |command|
123
+ Bunyan::Logger.collection.should_not_receive(command)
124
+ Bunyan::Logger.send command
125
+ end
126
+ end
127
+ end
128
+
@@ -0,0 +1,98 @@
1
+ require 'spec_helper'
2
+
3
+ describe Bunyan::Logger::Config do
4
+ before do
5
+ @config = Bunyan::Logger::Config.new
6
+ end
7
+
8
+ it 'should have a collection' do
9
+ @config.should respond_to :collection
10
+ end
11
+ end
12
+
13
+ describe Bunyan::Logger::Config, 'collection size' do
14
+ it 'should default to 50 megabytes' do
15
+ # configure_test_db
16
+ config = Bunyan::Logger::Config.new
17
+ config.size.should == 52428800
18
+ end
19
+
20
+ it 'should all the user to set the collection size' do
21
+ Bunyan::Logger.configure do |c|
22
+ c.database 'bunyan_test_log'
23
+ c.collection 'configured_size'
24
+ c.size 100_000_000
25
+ end
26
+
27
+ Bunyan::Logger.config[:size].should == 100_000_000
28
+ end
29
+ end
30
+
31
+ describe Bunyan::Logger::Config, 'when setting the collection size' do
32
+ it 'should never set the size to nil' do
33
+ @config = Bunyan::Logger::Config.new
34
+ @config.size nil
35
+ @config.size.should == 52428800
36
+ end
37
+
38
+ it 'should override the default value' do
39
+ @config = Bunyan::Logger::Config.new
40
+ @config.size 1010
41
+ @config.size.should == 1010
42
+ end
43
+ end
44
+
45
+ describe Bunyan::Logger::Config, 'when getting the collection size' do
46
+ it 'should return the collection size'
47
+ end
48
+
49
+ describe Bunyan::Logger::Config, 'alternate method invocation syntax' do
50
+ it 'should act like a hash' do
51
+ config = Bunyan::Logger::Config.new
52
+ config.size 10
53
+ config.should_receive(:size).and_return(10)
54
+ config[:size].should == 10
55
+ end
56
+ end
57
+
58
+ describe 'bunyan logger configuration' do
59
+ describe 'setting config values' do
60
+ before do
61
+ Bunyan::Logger.configure do |c|
62
+ c.database 'database2'
63
+ c.collection 'collection2'
64
+ end
65
+ end
66
+
67
+ it 'should allow setting of the database' do
68
+ Bunyan::Logger.config.database.should == 'database2'
69
+ end
70
+
71
+ it 'shoudl allow setting of the collection name' do
72
+ Bunyan::Logger.config.collection.should == 'collection2'
73
+ end
74
+ end
75
+
76
+ describe 'the optional config options' do
77
+ it 'should allow the user to mark bunyan as disabled' do
78
+ Bunyan::Logger.configure do |c|
79
+ c.database 'test_db'
80
+ c.collection 'test_collection'
81
+ c.disabled true
82
+ end
83
+ Bunyan::Logger.should be_disabled
84
+ end
85
+ end
86
+
87
+ describe "when the disabled flag is set" do
88
+ it 'should not create a new logger instance' do
89
+ Bunyan::Logger.should_not_receive(:initialize_connection)
90
+ Bunyan::Logger.configure do |c|
91
+ c.database 'test_db'
92
+ c.collection 'test_collection'
93
+ c.disabled true
94
+ end
95
+ end
96
+ end
97
+ end
98
+
@@ -0,0 +1,14 @@
1
+ # $LOAD_PATH.unshift(File.expand_path(File.dirname('../../lib/bunyan'))
2
+ #
3
+ # require 'bunyan'
4
+ #
5
+ # Bunyan::Logger.configure do |c|
6
+ # c.database 'bunyan_integration_db'
7
+ # c.collection 'test_log'
8
+ #
9
+ # c.size 104857600 # 100.megabytes
10
+ # end
11
+ #
12
+ # describe
13
+ #
14
+ # Bunyan::Logger.insert({ })
data/spec/spec_helper.rb CHANGED
@@ -1,8 +1,10 @@
1
+ require 'rubygems'
2
+ require 'bundler'
1
3
  Bundler.setup
2
4
  Bundler.require :default, :test
3
5
 
4
6
  require File.expand_path(File.dirname(__FILE__) + '/../lib/bunyan')
5
- $LOAD_PATH.unshift File.expand_path(__FILE__)
7
+ $LOAD_PATH.unshift File.expand_path(File.dirname(__FILE__))
6
8
 
7
9
  Spec::Runner.configure do |config|
8
10
 
@@ -16,18 +18,25 @@ Spec::Runner.configure do |config|
16
18
 
17
19
  def mock_mongo_connection
18
20
  @mock_collection = mock("Mongo Collection")
21
+ @mock_connection = mock("Mongo Connection")
19
22
  @mock_database = mock("Mongo Database",
20
23
  :collection => @mock_collection,
21
24
  :create_collection => @mock_collection,
22
- :collection_names => ['name 1'])
23
- @mock_connection = mock("Mongo Connection", :db => @mock_database)
25
+ :collection_names => ['name 1'],
26
+ :connection => @mock_connection)
27
+ @mock_connection.stub!(:db).and_return(@mock_database)
24
28
  Mongo::Connection.stub!(:new).and_return(@mock_connection)
25
29
  @mock_database
26
30
  end
27
31
 
28
32
  def cleanup_bunyan_config
29
- Bunyan::Logger.instance_variable_set(:@database, nil)
30
- Bunyan::Logger.instance_variable_set(:@collection, nil)
31
- Bunyan::Logger.instance_variable_set(:@db, nil)
33
+ Bunyan::Logger.instance_variable_set(:@config, Bunyan::Logger::Config.new)
34
+ end
35
+
36
+ def configure_test_db
37
+ Bunyan::Logger.configure do |config|
38
+ config.database 'bunyan_test'
39
+ config.collection 'bunyan_test_log'
40
+ end
32
41
  end
33
42
  end
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 1
7
+ - 2
8
8
  - 0
9
- version: 0.1.0
9
+ version: 0.2.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - Alex Sharp
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-03-17 00:00:00 -07:00
17
+ date: 2010-03-20 00:00:00 -07:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -53,13 +53,17 @@ extra_rdoc_files:
53
53
  - README.md
54
54
  files:
55
55
  - .gitignore
56
+ - CHANGELOG.md
56
57
  - Gemfile
57
58
  - README.md
58
59
  - Rakefile
59
60
  - VERSION
60
61
  - bunyan.gemspec
62
+ - examples/rails.rb
61
63
  - lib/bunyan.rb
64
+ - lib/bunyan/config.rb
62
65
  - spec/bunyan_spec.rb
66
+ - spec/config_spec.rb
63
67
  - spec/spec.opts
64
68
  - spec/spec_helper.rb
65
69
  has_rdoc: true
@@ -94,4 +98,7 @@ specification_version: 3
94
98
  summary: A MongoDB-based logging solution.
95
99
  test_files:
96
100
  - spec/bunyan_spec.rb
101
+ - spec/config_spec.rb
102
+ - spec/integration/non_rails_spec.rb
97
103
  - spec/spec_helper.rb
104
+ - examples/rails.rb