central_logger 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile CHANGED
@@ -5,12 +5,6 @@ gem "bundler", "~> 1.0.0"
5
5
  gem "mongo"
6
6
  gem "bson_ext"
7
7
 
8
- group :production do
9
- # don't want to force development to have this requirement, because every
10
- # Rails dependency is mocked
11
- gem "rails",">= 2.3.8"
12
- end
13
-
14
8
  group :development do
15
9
  # adds Bundler support for gemspec generation
16
10
  gem "jeweler", "~> 1.5.0.pre5"
data/Gemfile.lock CHANGED
@@ -1,42 +1,11 @@
1
1
  GEM
2
2
  remote: http://rubygems.org/
3
3
  specs:
4
- abstract (1.0.0)
5
- actionmailer (3.0.1)
6
- actionpack (= 3.0.1)
7
- mail (~> 2.2.5)
8
- actionpack (3.0.1)
9
- activemodel (= 3.0.1)
10
- activesupport (= 3.0.1)
11
- builder (~> 2.1.2)
12
- erubis (~> 2.6.6)
13
- i18n (~> 0.4.1)
14
- rack (~> 1.2.1)
15
- rack-mount (~> 0.6.12)
16
- rack-test (~> 0.5.4)
17
- tzinfo (~> 0.3.23)
18
- activemodel (3.0.1)
19
- activesupport (= 3.0.1)
20
- builder (~> 2.1.2)
21
- i18n (~> 0.4.1)
22
- activerecord (3.0.1)
23
- activemodel (= 3.0.1)
24
- activesupport (= 3.0.1)
25
- arel (~> 1.0.0)
26
- tzinfo (~> 0.3.23)
27
- activeresource (3.0.1)
28
- activemodel (= 3.0.1)
29
- activesupport (= 3.0.1)
30
4
  activesupport (3.0.1)
31
5
  archive-tar-minitar (0.5.2)
32
- arel (1.0.1)
33
- activesupport (~> 3.0.0)
34
- bson (1.1.1)
35
- bson_ext (1.1.1)
36
- builder (2.1.2)
37
- columnize (0.3.1)
38
- erubis (2.6.6)
39
- abstract (>= 1.0.0)
6
+ bson (1.1.2)
7
+ bson_ext (1.1.2)
8
+ columnize (0.3.2)
40
9
  git (1.2.5)
41
10
  i18n (0.4.2)
42
11
  jeweler (1.5.0.pre5)
@@ -45,35 +14,10 @@ GEM
45
14
  rake
46
15
  linecache19 (0.5.11)
47
16
  ruby_core_source (>= 0.1.4)
48
- mail (2.2.9)
49
- activesupport (>= 2.3.6)
50
- i18n (~> 0.4.1)
51
- mime-types (~> 1.16)
52
- treetop (~> 1.4.8)
53
- mime-types (1.16)
54
17
  mocha (0.9.9)
55
18
  rake
56
- mongo (1.1.1)
19
+ mongo (1.1.2)
57
20
  bson (>= 1.1.1)
58
- polyglot (0.3.1)
59
- rack (1.2.1)
60
- rack-mount (0.6.13)
61
- rack (>= 1.0.0)
62
- rack-test (0.5.6)
63
- rack (>= 1.0)
64
- rails (3.0.1)
65
- actionmailer (= 3.0.1)
66
- actionpack (= 3.0.1)
67
- activerecord (= 3.0.1)
68
- activeresource (= 3.0.1)
69
- activesupport (= 3.0.1)
70
- bundler (~> 1.0.0)
71
- railties (= 3.0.1)
72
- railties (3.0.1)
73
- actionpack (= 3.0.1)
74
- activesupport (= 3.0.1)
75
- rake (>= 0.8.4)
76
- thor (~> 0.14.0)
77
21
  rake (0.8.7)
78
22
  ruby-debug-base19 (0.11.24)
79
23
  columnize (>= 0.3.1)
@@ -86,10 +30,6 @@ GEM
86
30
  ruby_core_source (0.1.4)
87
31
  archive-tar-minitar (>= 0.5.2)
88
32
  shoulda (2.11.3)
89
- thor (0.14.3)
90
- treetop (1.4.8)
91
- polyglot (>= 0.3.1)
92
- tzinfo (0.3.23)
93
33
 
94
34
  PLATFORMS
95
35
  ruby
