Permalink
Browse files

Initial implementation of logger

  • Loading branch information...
1 parent 0e68e71 commit 2aed3e2485416a21c9f5d979f4ae76d0dae881c6 @joshuaclayton joshuaclayton committed Jan 8, 2010
View
@@ -58,11 +58,7 @@ def write_verbose_log(message)
# Look for the Rails logger currently defined
def logger
- if defined?(Rails.logger)
- Rails.logger
- elsif defined?(RAILS_DEFAULT_LOGGER)
- RAILS_DEFAULT_LOGGER
- end
+ self.configuration.logger
end
# Call this method to modify defaults in your initializers.
@@ -77,6 +77,9 @@ class Configuration
# The url of the notifier library being used to send notifications
attr_accessor :notifier_url
+ # The logger used by HoptoadNotifier
+ attr_accessor :logger
+
DEFAULT_PARAMS_FILTERS = %w(password password_confirmation).freeze
DEFAULT_BACKTRACE_FILTERS = [
@@ -0,0 +1,16 @@
+module HoptoadNotifier
+ # used to initialize Rails-specific code
+ class RailsInit
+ def initialize
+ rails_logger = if defined?(Rails.logger)
+ Rails.logger
+ elsif defined?(RAILS_DEFAULT_LOGGER)
+ RAILS_DEFAULT_LOGGER
+ end
+
+ HoptoadNotifier.configure(true) do |config|
+ config.logger = rails_logger
+ end
+ end
+ end
+end
@@ -15,7 +15,7 @@ def initialize(options = {})
#
# @param [String] data The XML notice to be sent off
def send_to_hoptoad(data)
- logger.debug { "Sending request to #{url.to_s}:\n#{data}" }
+ logger.debug { "Sending request to #{url.to_s}:\n#{data}" } if logger
http =
Net::HTTP::Proxy(proxy_host, proxy_port, proxy_user, proxy_pass).
View
@@ -2,6 +2,9 @@
ActionController::Base.send(:include, HoptoadNotifier::Catcher)
end
+require File.join(File.dirname(__FILE__), '..', 'lib', 'hoptoad_notifier', 'rails_init')
+HoptoadNotifier::RailsInit.new
+
HoptoadNotifier.configure(true) do |config|
config.environment_name = RAILS_ENV
config.project_root = RAILS_ROOT
@@ -11,6 +11,7 @@ class ConfigurationTest < Test::Unit::TestCase
assert_config_default :proxy_pass, nil
assert_config_default :project_root, nil
assert_config_default :environment_name, nil
+ assert_config_default :logger, nil
assert_config_default :notifier_version, HoptoadNotifier::VERSION
assert_config_default :notifier_name, 'Hoptoad Notifier'
assert_config_default :notifier_url, 'http://hoptoadapp.com'
@@ -67,6 +68,7 @@ class ConfigurationTest < Test::Unit::TestCase
assert_config_overridable :notifier_url
assert_config_overridable :environment_name
assert_config_overridable :development_lookup
+ assert_config_overridable :logger
end
should "have an api key" do
@@ -164,6 +166,12 @@ class ConfigurationTest < Test::Unit::TestCase
assert config.public?
end
+ should "use the assigned logger if set" do
+ config = HoptoadNotifier::Configuration.new
+ config.logger = "CUSTOM LOGGER"
+ assert_equal "CUSTOM LOGGER", config.logger
+ end
+
def assert_config_default(option, default_value, config = nil)
config ||= HoptoadNotifier::Configuration.new
assert_equal default_value, config.send(option)
View
@@ -235,4 +235,3 @@ def fatal(*args); end
end
RAILS_DEFAULT_LOGGER = FakeLogger.new
-
View
@@ -0,0 +1,23 @@
+require File.dirname(__FILE__) + '/helper'
+
+class RailsTest < Test::Unit::TestCase
+ include DefinesConstants
+
+ should "trigger use of Rails' logger if logger isn't set and Rails' logger exists" do
+ rails = Module.new do
+ def self.logger
+ "RAILS LOGGER"
+ end
+ end
+ define_constant("Rails", rails)
+ HoptoadNotifier::RailsInit.new
+ assert_equal "RAILS LOGGER", HoptoadNotifier.logger
+ end
+
+ should "trigger use of Rails' default logger if logger isn't set and Rails.logger doesn't exist" do
+ define_constant("RAILS_DEFAULT_LOGGER", "RAILS DEFAULT LOGGER")
+
+ HoptoadNotifier::RailsInit.new
+ assert_equal "RAILS DEFAULT LOGGER", HoptoadNotifier.logger
+ end
+end

0 comments on commit 2aed3e2

Please sign in to comment.