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

allow setting shift variables in LogDevice when using reopen #56

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
5 changes: 3 additions & 2 deletions lib/logger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -616,8 +616,9 @@ def initialize(logdev, shift_age = 0, shift_size = 1048576, level: DEBUG,
# # "E, [2022-05-12T14:21:27.596726 #22428] ERROR -- : one\n",
# # "E, [2022-05-12T14:23:05.847241 #22428] ERROR -- : three\n"]
#
def reopen(logdev = nil)
@logdev&.reopen(logdev)
def reopen(logdev = nil, shift_age = nil, shift_size = nil, shift_period_suffix: nil, binmode: nil)
@logdev&.reopen(logdev, shift_age: shift_age, shift_size: shift_size,
shift_period_suffix: shift_period_suffix, binmode: binmode)
self
end

Expand Down
26 changes: 15 additions & 11 deletions lib/logger/log_device.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,7 @@ def initialize(log = nil, shift_age: nil, shift_size: nil, shift_period_suffix:
@binmode = binmode
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_file(shift_age, shift_size, shift_period_suffix) if @filename
end

def write(message)
Expand Down Expand Up @@ -59,16 +50,18 @@ def close
end
end

def reopen(log = nil)
def reopen(log = nil, shift_age: nil, shift_size: nil, shift_period_suffix: nil, binmode: nil)
# reopen the same filename if no argument, do nothing for IO
log ||= @filename if @filename
@binmode = binmode unless binmode.nil?
if log
synchronize do
if @filename and @dev
@dev.close rescue nil # close only file opened by Logger
@filename = nil
end
set_dev(log)
set_file(shift_age, shift_size, shift_period_suffix) if @filename
end
end
self
Expand All @@ -92,6 +85,17 @@ def set_dev(log)
end
end

def set_file(shift_age, shift_size, shift_period_suffix)
@shift_age = shift_age.nil? ? @shift_age || 7 : shift_age
@shift_size = shift_size.nil? ? @shift_size || 1048576 : shift_size
@shift_period_suffix = shift_period_suffix.nil? ? @shift_period_suffix || '%Y%m%d' : shift_period_suffix

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

def open_logfile(filename)
begin
File.open(filename, (File::WRONLY | File::APPEND))
Expand Down
77 changes: 77 additions & 0 deletions test/logger/test_logdevice.rb
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,83 @@ def test_reopen_file_by_file
end
end

def test_shifting_size_with_reopen
tmpfile = Tempfile.new([File.basename(__FILE__, '.*'), '_1.log'])
logfile = tmpfile.path
logfile0 = logfile + '.0'
logfile1 = logfile + '.1'
logfile2 = logfile + '.2'
logfile3 = logfile + '.3'
tmpfile.close(true)
File.unlink(logfile) if File.exist?(logfile)
File.unlink(logfile0) if File.exist?(logfile0)
File.unlink(logfile1) if File.exist?(logfile1)
File.unlink(logfile2) if File.exist?(logfile2)

logger = Logger.new(STDERR)
logger.reopen(logfile, 4, 100)

logger.error("0" * 15)
assert_file.exist?(logfile)
assert_file.not_exist?(logfile0)
logger.error("0" * 15)
assert_file.exist?(logfile0)
assert_file.not_exist?(logfile1)
logger.error("0" * 15)
assert_file.exist?(logfile1)
assert_file.not_exist?(logfile2)
logger.error("0" * 15)
assert_file.exist?(logfile2)
assert_file.not_exist?(logfile3)
logger.error("0" * 15)
assert_file.not_exist?(logfile3)
logger.error("0" * 15)
assert_file.not_exist?(logfile3)
logger.close
File.unlink(logfile)
File.unlink(logfile0)
File.unlink(logfile1)
File.unlink(logfile2)

tmpfile = Tempfile.new([File.basename(__FILE__, '.*'), '_2.log'])
logfile = tmpfile.path
logfile0 = logfile + '.0'
logfile1 = logfile + '.1'
logfile2 = logfile + '.2'
logfile3 = logfile + '.3'
tmpfile.close(true)
logger = Logger.new(logfile, 4, 150)
logger.error("0" * 15)
assert_file.exist?(logfile)
assert_file.not_exist?(logfile0)
logger.error("0" * 15)
assert_file.not_exist?(logfile0)
logger.error("0" * 15)
assert_file.exist?(logfile0)
assert_file.not_exist?(logfile1)
logger.error("0" * 15)
assert_file.not_exist?(logfile1)
logger.error("0" * 15)
assert_file.exist?(logfile1)
assert_file.not_exist?(logfile2)
logger.error("0" * 15)
assert_file.not_exist?(logfile2)
logger.error("0" * 15)
assert_file.exist?(logfile2)
assert_file.not_exist?(logfile3)
logger.error("0" * 15)
assert_file.not_exist?(logfile3)
logger.error("0" * 15)
assert_file.not_exist?(logfile3)
logger.error("0" * 15)
assert_file.not_exist?(logfile3)
logger.close
File.unlink(logfile)
File.unlink(logfile0)
File.unlink(logfile1)
File.unlink(logfile2)
end

def test_shifting_size
tmpfile = Tempfile.new([File.basename(__FILE__, '.*'), '_1.log'])
logfile = tmpfile.path
Expand Down