@@ -102,7 +42,6 @@ DEPENDENCIES
102
42
  jeweler (~> 1.5.0.pre5)
103
43
  mocha
104
44
  mongo
105
- rails (>= 2.3.8)
106
45
  rake
107
46
  ruby-debug19
108
47
  shoulda
data/README.md CHANGED
@@ -21,7 +21,11 @@ Log to a central MongoDB from Rails apps.
21
21
  require 'central_logger'
22
22
  CentralLogger::Initializer.initialize_deprecated_logger(config)
23
23
 
24
- 1. Add mongo settings to database.yml for each environment in which you want to use the Central Logger for logging. The values below are defaults:
24
+ 1. Add mongo settings to database.yml for each environment in which you want to use the Central Logger. The central logger will also
25
+ look for a separate central_logger.yml or mongoid.yml (if you are using mongoid) before looking in database.yml.
26
+ In the central_logger.yml and mongoid.yml case, the settings should be defined without the 'mongo' subkey.
27
+
28
+ database.yml:
25
29
 
26
30
  development:
27
31
  adapter: mysql
@@ -33,13 +37,21 @@ Log to a central MongoDB from Rails apps.
33
37
  host: localhost # default: localhost
34
38
  port: 27017 # default: 27017
35
39
 
40
+ central_logger.yml:
41
+
42
+ development:
43
+ database: my_app
44
+ capsize: <%= 10.megabytes %>
45
+ host: localhost
46
+ port: 27017
47
+
36
48
  With that in place, a new MongoDB document (record) will be created for each request and,
37
49
  by default will record the following information: Runtime, IP Address, Request Time, Controller,
38
50
  Action, Params, Application Name and All messages sent to the logger. The structure of the Mongo document looks like this:
39
51
 
