Skip to content

Commit

Permalink
Set shift parameters at reopen
Browse files Browse the repository at this point in the history
Keep parameters already set, and do not clear at reopen to a non-file
device.

Fix #55.
  • Loading branch information
nobu committed Oct 9, 2024
1 parent d1d704a commit a516175
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 10 deletions.
25 changes: 15 additions & 10 deletions lib/logger/log_device.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -76,6 +67,7 @@ def reopen(log = nil)
@filename = nil
end
set_dev(log)
set_shift
end
end
self
Expand All @@ -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))
Expand Down
9 changes: 9 additions & 0 deletions test/logger/test_logdevice.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit a516175

Please sign in to comment.