From a51617578fc3ab10fc1d43a7b8699910f2f62a1a Mon Sep 17 00:00:00 2001 From: Nobuyoshi Nakada Date: Wed, 9 Oct 2024 13:33:48 +0900 Subject: [PATCH] Set shift parameters at reopen Keep parameters already set, and do not clear at reopen to a non-file device. Fix #55. --- lib/logger/log_device.rb | 25 +++++++++++++++---------- test/logger/test_logdevice.rb | 9 +++++++++ 2 files changed, 24 insertions(+), 10 deletions(-) diff --git a/lib/logger/log_device.rb b/lib/logger/log_device.rb index 4876adf..5d90ef1 100644 --- a/lib/logger/log_device.rb +++ b/lib/logger/log_device.rb @@ -17,16 +17,7 @@ def initialize(log = nil, shift_age: nil, shift_size: nil, shift_period_suffix: @reraise_write_errors = reraise_write_errors mon_initialize set_dev(log) - if @filename - @shift_age = shift_age || 7 - @shift_size = shift_size || 1048576 - @shift_period_suffix = shift_period_suffix || '%Y%m%d' - - unless @shift_age.is_a?(Integer) - base_time = @dev.respond_to?(:stat) ? @dev.stat.mtime : Time.now - @next_rotate_time = next_rotate_time(base_time, @shift_age) - end - end + set_shift(shift_age, shift_size, shift_period_suffix) end def write(message) @@ -76,6 +67,7 @@ def reopen(log = nil) @filename = nil end set_dev(log) + set_shift end end self @@ -99,6 +91,19 @@ def set_dev(log) end end + def set_shift(shift_age = @shift_age, shift_size = @shift_size, shift_period_suffix = @shift_period_suffix) + if @filename + @shift_age = shift_age || 7 + @shift_size = shift_size || 1048576 + @shift_period_suffix = shift_period_suffix || '%Y%m%d' + + unless @shift_age.is_a?(Integer) + base_time = @dev.respond_to?(:stat) ? @dev.stat.mtime : Time.now + @next_rotate_time = next_rotate_time(base_time, @shift_age) + end + end + end + def open_logfile(filename) begin File.open(filename, (File::WRONLY | File::APPEND)) diff --git a/test/logger/test_logdevice.rb b/test/logger/test_logdevice.rb index 8f1c155..dd34714 100644 --- a/test/logger/test_logdevice.rb +++ b/test/logger/test_logdevice.rb @@ -5,6 +5,14 @@ require 'tmpdir' class TestLogDevice < Test::Unit::TestCase + module LogDeviceAttributes + refine Logger::LogDevice do + attr_reader :shift_age, :shift_size, :shift_period_suffix, :binmode, :reraise_write_errors + end + end + + using LogDeviceAttributes + class LogExcnRaiser def write(*arg) raise 'disk is full' @@ -140,6 +148,7 @@ def test_reopen_io_by_file begin assert_file.exist?(@filename) assert_equal(@filename, logdev.filename) + assert_equal(7, logdev.shift_age) assert_not_predicate(old_dev, :closed?) ensure logdev.close