diff --git a/lib/web_console.rb b/lib/web_console.rb index ab489d88..279c6bcc 100644 --- a/lib/web_console.rb +++ b/lib/web_console.rb @@ -21,8 +21,9 @@ module WebConsole autoload :DoubleRenderError end - mattr_accessor :logger - @@logger = ActiveSupport::Logger.new($stderr) + def self.logger + Rails.logger || (@logger ||= ActiveSupport::Logger.new($stderr)) + end end require 'web_console/railtie' diff --git a/lib/web_console/railtie.rb b/lib/web_console/railtie.rb index 77778507..9f3d0a40 100644 --- a/lib/web_console/railtie.rb +++ b/lib/web_console/railtie.rb @@ -8,10 +8,6 @@ class Railtie < ::Rails::Railtie initializer 'web_console.initialize' do require 'bindex' require 'web_console/extensions' - - if logger = ::Rails.logger - WebConsole.logger = logger - end end initializer 'web_console.development_only' do diff --git a/test/web_console/middleware_test.rb b/test/web_console/middleware_test.rb index 1416c8a1..fdd69190 100644 --- a/test/web_console/middleware_test.rb +++ b/test/web_console/middleware_test.rb @@ -203,6 +203,25 @@ def body assert_raises(RuntimeError) { get '/' } end + test 'logs internal errors with Rails.logger' do + io = StringIO.new + logger = ActiveSupport::Logger.new(io) + old_logger, Rails.logger = Rails.logger, logger + + begin + @app.stubs(:call_app).raises('whoops') + + get '/' + rescue RuntimeError + output = io.rewind && io.read + lines = output.lines + + assert_equal ["\n", "RuntimeError: whoops\n"], lines.slice!(0, 2) + ensure + Rails.logger = old_logger + end + end + private # Override the put and post testing helper of ActionDispatch to customize http headers