pushed broadcasting down to a module

This commit is contained in:
Aaron Patterson 2012-01-20 14:18:29 -08:00
parent f02cec3c49
commit a0da46ecaf
3 changed files with 42 additions and 50 deletions

View File

@ -39,8 +39,7 @@ module ActiveRecord
console do |app|
require "active_record/railties/console_sandbox" if app.sandbox?
console = ActiveSupport::Logger.new(STDERR)
logger = ActiveSupport::BroadcastLogger.new [Rails.logger, console]
ActiveRecord::Base.logger = logger
Rails.logger.extend ActiveSupport::Logger.broadcast console
end
initializer "active_record.initialize_timezone" do

View File

@ -1,54 +1,42 @@
require 'logger'
module ActiveSupport
# Broadcasts logs to multiple loggers
class BroadcastLogger < ::Logger # :nodoc:
attr_reader :logs
def initialize(logs)
super(nil)
@logs = logs
end
def progname
logs.first.progname
end
def progname=(name)
logs.each { |x| x.progname = name }
end
def formatter
logs.first.formatter
end
def formatter=(formatter)
logs.each { |x| x.formatter = formatter }
end
def level
logs.first.level
end
def level=(level)
logs.each { |x| x.level = level }
end
def add(severity, message = nil, progname = nil, &block)
super
logs.each { |l| l.add(severity, message, progname, &block) }
end
def <<(x)
logs.each { |l| l << x }
end
def close
logs.each(&:close)
end
end
class Logger < ::Logger
# Broadcasts logs to multiple loggers
def self.broadcast(logger) # :nodoc:
Module.new do
define_method(:add) do |*args, &block|
logger.add(*args, &block)
super(*args, &block)
end
define_method(:<<) do |x|
logger << x
super(x)
end
define_method(:close) do
logger.close
super()
end
define_method(:progname=) do |name|
logger.progname = name
super(name)
end
define_method(:formatter=) do |formatter|
logger.formatter = formatter
super(formatter)
end
define_method(:level=) do |level|
logger.level = level
super(level)
end
end
end
def initialize(*args)
super
@formatter = SimpleFormatter.new

View File

@ -6,7 +6,8 @@ module ActiveSupport
def setup
@log1 = FakeLogger.new
@log2 = FakeLogger.new
@logger = BroadcastLogger.new [log1, log2]
@log1.extend Logger.broadcast @log2
@logger = @log1
end
def test_debug
@ -61,6 +62,10 @@ module ActiveSupport
@formatter = nil
end
def debug msg, &block
add(:omg, nil, msg, &block)
end
def << x
@chevrons << x
end