From ccf0739abea186007ca26bfe2b5306a4f2b56382 Mon Sep 17 00:00:00 2001 From: Genadi Samokovarov Date: Fri, 23 Jun 2017 13:33:26 +0300 Subject: [PATCH] Let WebConsole.logger respect Rails.logger We used to set WebConsole.logger during Bundle.require time, which happened to be way earlier than when Rails.logger was initialized, so we always ended up with `STDERR` logger. In this change, we let `WebConsole.logger` to dynamically call `Rails.logger`, which will let us use it once initialized. If by the time we try to log something, `Rails.logger` isn't initialized or is explicitly set to nothing, we'll still fallback to the `STDERR` logging. --- lib/web_console.rb | 5 +++-- lib/web_console/railtie.rb | 4 ---- test/web_console/middleware_test.rb | 19 +++++++++++++++++++ 3 files changed, 22 insertions(+), 6 deletions(-) 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