Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

pushed broadcasting down to a module

  • Loading branch information...
commit a0da46ecaf354a5b95210e11a4b7aba90a1d7383 1 parent f02cec3
@tenderlove tenderlove authored
View
3  activerecord/lib/active_record/railtie.rb
@@ -39,8 +39,7 @@ class Railtie < Rails::Railtie
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
80 activesupport/lib/active_support/logger.rb
@@ -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)
+ 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
- end
- class Logger < ::Logger
def initialize(*args)
super
@formatter = SimpleFormatter.new
View
7 activesupport/test/broadcast_logger_test.rb
@@ -6,7 +6,8 @@ class BroadcastLoggerTest < TestCase
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 @@ def initialize
@formatter = nil
end
+ def debug msg, &block
+ add(:omg, nil, msg, &block)
+ end
+
def << x
@chevrons << x
end
Please sign in to comment.
Something went wrong with that request. Please try again.