Skip to content

Commit

Permalink
Merge pull request #244 from gsamokovarov/stderr-logging
Browse files Browse the repository at this point in the history
Let WebConsole.logger respect Rails.logger
  • Loading branch information
gsamokovarov authored Aug 23, 2017
2 parents ce985eb + ccf0739 commit bc07283
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 6 deletions.
5 changes: 3 additions & 2 deletions lib/web_console.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
4 changes: 0 additions & 4 deletions lib/web_console/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 19 additions & 0 deletions test/web_console/middleware_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit bc07283

Please sign in to comment.