Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add reraise_exception_classes keyword argument to Logger and LogDevice #37

Merged
merged 1 commit into from
Jul 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Add reraise_write_errors keyword argument to Logger and LogDevice
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
commit 8a4f87cca6e8c3379c4f577fb86f742efdc9ef46
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