Skip to content

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also .

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also .
...
  • 7 commits
  • 8 files changed
  • 0 commit comments
  • 2 contributors
View
4 README.md
@@ -36,7 +36,9 @@ Log to a central MongoDB from Rails apps.
capsize: <%= 10.megabytes %> # default: 250MB for production; 100MB otherwise
host: localhost # default: localhost
port: 27017 # default: 27017
- replica_set: true # default: false - Adds safe inserts and retries for ConnectionFailure during voting
+ replica_set: true # default: false - Adds retries for ConnectionFailure during voting for replica set master
+ safe_insert: true # default: false - Enable/Disable safe inserts (wait for insert to propagate to all nodes)
+ application_name: my_app # default: Rails.application - Only really needed for non-capistrano Rails 2 deployments. Otherwise should set automatically.
central_logger.yml:
View
2 VERSION
@@ -1 +1 @@
-0.3.0
+0.3.1
View
10 central_logger.gemspec
@@ -5,11 +5,11 @@
Gem::Specification.new do |s|
s.name = %q{central_logger}
- s.version = "0.3.0"
+ s.version = "0.3.1"
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.authors = ["Phil Burrows", "Alex Stupka"]
- s.date = %q{2010-12-08}
+ s.date = %q{2011-03-08}
s.description = %q{Centralized logging for rails apps using MongoDB. The idea and the core code is from http://github.com/peburrows/central_logger}
s.email = %q{astupka@customink.com}
s.extra_rdoc_files = [
@@ -62,7 +62,7 @@ Gem::Specification.new do |s|
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
s.add_runtime_dependency(%q<rake>, [">= 0"])
- s.add_runtime_dependency(%q<bundler>, ["~> 1.0.0"])
+ s.add_runtime_dependency(%q<bundler>, [">= 0"])
s.add_runtime_dependency(%q<mongo>, [">= 0"])
s.add_development_dependency(%q<jeweler>, ["~> 1.5.0.pre5"])
s.add_development_dependency(%q<shoulda>, [">= 0"])
@@ -72,7 +72,7 @@ Gem::Specification.new do |s|
s.add_development_dependency(%q<ruby-debug19>, [">= 0"])
else
s.add_dependency(%q<rake>, [">= 0"])
- s.add_dependency(%q<bundler>, ["~> 1.0.0"])
+ s.add_dependency(%q<bundler>, [">= 0"])
s.add_dependency(%q<mongo>, [">= 0"])
s.add_dependency(%q<jeweler>, ["~> 1.5.0.pre5"])
s.add_dependency(%q<shoulda>, [">= 0"])
@@ -83,7 +83,7 @@ Gem::Specification.new do |s|
end
else
s.add_dependency(%q<rake>, [">= 0"])
- s.add_dependency(%q<bundler>, ["~> 1.0.0"])
+ s.add_dependency(%q<bundler>, [">= 0"])
s.add_dependency(%q<mongo>, [">= 0"])
s.add_dependency(%q<jeweler>, ["~> 1.5.0.pre5"])
s.add_dependency(%q<shoulda>, [">= 0"])
View
18 lib/central_logger/mongo_logger.rb
@@ -89,19 +89,33 @@ def internal_initialize
def configure
default_capsize = Rails.env.production? ? PRODUCTION_COLLECTION_SIZE : DEFAULT_COLLECTION_SIZE
- @application_name = Rails.root.basename.to_s
@mongo_collection_name = "#{Rails.env}_log"
@authenticated = false
@db_configuration = {
'host' => 'localhost',
'port' => 27017,
'capsize' => default_capsize}.merge(resolve_config)
+ @application_name = resolve_application_name
+ @safe_insert = @db_configuration['safe_insert'] || false
@insert_block = @db_configuration.has_key?('replica_set') && @db_configuration['replica_set'] ?
- lambda { rescue_connection_failure{ insert_log_record(true) } } :
+ lambda { rescue_connection_failure{ insert_log_record(@safe_insert) } } :
lambda { insert_log_record }
end
+ def resolve_application_name
+ if @db_configuration.has_key?('application_name')
+ @db_configuration['application_name']
+ elsif Rails::VERSION::MAJOR >= 3
+ Rails.application.class.to_s.split("::").first
+ else
+ # rails 2 requires detective work if it's been deployed by capistrano
+ # if last entry is a timestamp, go back 2 dirs (ex. /app_name/releases/20110304132847)
+ path = Rails.root.to_s.split('/')
+ path.length >= 4 && path.last =~ /^\d/ ? path.last(3)[0] : path.last
+ end
+ end
+
def resolve_config
config = {}
CONFIGURATION_FILES.each do |filename|
View
2 test/config/samples/database.yml
@@ -4,3 +4,5 @@ test:
database: database
mongo:
database: system_log
+ application_name: central_foo
+ safe_insert: true
View
9 test/rails.rb
@@ -1,3 +1,8 @@
+module CentralLogger
+ class Application
+ end
+end
+
class Rails
module VERSION
MAJOR = 3
@@ -10,4 +15,8 @@ def self.env
def self.root
Pathname.new(File.dirname(__FILE__))
end
+
+ def self.application
+ CentralLogger::Application.new
+ end
end
View
10 test/rails/common/test/functional/order_controller_test.rb
@@ -40,4 +40,14 @@ def setup
post :create, :order => {:password => OrderController::LOG_MESSAGE }
assert_equal 1, @collection.find_one({"params.order.password" => "[FILTERED]"})["params"]["order"].count
end
+
+ test "should set the application name" do
+ if Rails::VERSION::MAJOR == 3
+ assert_equal 'LoggerAndRails3', @central_logger.instance_variable_get(:@application_name)
+ else
+ # 2 is the base dir, so it assumes it should go up two directories due to
+ # capistrano deployment
+ assert_equal 'test', @central_logger.instance_variable_get(:@application_name)
+ end
+ end
end
View
15 test/unit/central_logger_test.rb
@@ -77,6 +77,14 @@ class CentralLogger::MongoLoggerTest < Test::Unit::TestCase
assert_equal "#{Rails.env}_log", @central_logger.mongo_collection_name
end
+ should "set the application name when specified in the config file" do
+ assert_equal "central_foo", @central_logger.instance_variable_get(:@application_name)
+ end
+
+ should "set safe insert when specified in the config file" do
+ assert @central_logger.instance_variable_get(:@safe_insert)
+ end
+
should "use the database name in the config file" do
assert_equal "system_log", @central_logger.db_configuration['database']
end
@@ -151,9 +159,14 @@ class CentralLogger::MongoLoggerTest < Test::Unit::TestCase
assert_equal 1, @collection.find({"application" => self.class.name}).count
end
- should "not raise an exception when bson-unserializable data is logged" do
+ should "not raise an exception when bson-unserializable data is logged in the :messages key" do
log(Tempfile.new("foo"))
+ assert_equal 1, @collection.count
+ end
+
+ should "not raise an exception when bson-unserializable data is logged in the :params key" do
log_params({:foo => Tempfile.new("bar")})
+ assert_equal 1, @collection.count
end
context "when an exception is raised" do

No commit comments for this range

Something went wrong with that request. Please try again.