Skip to content

Commit

Permalink
Add reraise_write_errors keyword argument to Logger and LogDevice
Browse files Browse the repository at this point in the history
This allows the user to specify exception classes to treat as regular
exceptions instead of being swallowed.  Among other things, it is
useful for having Logger work with Timeout.

Fixes Ruby Bug 9115.
  • Loading branch information
jeremyevans committed Jul 11, 2024
1 parent b23069d commit 436a7d6
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
9 changes: 7 additions & 2 deletions lib/logger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -574,10 +574,14 @@ def fatal!; self.level = FATAL; end
# - +shift_period_suffix+: sets the format for the filename suffix
# for periodic log file rotation; default is <tt>'%Y%m%d'</tt>.
# See {Periodic Rotation}[rdoc-ref:Logger@Periodic+Rotation].
# - +reraise_write_errors+: An array of exception classes, which will
# be reraised if there is an error when writing to the log device.
# The default is to swallow all exceptions raised.
#
def initialize(logdev, shift_age = 0, shift_size = 1048576, level: DEBUG,
progname: nil, formatter: nil, datetime_format: nil,
binmode: false, shift_period_suffix: '%Y%m%d')
binmode: false, shift_period_suffix: '%Y%m%d',
reraise_write_errors: [])
self.level = level
self.progname = progname
@default_formatter = Formatter.new
Expand All @@ -589,7 +593,8 @@ def initialize(logdev, shift_age = 0, shift_size = 1048576, level: DEBUG,
@logdev = LogDevice.new(logdev, shift_age: shift_age,
shift_size: shift_size,
shift_period_suffix: shift_period_suffix,
binmode: binmode)
binmode: binmode,
reraise_write_errors: reraise_write_errors)
end
end

Expand Down
9 changes: 8 additions & 1 deletion lib/logger/log_device.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ class LogDevice
attr_reader :filename
include MonitorMixin

def initialize(log = nil, shift_age: nil, shift_size: nil, shift_period_suffix: nil, binmode: false)
def initialize(log = nil, shift_age: nil, shift_size: nil, shift_period_suffix: nil, binmode: false, reraise_write_errors: [])
@dev = @filename = @shift_age = @shift_size = @shift_period_suffix = nil
@binmode = binmode
@reraise_write_errors = reraise_write_errors
mon_initialize
set_dev(log)
if @filename
Expand All @@ -34,16 +35,22 @@ def write(message)
if @shift_age and @dev.respond_to?(:stat)
begin
check_shift_log
rescue *@reraise_write_errors
raise
rescue
warn("log shifting failed. #{$!}")
end
end
begin
@dev.write(message)
rescue *@reraise_write_errors
raise
rescue
warn("log writing failed. #{$!}")
end
end
rescue *@reraise_write_errors
raise
rescue Exception => ignored
warn("log writing failed. #{ignored}")
end
Expand Down
9 changes: 9 additions & 0 deletions test/logger/test_logger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,15 @@ def test_string_level
assert_raise(ArgumentError) { @logger.level = 'something_wrong' }
end

def test_reraise_write_errors
c = Object.new
e = Class.new(StandardError)
c.define_singleton_method(:write){|*| raise e}
c.define_singleton_method(:close){}
logger = Logger.new(c, :reraise_write_errors=>[e])
assert_raise(e) { logger.warn('foo') }
end

def test_progname
assert_nil(@logger.progname)
@logger.progname = "name"
Expand Down

0 comments on commit 436a7d6

Please sign in to comment.