mongodb_logger 0.2.6-jruby

Sign up to get free protection for your applications and to get access to all the features.
Files changed (118) hide show
  1. data/.gitignore +20 -0
  2. data/.rvmrc +1 -0
  3. data/.travis.yml +19 -0
  4. data/CHANGELOG.md +30 -0
  5. data/Gemfile +10 -0
  6. data/LICENSE +22 -0
  7. data/README.md +207 -0
  8. data/Rakefile +169 -0
  9. data/SUPPORTED_RAILS_VERSIONS +17 -0
  10. data/TESTING.md +24 -0
  11. data/bin/mongodb_logger_web +24 -0
  12. data/config.ru +17 -0
  13. data/examples/server_config.yml +5 -0
  14. data/features/mongodb_logger_web.feature +14 -0
  15. data/features/rails.feature +12 -0
  16. data/features/step_definitions/mongodb_logger_web_steps.rb +45 -0
  17. data/features/step_definitions/rails_application_steps.rb +65 -0
  18. data/features/support/env.rb +15 -0
  19. data/features/support/rails.rb +98 -0
  20. data/features/support/terminal.rb +95 -0
  21. data/lib/mongodb_logger/initializer_mixin.rb +26 -0
  22. data/lib/mongodb_logger/logger.rb +239 -0
  23. data/lib/mongodb_logger/railtie.rb +12 -0
  24. data/lib/mongodb_logger/replica_set_helper.rb +19 -0
  25. data/lib/mongodb_logger/server/coffee/logs.coffee +250 -0
  26. data/lib/mongodb_logger/server/content_for.rb +58 -0
  27. data/lib/mongodb_logger/server/model/additional_filter.rb +104 -0
  28. data/lib/mongodb_logger/server/model/analytic.rb +82 -0
  29. data/lib/mongodb_logger/server/model/filter.rb +84 -0
  30. data/lib/mongodb_logger/server/partials.rb +24 -0
  31. data/lib/mongodb_logger/server/public/images/arrow-down.png +0 -0
  32. data/lib/mongodb_logger/server/public/images/arrow-up.png +0 -0
  33. data/lib/mongodb_logger/server/public/images/date.png +0 -0
  34. data/lib/mongodb_logger/server/public/images/external.png +0 -0
  35. data/lib/mongodb_logger/server/public/images/failure.png +0 -0
  36. data/lib/mongodb_logger/server/public/images/logo.png +0 -0
  37. data/lib/mongodb_logger/server/public/images/mongodb.png +0 -0
  38. data/lib/mongodb_logger/server/public/images/newlog.png +0 -0
  39. data/lib/mongodb_logger/server/public/images/play-icon.png +0 -0
  40. data/lib/mongodb_logger/server/public/images/spinner.gif +0 -0
  41. data/lib/mongodb_logger/server/public/images/spinner2.gif +0 -0
  42. data/lib/mongodb_logger/server/public/images/stop-icon.png +0 -0
  43. data/lib/mongodb_logger/server/public/images/success.png +0 -0
  44. data/lib/mongodb_logger/server/public/javascripts/logs.js +1 -0
  45. data/lib/mongodb_logger/server/public/javascripts/vendors/highlight.pack.js +1 -0
  46. data/lib/mongodb_logger/server/public/javascripts/vendors/jquery-1.7.1.min.js +4 -0
  47. data/lib/mongodb_logger/server/public/javascripts/vendors/jquery-ui-1.8.16.min.js +791 -0
  48. data/lib/mongodb_logger/server/public/javascripts/vendors/jquery.pjax.min.js +6 -0
  49. data/lib/mongodb_logger/server/public/stylesheets/all.css +12 -0
  50. data/lib/mongodb_logger/server/public/stylesheets/grids.css +18 -0
  51. data/lib/mongodb_logger/server/public/stylesheets/group-buttons.css +81 -0
  52. data/lib/mongodb_logger/server/public/stylesheets/group-forms.css +59 -0
  53. data/lib/mongodb_logger/server/public/stylesheets/group-headers.css +8 -0
  54. data/lib/mongodb_logger/server/public/stylesheets/group-tables.css +87 -0
  55. data/lib/mongodb_logger/server/public/stylesheets/highlight/zenburn.css +115 -0
  56. data/lib/mongodb_logger/server/public/stylesheets/humanity/images/ui-bg_flat_75_aaaaaa_40x100.png +0 -0
  57. data/lib/mongodb_logger/server/public/stylesheets/humanity/images/ui-bg_glass_100_f5f0e5_1x400.png +0 -0
  58. data/lib/mongodb_logger/server/public/stylesheets/humanity/images/ui-bg_glass_25_cb842e_1x400.png +0 -0
  59. data/lib/mongodb_logger/server/public/stylesheets/humanity/images/ui-bg_glass_70_ede4d4_1x400.png +0 -0
  60. data/lib/mongodb_logger/server/public/stylesheets/humanity/images/ui-bg_highlight-hard_100_f4f0ec_1x100.png +0 -0
  61. data/lib/mongodb_logger/server/public/stylesheets/humanity/images/ui-bg_highlight-hard_65_fee4bd_1x100.png +0 -0
  62. data/lib/mongodb_logger/server/public/stylesheets/humanity/images/ui-bg_highlight-hard_75_f5f5b5_1x100.png +0 -0
  63. data/lib/mongodb_logger/server/public/stylesheets/humanity/images/ui-bg_inset-soft_100_f4f0ec_1x100.png +0 -0
  64. data/lib/mongodb_logger/server/public/stylesheets/humanity/images/ui-icons_c47a23_256x240.png +0 -0
  65. data/lib/mongodb_logger/server/public/stylesheets/humanity/images/ui-icons_cb672b_256x240.png +0 -0
  66. data/lib/mongodb_logger/server/public/stylesheets/humanity/images/ui-icons_f08000_256x240.png +0 -0
  67. data/lib/mongodb_logger/server/public/stylesheets/humanity/images/ui-icons_f35f07_256x240.png +0 -0
  68. data/lib/mongodb_logger/server/public/stylesheets/humanity/images/ui-icons_ff7519_256x240.png +0 -0
  69. data/lib/mongodb_logger/server/public/stylesheets/humanity/images/ui-icons_ffffff_256x240.png +0 -0
  70. data/lib/mongodb_logger/server/public/stylesheets/humanity/jquery-ui-1.8.16.custom.css +568 -0
  71. data/lib/mongodb_logger/server/public/stylesheets/layout.css +205 -0
  72. data/lib/mongodb_logger/server/public/stylesheets/library.css +330 -0
  73. data/lib/mongodb_logger/server/public/stylesheets/reset.css +43 -0
  74. data/lib/mongodb_logger/server/public/stylesheets/spaces.css +42 -0
  75. data/lib/mongodb_logger/server/view_helpers.rb +113 -0
  76. data/lib/mongodb_logger/server/views/analytics.erb +61 -0
  77. data/lib/mongodb_logger/server/views/error.erb +2 -0
  78. data/lib/mongodb_logger/server/views/layout.erb +47 -0
  79. data/lib/mongodb_logger/server/views/overview.erb +119 -0
  80. data/lib/mongodb_logger/server/views/shared/_collection_stats.erb +14 -0
  81. data/lib/mongodb_logger/server/views/shared/_dynamic_filter.erb +34 -0
  82. data/lib/mongodb_logger/server/views/shared/_log.erb +8 -0
  83. data/lib/mongodb_logger/server/views/shared/_log_info.erb +27 -0
  84. data/lib/mongodb_logger/server/views/shared/_message_tabs.erb +15 -0
  85. data/lib/mongodb_logger/server/views/shared/_tabs.erb +4 -0
  86. data/lib/mongodb_logger/server/views/shared/_tail_panel.erb +13 -0
  87. data/lib/mongodb_logger/server/views/shared/_top_panel.erb +7 -0
  88. data/lib/mongodb_logger/server/views/show_log.erb +105 -0
  89. data/lib/mongodb_logger/server.rb +174 -0
  90. data/lib/mongodb_logger/server_config.rb +77 -0
  91. data/lib/mongodb_logger/version.rb +3 -0
  92. data/lib/mongodb_logger.rb +31 -0
  93. data/mongodb_logger.gemspec +44 -0
  94. data/mongodb_logger.java.gemspec +42 -0
  95. data/spec/javascripts/MongodbLoggerMainSpec.js +13 -0
  96. data/spec/javascripts/helpers/SpecHelper.js +3 -0
  97. data/spec/javascripts/support/jasmine.yml +77 -0
  98. data/spec/javascripts/support/jasmine_config.rb +23 -0
  99. data/spec/javascripts/support/jasmine_runner.rb +32 -0
  100. data/test/active_record.rb +13 -0
  101. data/test/config/samples/database.yml +9 -0
  102. data/test/config/samples/database_no_file_logging.yml +10 -0
  103. data/test/config/samples/database_replica_set.yml +12 -0
  104. data/test/config/samples/database_with_auth.yml +9 -0
  105. data/test/config/samples/database_with_collection.yml +8 -0
  106. data/test/config/samples/mongodb_logger.yml +2 -0
  107. data/test/config/samples/mongoid.yml +30 -0
  108. data/test/config/samples/server_config.yml +3 -0
  109. data/test/rails/app/controllers/order_controller.rb +23 -0
  110. data/test/rails/test/functional/order_controller_test.rb +116 -0
  111. data/test/rails/test/test_helper.rb +10 -0
  112. data/test/rails.rb +22 -0
  113. data/test/shoulda_macros/log_macros.rb +13 -0
  114. data/test/test.sh +6 -0
  115. data/test/test_helper.rb +89 -0
  116. data/test/unit/mongodb_logger_replica_test.rb +56 -0
  117. data/test/unit/mongodb_logger_test.rb +270 -0
  118. metadata +383 -0
