1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Add Logger option to disable message broadcasts

When setting the Rails logger to log to STDOUT, it would broadcast the
log twice in development. This adds a setting that will prevent messages
from being broadcast to multiple logs, while still allowing calls to
`#close`, `#level=`, `#progname=`, and `#formatter=` to be broadcasted.

Fixes #14769, #11415
This commit is contained in:
Nate Smith 2015-12-14 17:27:03 -05:00
parent 87e4481467
commit e12ffb76ff
3 changed files with 40 additions and 23 deletions

View file

@ -1,4 +1,3 @@
require 'active_support/core_ext/module/attribute_accessors'
require 'active_support/logger_silence' require 'active_support/logger_silence'
require 'logger' require 'logger'
@ -6,16 +5,18 @@ module ActiveSupport
class Logger < ::Logger class Logger < ::Logger
include LoggerSilence include LoggerSilence
attr_accessor :broadcast_messages
# Broadcasts logs to multiple loggers. # Broadcasts logs to multiple loggers.
def self.broadcast(logger) # :nodoc: def self.broadcast(logger) # :nodoc:
Module.new do Module.new do
define_method(:add) do |*args, &block| define_method(:add) do |*args, &block|
logger.add(*args, &block) logger.add(*args, &block) if broadcast_messages
super(*args, &block) super(*args, &block)
end end
define_method(:<<) do |x| define_method(:<<) do |x|
logger << x logger << x if broadcast_messages
super(x) super(x)
end end
@ -44,6 +45,7 @@ module ActiveSupport
def initialize(*args) def initialize(*args)
super super
@formatter = SimpleFormatter.new @formatter = SimpleFormatter.new
@broadcast_messages = true
end end
# Simple formatter which only displays the message. # Simple formatter which only displays the message.

View file

@ -1,4 +1,5 @@
require 'active_support/concern' require 'active_support/concern'
require 'active_support/core_ext/module/attribute_accessors'
module LoggerSilence module LoggerSilence
extend ActiveSupport::Concern extend ActiveSupport::Concern

View file

@ -2,56 +2,69 @@ require 'abstract_unit'
module ActiveSupport module ActiveSupport
class BroadcastLoggerTest < TestCase class BroadcastLoggerTest < TestCase
attr_reader :logger, :log1, :log2 attr_reader :logger, :receiving_logger
def setup def setup
@log1 = FakeLogger.new @logger = FakeLogger.new
@log2 = FakeLogger.new @receiving_logger = FakeLogger.new
@log1.extend Logger.broadcast @log2 @logger.extend Logger.broadcast @receiving_logger
@logger = @log1
end end
def test_debug def test_debug
logger.debug "foo" logger.debug "foo"
assert_equal 'foo', log1.adds.first[2] assert_equal 'foo', logger.adds.first[2]
assert_equal 'foo', log2.adds.first[2] assert_equal 'foo', receiving_logger.adds.first[2]
end
def test_debug_without_message_broadcasts
logger.broadcast_messages = false
logger.debug "foo"
assert_equal 'foo', logger.adds.first[2]
assert_equal [], receiving_logger.adds
end end
def test_close def test_close
logger.close logger.close
assert log1.closed, 'should be closed' assert logger.closed, 'should be closed'
assert log2.closed, 'should be closed' assert receiving_logger.closed, 'should be closed'
end end
def test_chevrons def test_chevrons
logger << "foo" logger << "foo"
assert_equal %w{ foo }, log1.chevrons assert_equal %w{ foo }, logger.chevrons
assert_equal %w{ foo }, log2.chevrons assert_equal %w{ foo }, receiving_logger.chevrons
end
def test_chevrons_without_message_broadcasts
logger.broadcast_messages = false
logger << "foo"
assert_equal %w{ foo }, logger.chevrons
assert_equal [], receiving_logger.chevrons
end end
def test_level def test_level
assert_nil logger.level assert_nil logger.level
logger.level = 10 logger.level = 10
assert_equal 10, log1.level assert_equal 10, logger.level
assert_equal 10, log2.level assert_equal 10, receiving_logger.level
end end
def test_progname def test_progname
assert_nil logger.progname assert_nil logger.progname
logger.progname = 10 logger.progname = 10
assert_equal 10, log1.progname assert_equal 10, logger.progname
assert_equal 10, log2.progname assert_equal 10, receiving_logger.progname
end end
def test_formatter def test_formatter
assert_nil logger.formatter assert_nil logger.formatter
logger.formatter = 10 logger.formatter = 10
assert_equal 10, log1.formatter assert_equal 10, logger.formatter
assert_equal 10, log2.formatter assert_equal 10, receiving_logger.formatter
end end
class FakeLogger class FakeLogger
attr_reader :adds, :closed, :chevrons attr_reader :adds, :closed, :chevrons
attr_accessor :level, :progname, :formatter attr_accessor :level, :progname, :formatter, :broadcast_messages
def initialize def initialize
@adds = [] @adds = []
@ -60,6 +73,7 @@ module ActiveSupport
@level = nil @level = nil
@progname = nil @progname = nil
@formatter = nil @formatter = nil
@broadcast_messages = true
end end
def debug msg, &block def debug msg, &block