Skip to content

Commit 88cd173

Browse files
oggynobu
authored andcommitted
Expose the logdev
The motivation here is to fix a Rails issue caused by the way ActiveSupport extends the ruby Logger to add broadcasting of messages to multiple destinations. [1] I believe the problem could be much more elegantly solved if Logger exposed the underlying LogDevice. Going by repo history, the existence of this object hasn't changed since 2003, so I think it's stable enough to expose. In addition to letting you read the logdev, this also lets you pass a logdev in. To implement broadcasting, we could now define a LogDevice subclass that delegates its 3 methods to the LogDevices of a list of underlying loggers, and simply create a new logger with this device. [1]: https://github.com/rails/rails/blob/ba19dbc49956a73f417abd68c7a5f33e302eacd3/activesupport/lib/active_support/logger.rb#L23
1 parent f80a18e commit 88cd173

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

lib/logger.rb

+14-1
Original file line numberDiff line numberDiff line change
@@ -545,6 +545,7 @@ def fatal!; self.level = FATAL; end
545545
# new entries are appended.
546546
# - An IO stream (typically <tt>$stdout</tt>, <tt>$stderr</tt>. or
547547
# an open file): entries are to be written to the given stream.
548+
# - An instance of Logger::LogDevice, such as the #logdev of another Logger.
548549
# - +nil+ or +File::NULL+: no entries are to be written.
549550
#
550551
# Argument +shift_age+ must be one of:
@@ -603,7 +604,13 @@ def initialize(logdev, shift_age = 0, shift_size = 1048576, level: DEBUG,
603604
self.formatter = formatter
604605
@logdev = nil
605606
@level_override = {}
606-
if logdev && logdev != File::NULL
607+
return unless logdev
608+
case logdev
609+
when File::NULL
610+
# null logger
611+
when LogDevice
612+
@logdev = logdev
613+
else
607614
@logdev = LogDevice.new(logdev, shift_age: shift_age,
608615
shift_size: shift_size,
609616
shift_period_suffix: shift_period_suffix,
@@ -612,6 +619,12 @@ def initialize(logdev, shift_age = 0, shift_size = 1048576, level: DEBUG,
612619
end
613620
end
614621

622+
# The underlying log device.
623+
#
624+
# This is the first argument passed to the constructor, wrapped in a
625+
# Logger::LogDevice, along with the binmode flag and rotation options.
626+
attr_reader :logdev
627+
615628
# Sets the logger's output stream:
616629
#
617630
# - If +logdev+ is +nil+, reopens the current output stream.

test/logger/test_logger.rb

+10
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,16 @@ def test_initialize
177177
assert_nil(logger.datetime_format)
178178
end
179179

180+
def test_logdev
181+
logger = Logger.new(STDERR)
182+
assert_instance_of(Logger::LogDevice, logger.logdev)
183+
184+
logdev = Logger::LogDevice.new(STDERR)
185+
logger = Logger.new(logdev)
186+
assert_instance_of(Logger::LogDevice, logger.logdev)
187+
assert_equal(STDERR, logger.logdev.dev)
188+
end
189+
180190
def test_initialize_with_level
181191
# default
182192
logger = Logger.new(STDERR)

0 commit comments

Comments
 (0)