@@ -0,0 +1,116 @@
1
+ require 'test_helper'
2
+
3
+ class OrderControllerTest < ActionController::TestCase
4
+ def setup
5
+ @mongodb_logger = Rails.logger
6
+ @mongodb_logger.reset_collection
7
+ common_setup
8
+ end
9
+
10
+ test "should have log level set" do
11
+ assert_equal ActiveSupport::BufferedLogger.const_get(Rails.configuration.log_level.to_s.upcase), Rails.logger.level
12
+ end
13
+
14
+ test "should log a single record" do
15
+ get :index
16
+ assert_response :success
17
+ assert_equal 1, @collection.find({"controller" => "order","action"=> "index"}).count
18
+ end
19
+
20
+ test "should log a debug message" do
21
+ get :index
22
+ assert_equal OrderController::LOG_MESSAGE, @collection.find_one({}, :fields => ["messages"])["messages"]["debug"][0]
23
+ end
24
+
25
+ test "should log extra metadata" do
26
+ get :index
27
+ assert_equal Rails.root.basename.to_s, @collection.find_one({}, :fields => "application_name_again")["application_name_again"]
28
+ assert_equal OrderController::LOG_USER_ID, @collection.find_one({}, :fields => "user_id")["user_id"]
29
+ end
30
+
31
+ test "should write GET request method" do
32
+ get :index
33
+ log = @collection.find_one()
34
+ http_method = 'GET'
35
+ assert_equal http_method, log['method']
36
+ end
37
+
38
+ test "should write POST request method" do
39
+ post :create
40
+ log = @collection.find_one()
41
+ http_method = 'POST'
42
+ assert_equal http_method, log['method']
43
+ end
44
+
45
+ test "should log exceptions" do
46
+ assert_raise(RuntimeError, OrderController::LOG_MESSAGE) {get :new}
47
+ assert_equal 1, @collection.find_one({"messages.error" => /^#{OrderController::LOG_MESSAGE}/})["messages"]["error"].count
48
+ assert_equal 1, @collection.find_one({"is_exception" => true})["messages"]["error"].count
49
+ end
50
+
51
+ test "should log find by params keys" do
52
+ some_name = "name"
53
+ post :create, :activity => {:name => some_name}
54
+ assert_equal 1, @collection.find({"params.activity.name" => some_name}).count
55
+ end
56
+
57
+ test "should search any values by params keys" do
58
+ post :test_post, :data => {
59
+ :int => 1,
60
+ :is_true => true,
61
+ :is_false => false,
62
+ :string => "string",
63
+ :push_hash => { :yes => "yes" },
64
+ :float => 1.22
65
+ }
66
+
67
+ # such testing down on Rails 3.1.x, because in tests params converte Fixnum values into String
68
+ # :(
69
+ # assert_equal 1, @collection.find({"params.data.int" => 1}).count
70
+
71
+ # data types
72
+ assert_equal 1, @collection.find({"params.data.is_true" => true}).count
73
+ assert_equal 1, @collection.find({"params.data.is_false" => false}).count
74
+ assert_equal 1, @collection.find({"params.data.string" => "string"}).count
75
+ assert_equal 1, @collection.find({"params.data.push_hash.yes" => "yes"}).count
76
+ end
77
+
78
+ test "should search any values by params keys with attachments" do
79
+ filepath = "mltest_file.html"
80
+ content_type = "text/html"
81
+ tmpfile = File.open("#{ActionController::TestCase.fixture_path}#{filepath}", 'w') {|f| f.write("<html></html>") }
82
+
83
+ uploaded_file = fixture_file_upload(filepath, content_type, :binary)
84
+ post :test_post, :data => {
85
+ :some_key => [
86
+ {:file => uploaded_file}
87
+ ],
88
+ :int => 1,
89
+ :is_true => true,
90
+ :is_false => false,
91
+ :string => "string",
92
+ :push_hash => { :yes => "yes" },
93
+ :float => 1.22
94
+ }
95
+
96
+ # data types
97
+ assert_equal 1, @collection.find({"params.data.is_true" => true}).count
98
+ assert_equal 1, @collection.find({"params.data.is_false" => false}).count
99
+ assert_equal 1, @collection.find({"params.data.string" => "string"}).count
100
+ assert_equal 1, @collection.find({"params.data.push_hash.yes" => "yes"}).count
101
+ # attachment
102
+ assert_equal 1, @collection.find({"params.data.some_key.file.content_type" => content_type}).count
103
+ assert_equal 1, @collection.find({"params.data.some_key.file.original_filename" => filepath}).count
104
+
105
+ File.delete(filepath) if File.exist?(filepath)
106
+ end
107
+
108
+ test "should not log passwords" do
109
+ post :create, :order => {:password => OrderController::LOG_MESSAGE }
110
+ assert_equal 1, @collection.find_one({"params.order.password" => "[FILTERED]"})["params"]["order"].count
111
+ end
112
+
113
+ test "should set the application name" do
114
+ assert_equal 'mongo_foo', @mongodb_logger.instance_variable_get(:@application_name)
115
+ end
116
+ end
@@ -0,0 +1,10 @@
1
+ ENV["RAILS_ENV"] = "test"
2
+ require File.expand_path('../../config/environment', __FILE__)
3
+ require 'rails/test_help'
4
+
5
+ class ActiveSupport::TestCase
6
+ def common_setup
7
+ @con = @mongodb_logger.mongo_connection
8
+ @collection = @con[@mongodb_logger.mongo_collection_name]
9
+ end
10
+ end
data/test/rails.rb ADDED
@@ -0,0 +1,22 @@
1
+ module MongodbLogger
2
+ class Application
3
+ end
4
+ end
5
+
6
+ class Rails
7
+ module VERSION
8
+ MAJOR = 3
9
+ end
10
+
11
+ def self.env
12
+ ActiveSupport::StringInquirer.new("test")
13
+ end
14
+
15
+ def self.root
16
+ Pathname.new(File.dirname(__FILE__))
17
+ end
18
+
19
+ def self.application
20
+ MongodbLogger::Application.new
21
+ end
22
+ end
@@ -0,0 +1,13 @@
1
+ module LogMacros
2
+ def should_contain_one_log_record
3
+ should "contain a log record" do
4
+ assert_equal 1, @con[@mongodb_logger.mongo_collection_name].count
5
+ end
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", @mongodb_logger.db_configuration['database']
11
+ end
12
+ end
13
+ end
data/test/test.sh ADDED
@@ -0,0 +1,6 @@
1
+ #!/bin/bash
2
+ rake vendor_test_gems && \
3
+ rake test && \
4
+ rake cucumber:web && \
5
+ rake jasmine:ci && \
6
+ rake cucumber:rails:all
@@ -0,0 +1,89 @@
1
+ require 'test/unit'
2
+
3
+ begin
4
+ require 'shoulda'
5
+ rescue LoadError
6
+ require 'rubygems'
7
+ require 'shoulda'
8
+ end
9
+
10
+ require 'mocha'
11
+ # mock rails class
12
+ require 'pathname'
13
+ require 'rails'
14
+ require 'fileutils'
15
+
16
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
17
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
18
+
19
+ Shoulda.autoload_macros("#{File.dirname(__FILE__)}/..")
20
+
21
+ class Test::Unit::TestCase
22
+ CONFIG_DIR = Rails.root.join("config")
23
+ SAMPLE_CONFIG_DIR = File.join(CONFIG_DIR, "samples")
24
+ DEFAULT_CONFIG = "database.yml"
25
+ DEFAULT_CONFIG_WITH_AUTH = "database_with_auth.yml"
26
+ DEFAULT_CONFIG_WITH_COLLECTION = "database_with_collection.yml"
27
+ DEFAULT_CONFIG_WITH_NO_FILE_LOGGING = "database_no_file_logging.yml"
28
+ MONGOID_CONFIG = "mongoid.yml"
29
+ REPLICA_SET_CONFIG = "database_replica_set.yml"
30
+ LOGGER_CONFIG = "mongodb_logger.yml"
31
+
32
+ def log(msg)
33
+ @mongodb_logger.mongoize({"id" => 1}) do
34
+ @mongodb_logger.debug(msg)
35
+ end
36
+ end
37
+
38
+ def log_params(msg)
39
+ @mongodb_logger.mongoize({:params => msg})
40
+ end
41
+
42
+ def log_exception(msg)
43
+ @mongodb_logger.mongoize({"id" => 1}) do
44
+ raise msg
45
+ end
46
+ end
47
+
48
+ def setup_for_config(source, dest=source)
49
+ File.delete(File.join(CONFIG_DIR, DEFAULT_CONFIG))
50
+ cp_config(source, dest)
51
+ @mongodb_logger.send(:configure)
52
+ end
53
+
54
+ def cp_config(source, dest=source)
55
+ FileUtils.cp(File.join(SAMPLE_CONFIG_DIR, source), File.join(CONFIG_DIR, dest))
56
+ end
57
+
58
+ def teardown_for_config(file)
59
+ File.delete(File.join(CONFIG_DIR, file))
60
+ end
61
+
62
+ def log_metadata(options)
63
+ @mongodb_logger.mongoize({"id" => 1}) do
64
+ @mongodb_logger.add_metadata(options)
65
+ end
66
+ end
67
+
68
+ def require_bogus_active_record
69
+ require 'active_record'
70
+ end
71
+
72
+ def common_setup
73
+ @con = @mongodb_logger.mongo_connection
74
+ @collection = @con[@mongodb_logger.mongo_collection_name]
75
+ end
76
+
77
+ def create_user
78
+ db_conf = @mongodb_logger.db_configuration
79
+ @user = db_conf['username']
80
+ mongo_connection = Mongo::Connection.new(db_conf['host'],
81
+ db_conf['port']).db(db_conf['database'])
82
+ mongo_connection.add_user(@user, db_conf['password'])
83
+ end
84
+
85
+ def remove_user
86
+ @mongodb_logger.mongo_connection.remove_user(@user)
87
+ end
88
+
89
+ end
@@ -0,0 +1,56 @@
1
+ require 'test_helper'
2
+ require 'mongodb_logger/logger'
3
+
4
+ # HOWTO run this test:
5
+ # before start this test, do this in console:
6
+ # mkdir -p /tmp/data1
7
+ # mkdir -p /tmp/data2
8
+ # mkdir -p /tmp/data3
9
+ # mongod --replSet foo --port 27018 --dbpath /tmp/data1
10
+ # mongod --replSet foo --port 27019 --dbpath /tmp/data2
11
+ # mongod --replSet foo --port 27020 --dbpath /tmp/data3
12
+ # mongo localhost:27018
13
+ # In mongo console:
14
+ # config = {_id: 'foo', members: [
15
+ # {_id: 0, host: 'localhost:27018'},
16
+ # {_id: 1, host: 'localhost:27019'},
17
+ # {_id: 2, host: 'localhost:27020', arbiterOnly: true}]
18
+ # }
19
+ #
20
+ # rs.initiate(config);
21
+ # You should see such output:
22
+ # {
23
+ # "info" : "Config now saved locally. Should come online in about a minute.",
24
+ # "ok" : 1
25
+ # }
26
+
27
+
28
+ class MongodbLogger::MongodbLoggerReplicaTest < Test::Unit::TestCase
29
+ extend LogMacros
30
+
31
+ context "A MongodbLogger::MongoLogger connecting to a replica set" do
32
+ setup do
33
+ FileUtils.cp(File.join(SAMPLE_CONFIG_DIR, REPLICA_SET_CONFIG), File.join(CONFIG_DIR, DEFAULT_CONFIG))
34
+ MongodbLogger::Logger.any_instance.stubs(:internal_initialize).returns(nil)
35
+ MongodbLogger::Logger.any_instance.stubs(:disable_file_logging?).returns(false)
36
+ @mongodb_logger = MongodbLogger::Logger.new
37
+ @mongodb_logger.send(:configure)
38
+ @mongodb_logger.send(:connect)
39
+ common_setup
40
+ @collection.drop
41
+ end
42
+
43
+ should "derive from Mongo::ReplSetConnection" do
44
+ assert_equal Mongo::ReplSetConnection, @mongodb_logger.mongo_connection_type
45
+ end
46
+
47
+ should "force replica_set parameter to be true" do
48
+ assert @mongodb_logger.db_configuration['replica_set']
49
+ end
50
+
51
+ teardown do
52
+ file = File.join(CONFIG_DIR, DEFAULT_CONFIG)
53
+ File.delete(file) if File.exist?(file)
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,270 @@
1
+ require 'test_helper'
2
+ require 'mongodb_logger/logger'
3
+ require 'tempfile'
4
+ require 'pathname'
5
+
6
+ # test the basic stuff
7
+ class MongodbLogger::LoggerTest < Test::Unit::TestCase
8
+ extend LogMacros
9
+
10
+ EXCEPTION_MSG = "Foo"
11
+
12
+ context "A MongodbLogger::Logger" do
13
+ setup do
14
+ # Can use different configs, but most tests use database.yml
15
+ FileUtils.cp(File.join(SAMPLE_CONFIG_DIR, DEFAULT_CONFIG), CONFIG_DIR)
16
+ end
17
+
18
+ context "in instantiation" do
19
+ setup do
20
+ MongodbLogger::Logger.any_instance.stubs(:internal_initialize).returns(nil)
21
+ MongodbLogger::Logger.any_instance.stubs(:disable_file_logging?).returns(false)
22
+ @mongodb_logger = MongodbLogger::Logger.new
23
+ end
24
+
25
+ context "during configuration when using a separate " + LOGGER_CONFIG do
26
+ setup do
27
+ setup_for_config(LOGGER_CONFIG)
28
+ end
29
+
30
+ should_use_database_name_in_config
31
+
32
+ teardown do
33
+ teardown_for_config(LOGGER_CONFIG)
34
+ end
35
+ end
36
+
37
+ context "during configuration when using a separate " + MONGOID_CONFIG do
38
+ setup do
39
+ setup_for_config(MONGOID_CONFIG)
40
+ end
41
+
42
+ should_use_database_name_in_config
43
+
44
+ teardown do
45
+ teardown_for_config(MONGOID_CONFIG)
46
+ end
47
+ end
48
+
49
+ # this test will work without the --auth mongod arg
50
+ context "upon connecting with authentication settings" do
51
+ setup do
52
+ setup_for_config(DEFAULT_CONFIG_WITH_AUTH, DEFAULT_CONFIG)
53
+ create_user
54
+ end
55
+
56
+ should "authenticate with the credentials in the configuration" do
57
+ @mongodb_logger.send(:connect)
58
+ assert @mongodb_logger.authenticated?
59
+ end
60
+
61
+ teardown do
62
+ # config will be deleted by outer teardown
63
+ remove_user
64
+ end
65
+ end
66
+
67
+ context "after configuration" do
68
+ setup do
69
+ @mongodb_logger.send(:configure)
70
+ end
71
+
72
+ should "set the default host, port, and capsize if not configured" do
73
+ assert_equal 'localhost', @mongodb_logger.db_configuration['host']
74
+ assert_equal 27017, @mongodb_logger.db_configuration['port']
75
+ assert_equal MongodbLogger::Logger::DEFAULT_COLLECTION_SIZE, @mongodb_logger.db_configuration['capsize']
76
+ end
77
+
78
+ should "set the mongo collection name depending on the Rails environment" do
79
+ assert_equal "#{Rails.env}_log", @mongodb_logger.mongo_collection_name
80
+ end
81
+
82
+ should "set the application name when specified in the config file" do
83
+ assert_equal "mongo_foo", @mongodb_logger.instance_variable_get(:@application_name)
84
+ end
85
+
86
+ should "set safe insert when specified in the config file" do
87
+ assert @mongodb_logger.instance_variable_get(:@safe_insert)
88
+ end
89
+
90
+ should "use the database name in the config file" do
91
+ assert_equal "system_log", @mongodb_logger.db_configuration['database']
92
+ end
93
+
94
+ context "upon connecting to an empty database" do
95
+ setup do
96
+ @mongodb_logger.send(:connect)
97
+ common_setup
98
+ @collection.drop
99
+ end
100
+
101
+ should "expose a valid mongo connection" do
102
+ assert_instance_of Mongo::DB, @mongodb_logger.mongo_connection
103
+ end
104
+
105
+ should "derive from Mongo::Connection for single host" do
106
+ assert_equal Mongo::Connection, @mongodb_logger.mongo_connection_type
107
+ end
108
+
109
+ should "not authenticate" do
110
+ assert !@mongodb_logger.authenticated?
111
+ end
112
+
113
+ should "create a capped collection in the database with the configured size" do
114
+ @mongodb_logger.send(:check_for_collection)
115
+ assert @con.collection_names.include?(@mongodb_logger.mongo_collection_name)
116
+ # new capped collections are X MB + 5888 bytes, but don't be too strict in case that changes
117
+ assert @collection.stats["storageSize"] < MongodbLogger::Logger::DEFAULT_COLLECTION_SIZE + 1.megabyte
118
+ end
119
+ end
120
+ end
121
+ end
122
+
123
+ context "after instantiation" do
124
+ setup do
125
+ @mongodb_logger = MongodbLogger::Logger.new
126
+ common_setup
127
+ @mongodb_logger.reset_collection
128
+ end
129
+
130
+ context "upon insertion of a log record when active record is not used" do
131
+ # mock ActiveRecord has not been included
132
+ setup do
133
+ log("Test")
134
+ end
135
+
136
+ should_contain_one_log_record
137
+
138
+ should "allow recreation of the capped collection to remove all records" do
139
+ @mongodb_logger.reset_collection
140
+ assert_equal 0, @collection.count
141
+ end
142
+ end
143
+
144
+ context "upon insertion of a colorized log record when ActiveRecord is used" do
145
+ setup do
146
+ @log_message = "TESTING"
147
+ require_bogus_active_record
148
+ log("\e[31m #{@log_message} \e[0m")
149
+ end
150
+
151
+ should "detect logging is colorized" do
152
+ assert @mongodb_logger.send(:logging_colorized?)
153
+ end
154
+
155
+ should_contain_one_log_record
156
+
157
+ should "strip out colorization from log messages" do
158
+ assert_equal 1, @collection.find({"messages.debug" => @log_message}).count
159
+ end
160
+ end
161
+
162
+ should "add application metadata to the log record" do
163
+ options = {"application" => self.class.name}
164
+ log_metadata(options)
165
+ assert_equal 1, @collection.find({"application" => self.class.name}).count
166
+ end
167
+
168
+ should "not raise an exception when bson-unserializable data is logged in the :messages key" do
169
+ log(Tempfile.new("foo"))
170
+ assert_equal 1, @collection.count
171
+ end
172
+
173
+ should "not raise an exception when bson-unserializable data is logged in the :params key" do
174
+ log_params({:foo => Tempfile.new("bar")})
175
+ assert_equal 1, @collection.count
176
+ end
177
+
178
+ context "when an exception is raised" do
179
+ should "log the exception" do
180
+ assert_raise(RuntimeError, EXCEPTION_MSG) {log_exception(EXCEPTION_MSG)}
181
+ assert_equal 1, @collection.find_one({"messages.error" => /^#{EXCEPTION_MSG}/})["messages"]["error"].count
182
+ assert_equal 1, @collection.find_one({"is_exception" => true})["messages"]["error"].count
183
+ end
184
+ end
185
+ end
186
+
187
+ context "logging at INFO level" do
188
+ setup do
189
+ @mongodb_logger = MongodbLogger::Logger.new(:level => MongodbLogger::Logger::INFO)
190
+ common_setup
191
+ @mongodb_logger.reset_collection
192
+ log("INFO")
193
+ end
194
+
195
+ should_contain_one_log_record
196
+
197
+ should "not log DEBUG messages" do
198
+ assert_equal 0, @collection.find_one({}, :fields => ["messages"])["messages"].count
199
+ end
200
+ end
201
+ teardown do
202
+ file = File.join(CONFIG_DIR, DEFAULT_CONFIG)
203
+ File.delete(file) if File.exist?(file)
204
+ end
205
+ end
206
+
207
+ context "A MongodbLogger::Logger without file logging" do
208
+ setup do
209
+ FileUtils.cp(File.join(SAMPLE_CONFIG_DIR, DEFAULT_CONFIG_WITH_NO_FILE_LOGGING), File.join(CONFIG_DIR, DEFAULT_CONFIG))
210
+ @log_file = Pathname.new('log.out')
211
+ FileUtils.touch(@log_file)
212
+ end
213
+
214
+ context "in instantiation" do
215
+ should "not call super in the initialize method" do
216
+ MongodbLogger::Logger.any_instance.expects(:open).never # Stubbing out super doesn't work, so we use this side effect instead.
217
+ MongodbLogger::Logger.new
218
+ end
219
+
220
+ should "set buffer" do
221
+ assert_equal({}, MongodbLogger::Logger.new.instance_variable_get(:@buffer))
222
+ end
223
+ should "set guard" do
224
+ assert MongodbLogger::Logger.new.instance_variable_get(:@guard).is_a?(Mutex)
225
+ end
226
+ end
227
+
228
+ context "after instantiation" do
229
+ context "upon insertion of a log record" do
230
+ setup do
231
+ @mongodb_logger = MongodbLogger::Logger.new(:path => @log_file)
232
+ log("Test")
233
+ end
234
+
235
+ should "not log the record to a file" do
236
+ assert_equal '', open(@log_file).read
237
+ end
238
+ end
239
+ end
240
+
241
+ teardown do
242
+ file = File.join(CONFIG_DIR, DEFAULT_CONFIG)
243
+ File.delete(file) if File.exist?(file)
244
+ File.delete(@log_file)
245
+ end
246
+ end
247
+
248
+ context "A MongodbLogger::Logger with custom collection" do
249
+ setup do
250
+ file_path = File.join(SAMPLE_CONFIG_DIR, DEFAULT_CONFIG_WITH_COLLECTION)
251
+ FileUtils.cp(file_path, File.join(CONFIG_DIR, DEFAULT_CONFIG))
252
+ @mongodb_logger = MongodbLogger::Logger.new
253
+ common_setup
254
+ @mongodb_logger.reset_collection
255
+
256
+ @file_config = YAML.load(ERB.new(File.new(file_path).read).result)[Rails.env]['mongodb_logger']
257
+ end
258
+
259
+ should "changed collection name" do
260
+ assert_equal @file_config['collection'], @mongodb_logger.mongo_collection_name
261
+ assert_equal "#{@file_config['database']}.#{@file_config['collection']}", @collection.stats()['ns']
262
+ end
263
+
264
+ teardown do
265
+ file = File.join(CONFIG_DIR, DEFAULT_CONFIG)
266
+ File.delete(file) if File.exist?(file)
267
+ end
268
+ end
269
+
270
+ end