40
52
  {
41
53
  'action' : action_name,
42
- 'application_name' : action_name,
54
+ 'application_name' : application_name (rails root),
43
55
  'controller' : controller_name,
44
56
  'ip' : ip_address,
45
57
  'messages' : {
@@ -97,4 +109,8 @@ Find all requests with an exception that contains "RoutingError" in the message
97
109
 
98
110
  >> collection.find({"messages.error" => /RoutingError/})
99
111
 
112
+ Find all requests with a request_date greater than '11/18/2010 22:59:52 GMT'
113
+
114
+ >> collection.find({:request_time => {'$gt' => Time.utc(2010, 11, 18, 22, 59, 52)}})
115
+
100
116
  Copyright (c) 2009 Phil Burrows, released under the MIT license
data/Rakefile CHANGED
@@ -6,8 +6,8 @@ require 'bundler'
6
6
  require 'jeweler'
7
7
 
8
8
  desc 'Default: run unit tests.'
9
- task :default => :test
10
- task :test => :check_dependencies
9
+ task :default => "test:units"
10
+ task :test => "test:functionals"
11
11
 
12
12
  begin
13
13
  Bundler.setup(:default, :development)
@@ -24,13 +24,39 @@ Jeweler::Tasks.new do |gem|
24
24
  gem.email = "astupka@customink.com"
25
25
  gem.homepage = "http://github.com/customink/central_logger"
26
26
  gem.authors = ["Phil Burrows", "Alex Stupka"]
27
+ gem.files.exclude 'test/rails/**/*'
28
+ gem.test_files.exclude 'test/rails/**/*'
27
29
  end
28
30
  # dependencies defined in Gemfile
29
31
 
30
- Rake::TestTask.new(:test) do |test|
31
- test.libs << 'lib' << 'test'
32
- test.pattern = 'test/**/*_test.rb'
33
- test.verbose = true
32
+ def rake_functionals(opts=nil)
33
+ if ENV['RUBYOPT']
34
+ # remove bundler/setup require that prematurely checks for gems and crashes
35
+ ENV['RUBYOPT'] = ENV['RUBYOPT'].gsub(%r{-r\s*bundler/setup}, '')
36
+ end
37
+ # runs all the tests for each ruby version in each rails dir
38
+ system("bash test/test.sh #{opts}")
39
+ end
40
+
41
+ namespace :test do
42
+ desc "Run all tests against all permutations of ruby and rails"
43
+ task :functionals do
44
+ rake_functionals
45
+ end
46
+
47
+ namespace :functionals do
48
+ desc "Clean out gemsets before running functional tests."
49
+ task :clean do
50
+ rake_functionals('--clean')
51
+ end
52
+ end
53
+
54
+ desc "Run unit tests"
55
+ Rake::TestTask.new(:units) do |test|
56
+ test.libs << 'lib' << 'test'
57
+ test.pattern = 'test/unit/*_test.rb'
58
+ test.verbose = true
59
+ end
34
60
  end
35
61
 
36
62
  Rake::RDocTask.new do |rdoc|
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.1
1
+ 0.2.0
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{central_logger}
8
- s.version = "0.1.1"
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 = ["Phil Burrows", "Alex Stupka"]
12
- s.date = %q{2010-11-03}
12
+ s.date = %q{2010-11-30}
13
13
  s.description = %q{Centralized logging for rails apps using MongoDB. The idea and the core code is from http://github.com/peburrows/central_logger}
14
14
  s.email = %q{astupka@customink.com}
15
15
  s.extra_rdoc_files = [
@@ -29,11 +29,15 @@ Gem::Specification.new do |s|
29
29
  "lib/central_logger/initializer.rb",
30
30
  "lib/central_logger/initializer_mixin.rb",
31
31
  "lib/central_logger/mongo_logger.rb",
32
- "lib/railtie.rb",
32
+ "lib/central_logger/railtie.rb",
33
33
  "test/active_record.rb",
34
- "test/config/database.yml",
34
+ "test/config/samples/central_logger.yml",
35
+ "test/config/samples/database.yml",
36
+ "test/config/samples/database_with_auth.yml",
37
+ "test/config/samples/mongoid.yml",
35
38
  "test/rails.rb",
36
39
  "test/shoulda_macros/log_macros.rb",
40
+ "test/test.sh",
37
41
  "test/test_helper.rb",
38
42
  "test/unit/central_logger_test.rb"
39
43
  ]
@@ -4,5 +4,5 @@ require 'mongo'
4
4
  require 'central_logger/mongo_logger'
5
5
  require 'central_logger/filter'
6
6
  require 'central_logger/initializer'
7
- require 'railtie'
7
+ require 'central_logger/railtie'
8
8
 
@@ -7,8 +7,12 @@ module CentralLogger
7
7
  def enable_central_logger
8
8
  return yield unless Rails.logger.respond_to?(:mongoize)
9
9
 
10
- # make sure the controller knows how to filter its parameters
11
- f_params = respond_to?(:filter_parameters) ? filter_parameters(params) : params
10
+ # make sure the controller knows how to filter its parameters (Rails 3, 2, respectively)
11
+ f_params = case
12
+ when request.respond_to?(:filtered_parameters) then request.filtered_parameters
13
+ when respond_to?(:filter_parameters) then filter_parameters(params)
14
+ else params
15
+ end
12
16
  Rails.logger.mongoize({
13
17
  :action => action_name,
14
18
  :controller => controller_name,
@@ -7,6 +7,8 @@ module CentralLogger
7
7
  class MongoLogger < ActiveSupport::BufferedLogger
8
8
  PRODUCTION_COLLECTION_SIZE = 250.megabytes
9
9
  DEFAULT_COLLECTION_SIZE = 100.megabytes
10
+ # Looks for configuration files in this order
11
+ CONFIGURATION_FILES = ["central_logger.yml", "mongoid.yml", "database.yml"]
10
12
 
11
13
  attr_reader :db_configuration, :mongo_connection, :mongo_collection_name
12
14
 
@@ -16,9 +18,8 @@ module CentralLogger
16
18
  super(path, level)
17
19
  internal_initialize
18
20
  rescue => e
19
- # in case the logger is fouled up use stdout
20
- puts "=> !! A connection to mongo could not be established - the logger will function like a normal ActiveSupport::BufferedLogger !!"
21
- puts e.message + "\n" + e.backtrace.join("\n")
21
+ # should use a config block for this
22
+ Rails.env.production? ? (raise e) : (puts "Using BufferedLogger due to exception: " + e.message)
22
23
  end
23
24
 
24
25
  def add_metadata(options={})
@@ -64,6 +65,10 @@ module CentralLogger
64
65
  insert_log_record(runtime)
65
66
  end
66
67
 
68
+ def authenticated?
69
+ @authenticated
70
+ end
71
+
67
72
  private
68
73
  # facilitate testing
69
74
  def internal_initialize
@@ -74,21 +79,38 @@ module CentralLogger
74
79
 
75
80
  def configure
76
81
  default_capsize = Rails.env.production? ? PRODUCTION_COLLECTION_SIZE : DEFAULT_COLLECTION_SIZE
77
- user_config = YAML::load(ERB.new(IO.read(File.join(Rails.root, 'config/database.yml'))).result)[Rails.env]['mongo'] || {}
78
82
  @application_name = Rails.root.basename.to_s
79
-
80
83
  @mongo_collection_name = "#{Rails.env}_log"
81
-
84
+ @authenticated = false
82
85
  @db_configuration = {
83
86
  'host' => 'localhost',
84
87
  'port' => 27017,
85
- 'capsize' => default_capsize}.merge(user_config)
88
+ 'capsize' => default_capsize}.merge(resolve_config)
89
+ end
90
+
91
+ def resolve_config
92
+ config = {}
93
+ CONFIGURATION_FILES.each do |filename|
94
+ config_file = Rails.root.join("config", filename)
95
+ if config_file.file?
96
+ config = YAML.load(ERB.new(config_file.read).result)[Rails.env]
97
+ config = config['mongo'] if config.has_key?('mongo')
98
+ break
99
+ end
100
+ end
101
+ config
86
102
  end
87
103
 
88
104
  def connect
89
105
  @mongo_connection ||= Mongo::Connection.new(@db_configuration['host'],
90
106
  @db_configuration['port'],
91
107
  :auto_reconnect => true).db(@db_configuration['database'])
108
+
109
+ if @db_configuration['username'] && @db_configuration['password']
110
+ # the driver stores credentials in case reconnection is required
111
+ @authenticated = @mongo_connection.authenticate(@db_configuration['username'],
112
+ @db_configuration['password'])
113
+ end
92
114
  end
93
115
 
94
116
  def create_collection
@@ -120,7 +142,7 @@ module CentralLogger
120
142
  end
121
143
 
122
144
  def logging_colorized?
123
- # Cache it since these ActiveRecord attributes are assigned after logger initialization occurs
145
+ # Cache it since these ActiveRecord attributes are assigned after logger initialization occurs in Rails boot
124
146
  @colorized ||= Object.const_defined?(:ActiveRecord) &&
125
147
  (Rails::VERSION::MAJOR >= 3 ?
126
148
  ActiveRecord::LogSubscriber.colorize_logging :
File without changes
@@ -0,0 +1,2 @@
1
+ test:
2
+ database: system_log
File without changes
@@ -0,0 +1,8 @@
1
+ test:
2
+ adapter: mysql
3
+ username: user
4
+ database: database
5
+ mongo:
6
+ database: system_log
7
+ username: admin
8
+ password: password
@@ -0,0 +1,31 @@
1
+ # taken from http://mongoid.org/docs/installation/
2
+ defaults: &defaults
3
+ host: localhost
4
+ slaves:
5
+ - host: slave1.local
6
+ port: 27018
7
+ - host: slave2.local
8
+ port: 27019
9
+ autocreate_indexes: false
10
+ allow_dynamic_fields: true
11
+ include_root_in_json: false
12
+ parameterize_keys: true
13
+ persist_in_safe_mode: false
14
+ raise_not_found_error: true
15
+ reconnect_time: 3
16
+
17
+ development:
18
+ <<: *defaults
19
+ database: control_development
20
+
21
+ test:
22
+ <<: *defaults
23
+ database: system_log
24
+
25
+ # set these environment variables on your prod server
26
+ production:
27
+ <<: *defaults
28
+ host: <%= ENV['MONGOID_HOST'] %>
29
+ port: <%= ENV['MONGOID_PORT'] %>
30
+ database: <%= ENV['MONGOID_DATABASE'] %>
31
+
@@ -4,4 +4,10 @@ module LogMacros
4
4
  assert_equal 1, @con[@central_logger.mongo_collection_name].count
5
5
  end
6
6
  end
7
+
8
+ def should_use_database_name_in_config
9
+ should "use the database name in the config file" do
10
+ assert_equal "system_log", @central_logger.db_configuration['database']
11
+ end
12
+ end
7
13
  end
data/test/test.sh ADDED
@@ -0,0 +1,91 @@
1
+ #!/bin/bash
2
+ # This script uses rvm and gemsets to test the gem against all versions of ruby
3
+ # (RUBY_VERSIONS) and all rails versions in the test/rails directories
4
+ # (RAILS_VERSIONS).
5
+
6
+ # If these aren't unset, bundler always uses them to find itself and the
7
+ # root project gemfile
8
+ unset BUNDLE_BIN_PATH
9
+ unset BUNDLE_GEMFILE
10
+
11
+ RAILS_VERSIONS='2 3'
12
+ RUBY_VERSIONS='1.8.7 1.9.2'
13
+ PROJECT_GEMSET='central_logger'
14
+ RAILS_DIR=rails
15
+ RAILS_RAKE_TASK_MODIFIER=':functionals'
16
+ RAKE_UNIT_TASK_MODIFIER=':units'
17
+ CLEAN=0
18
+ VALID_OPT='--clean'
19
+
20
+ function usage() {
21
+ echo 'Usage: ' $0 "[${VALID_OPT}]"
22
+ echo 'Options:' $VALID_OPT 'Delete and recreate all gemsets before running tests'
23
+ exit 1
24
+ }
25
+
26
+ ARG=$1
27
+ if [ $# -gt 1 ] || [ ${ARG:-$VALID_OPT} != $VALID_OPT ]; then
28
+ usage
29
+ elif [ -n "$1" ]; then
30
+ CLEAN=1
31
+ fi
32
+
33
+ # Load RVM into a shell session *as a function*
34
+ if [[ -s "$HOME/.rvm/scripts/rvm" ]]; then
35
+ # First try to load from a user install
36
+ source "$HOME/.rvm/scripts/rvm"
37
+ elif [[ -s "/usr/local/rvm/scripts/rvm" ]]; then
38
+ # Then try to load from a root install
39
+ source "/usr/local/rvm/scripts/rvm"
40
+ else
41
+ printf "ERROR: An RVM installation was not found.\n"
42
+ exit 1
43
+ fi
44
+
45
+ # deletes and recreates a gemset
46
+ function clean_gemset() {
47
+ RUBY=$1; GEMSET=$2
48
+ # easier than checking and emptying/creating
49
+ rvm --force gemset delete ${RUBY}@${GEMSET}
50
+ rvm gemset create $GEMSET
51
+ }
52
+
53
+ # runs rake test, optionally cleaning the gemset
54
+ function rake_test() {
55
+ RUBY=$1; GEMSET=$2; SUBTASK=$3
56
+ echo
57
+ echo "-----------Running tests for '${RUBY}@${GEMSET}'------------"
58
+ rvm $RUBY
59
+
60
+ if [ $CLEAN -eq 1 ]; then
61
+ clean_gemset $RUBY $GEMSET
62
+ rvm gemset use $GEMSET
63
+ bundle install
64
+ else
65
+ rvm gemset use $GEMSET
66
+ fi
67
+
68
+ # 'rvm $RUBY_VER rake' doesn't work with bundle install
69
+ rake test${SUBTASK}
70
+ if [ $? -ne 0 ]; then exit 1; fi
71
+ }
72
+
73
+ # Loop through all perms of rubies and rails
74
+ cd test
75
+ for RBV in $RUBY_VERSIONS; do
76
+ rake_test $RBV $PROJECT_GEMSET $RAKE_UNIT_TASK_MODIFIER
77
+
78
+ NEXT_DIR=$RAILS_DIR
79
+ for RV in $RAILS_VERSIONS; do
80
+ if [ "$NEXT_DIR" == "$RAILS_DIR" ]; then
81
+ NEXT_DIR=${NEXT_DIR}/${RV}
82
+ else
83
+ NEXT_DIR=../${RV}
84
+ fi
85
+ cd $NEXT_DIR
86
+
87
+ rake_test $RBV ${PROJECT_GEMSET}_${RV} $RAILS_RAKE_TASK_MODIFIER
88
+ done
89
+
90
+ cd ../..
91
+ done
data/test/test_helper.rb CHANGED
@@ -4,6 +4,7 @@ require 'mocha'
4
4
  # mock rails class
5
5
  require 'pathname'
6
6
  require 'rails'
7
+ require 'fileutils'
7
8
 
8
9
  $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
9
10
  $LOAD_PATH.unshift(File.dirname(__FILE__))
@@ -11,6 +12,13 @@ $LOAD_PATH.unshift(File.dirname(__FILE__))
11
12
  Shoulda.autoload_macros("#{File.dirname(__FILE__)}/..")
12
13
 
13
14
  class Test::Unit::TestCase
15
+ CONFIG_DIR = Rails.root.join("config")
16
+ SAMPLE_CONFIG_DIR = File.join(CONFIG_DIR, "samples")
17
+ DEFAULT_CONFIG = "database.yml"
18
+ DEFAULT_CONFIG_WITH_AUTH = "database_with_auth.yml"
19
+ MONGOID_CONFIG = "mongoid.yml"
20
+ LOGGER_CONFIG = "central_logger.yml"
21
+
14
22
  def log(msg)
15
23
  @central_logger.mongoize({"id" => 1}) do
16
24
  @central_logger.debug(msg)
@@ -23,6 +31,16 @@ class Test::Unit::TestCase
23
31
  end
24
32
  end
25
33
 
34
+ def setup_for_config(source, dest=source)
35
+ File.delete(File.join(CONFIG_DIR, DEFAULT_CONFIG))
36
+ FileUtils.cp(File.join(SAMPLE_CONFIG_DIR, source), File.join(CONFIG_DIR, dest))
37
+ @central_logger.send(:configure)
38
+ end
39
+
40
+ def teardown_for_config(file)
41
+ File.delete(File.join(CONFIG_DIR, file))
42
+ end
43
+
26
44
  def log_metadata(options)
27
45
  @central_logger.mongoize({"id" => 1}) do
28
46
  @central_logger.add_metadata(options)
@@ -37,4 +55,17 @@ class Test::Unit::TestCase
37
55
  @con = @central_logger.mongo_connection
38
56
  @collection = @con[@central_logger.mongo_collection_name]
39
57
  end
58
+
59
+ def create_user
60
+ db_conf = @central_logger.db_configuration
61
+ @user = db_conf['username']
62
+ mongo_connection = Mongo::Connection.new(db_conf['host'],
63
+ db_conf['port']).db(db_conf['database'])
64
+ mongo_connection.add_user(@user, db_conf['password'])
65
+ end
66
+
67
+ def remove_user
68
+ @central_logger.mongo_connection.remove_user(@user)
69
+ end
70
+
40
71
  end
@@ -8,39 +8,99 @@ class CentralLogger::MongoLoggerTest < Test::Unit::TestCase
8
8
  EXCEPTION_MSG = "Foo"
9
9
 
10
10
  context "A CentralLogger::MongoLogger" do
11
- context "during configuration in instantiation" do
11
+ setup do
12
+ # Can use different configs, but most tests use database.yml
13
+ FileUtils.cp(File.join(SAMPLE_CONFIG_DIR, DEFAULT_CONFIG), CONFIG_DIR)
14
+ end
15
+
16
+ context "in instantiation" do
12
17
  setup do
13
18
  CentralLogger::MongoLogger.any_instance.stubs(:internal_initialize).returns(nil)
14
19
  @central_logger = CentralLogger::MongoLogger.new
15
- @central_logger.send(:configure)
16
20
  end
17
21
 
18
- should "set the default host, port, and capsize if not configured" do
19
- assert_equal 'localhost', @central_logger.db_configuration['host']
20
- assert_equal 27017, @central_logger.db_configuration['port']
21
- assert_equal CentralLogger::MongoLogger::DEFAULT_COLLECTION_SIZE, @central_logger.db_configuration['capsize']
22
+ context "during configuration when using a separate " + LOGGER_CONFIG do
23
+ setup do
24
+ setup_for_config(LOGGER_CONFIG)
25
+ end
26
+
27
+ should_use_database_name_in_config
28
+
29
+ teardown do
30
+ teardown_for_config(LOGGER_CONFIG)
31
+ end
22
32
  end
23
33
 
24
- should "set the mongo collection name depending on the Rails environment" do
25
- assert_equal "#{Rails.env}_log", @central_logger.mongo_collection_name
34
+ context "during configuration when using a separate " + MONGOID_CONFIG do
35
+ setup do
36
+ setup_for_config(MONGOID_CONFIG)
37
+ end
38
+
39
+ should_use_database_name_in_config
40
+
41
+ teardown do
42
+ teardown_for_config(MONGOID_CONFIG)
43
+ end
26
44
  end
27
45
 
28
- context "upon connecting to an empty database" do
46
+ # this test will work without the --auth mongod arg
47
+ context "upon connecting with authentication settings" do
29
48
  setup do
49
+ setup_for_config(DEFAULT_CONFIG_WITH_AUTH, DEFAULT_CONFIG)
50
+ create_user
51
+ end
52
+
53
+ should "authenticate with the credentials in the configuration" do
30
54
  @central_logger.send(:connect)
31
- common_setup
32
- @collection.drop
55
+ assert @central_logger.authenticated?
56
+ end
57
+
58
+ teardown do
59
+ # config will be deleted by outer teardown
60
+ remove_user
61
+ end
62
+ end
63
+
64
+ context "after configuration" do
65
+ setup do
66
+ @central_logger.send(:configure)
33
67
  end
34
68
 
35
- should "expose a valid mongo connection" do
36
- assert_instance_of Mongo::DB, @central_logger.mongo_connection
69
+ should "set the default host, port, and capsize if not configured" do
70
+ assert_equal 'localhost', @central_logger.db_configuration['host']
71
+ assert_equal 27017, @central_logger.db_configuration['port']
72
+ assert_equal CentralLogger::MongoLogger::DEFAULT_COLLECTION_SIZE, @central_logger.db_configuration['capsize']
37
73
  end
38
74
 
39
- should "create a capped collection in the database with the configured size" do
40
- @central_logger.send(:check_for_collection)
41
- assert @con.collection_names.include?(@central_logger.mongo_collection_name)
42
- # new capped collections are X MB + 5888 bytes, but don't be too strict in case that changes
43
- assert @collection.stats["storageSize"] < CentralLogger::MongoLogger::DEFAULT_COLLECTION_SIZE + 1.megabyte
75
+ should "set the mongo collection name depending on the Rails environment" do
76
+ assert_equal "#{Rails.env}_log", @central_logger.mongo_collection_name
77
+ end
78
+
79
+ should "use the database name in the config file" do
80
+ assert_equal "system_log", @central_logger.db_configuration['database']
81
+ end
82
+
83
+ context "upon connecting to an empty database" do
84
+ setup do
85
+ @central_logger.send(:connect)
86
+ common_setup
87
+ @collection.drop
88
+ end
89
+
90
+ should "expose a valid mongo connection" do
91
+ assert_instance_of Mongo::DB, @central_logger.mongo_connection
92
+ end
93
+
94
+ should "not authenticate" do
95
+ assert !@central_logger.authenticated?
96
+ end
97
+
98
+ should "create a capped collection in the database with the configured size" do
99
+ @central_logger.send(:check_for_collection)
100
+ assert @con.collection_names.include?(@central_logger.mongo_collection_name)
101
+ # new capped collections are X MB + 5888 bytes, but don't be too strict in case that changes
102
+ assert @collection.stats["storageSize"] < CentralLogger::MongoLogger::DEFAULT_COLLECTION_SIZE + 1.megabyte
103
+ end
44
104
  end
45
105
  end
46
106
  end
@@ -112,6 +172,9 @@ class CentralLogger::MongoLoggerTest < Test::Unit::TestCase
112
172
  assert_equal 0, @collection.find_one({}, :fields => ["messages"])["messages"].count
113
173
  end
114
174
  end
115
-
175
+ teardown do
176
+ file = File.join(CONFIG_DIR, DEFAULT_CONFIG)
177
+ File.delete(file) if File.exist?(file)
178
+ end
116
179
  end
117
180
  end
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 1
8
- - 1
9
- version: 0.1.1
7
+ - 2
8
+ - 0
9
+ version: 0.2.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - Phil Burrows
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-11-03 00:00:00 -04:00
18
+ date: 2010-11-30 00:00:00 -05:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -175,11 +175,15 @@ files:
175
175
  - lib/central_logger/initializer.rb
176
176
  - lib/central_logger/initializer_mixin.rb
177
177
  - lib/central_logger/mongo_logger.rb
178
- - lib/railtie.rb
178
+ - lib/central_logger/railtie.rb
179
179
  - test/active_record.rb
180
- - test/config/database.yml
180
+ - test/config/samples/central_logger.yml
181
+ - test/config/samples/database.yml
182
+ - test/config/samples/database_with_auth.yml
183
+ - test/config/samples/mongoid.yml
181
184
  - test/rails.rb
182
185
  - test/shoulda_macros/log_macros.rb
186
+ - test/test.sh
183
187
  - test/test_helper.rb
184
188
  - test/unit/central_logger_test.rb
185
189
  has_rdoc: true