Skip to content

Commit c03c860

Browse files
oggyhsbt
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 0996f90 commit c03c860

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

lib/logger.rb

+15-4
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 +$stdout+, +$stderr+. or an open file):
547547
# 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
# Examples:
@@ -586,13 +587,23 @@ def initialize(logdev, shift_age = 0, shift_size = 1048576, level: DEBUG,
586587
@logdev = nil
587588
@level_override = {}
588589
if logdev && logdev != File::NULL
589-
@logdev = LogDevice.new(logdev, shift_age: shift_age,
590-
shift_size: shift_size,
591-
shift_period_suffix: shift_period_suffix,
592-
binmode: binmode)
590+
if logdev.is_a?(LogDevice)
591+
@logdev = logdev
592+
else
593+
@logdev = LogDevice.new(logdev, shift_age: shift_age,
594+
shift_size: shift_size,
595+
shift_period_suffix: shift_period_suffix,
596+
binmode: binmode)
597+
end
593598
end
594599
end
595600

601+
# The underlying log device.
602+
#
603+
# This is the first argument passed to the constructor, wrapped in a
604+
# Logger::LogDevice, along with the binmode flag and rotation options.
605+
attr_reader :logdev
606+
596607
# Sets the logger's output stream:
597608
#
598609
# - If +logdev+ is +nil+, reopens the current output stream.

test/logger/test_logger.rb

+10
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,16 @@ def test_initialize
168168
assert_nil(logger.datetime_format)
169169
end
170170

171+
def test_logdev
172+
logger = Logger.new(STDERR)
173+
assert_instance_of(Logger::LogDevice, logger.logdev)
174+
175+
logdev = Logger::LogDevice.new(STDERR)
176+
logger = Logger.new(logdev)
177+
assert_instance_of(Logger::LogDevice, logger.logdev)
178+
assert_equal(STDERR, logger.logdev.dev)
179+
end
180+
171181
def test_initialize_with_level
172182
# default
173183
logger = Logger.new(STDERR)

0 commit comments

Comments
 (0)