mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
* lib/logger.rb: Add shift_period_suffix option [Fix GH-10772]
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@54639 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
a040953386
commit
2c6f15b1ad
2 changed files with 41 additions and 7 deletions
|
@ -366,13 +366,17 @@ class Logger
|
|||
# Logging formatter. Default values is an instance of Logger::Formatter.
|
||||
# +datetime_format+::
|
||||
# Date and time format. Default value is '%Y-%m-%d %H:%M:%S'.
|
||||
# +shift_period_suffix+::
|
||||
# The log file suffix format for +daily+, +weekly+ or +monthly+ rotation.
|
||||
# Default is '%Y%m%d'.
|
||||
#
|
||||
# === Description
|
||||
#
|
||||
# Create an instance.
|
||||
#
|
||||
def initialize(logdev, shift_age = 0, shift_size = 1048576, level: DEBUG,
|
||||
progname: nil, formatter: nil, datetime_format: nil)
|
||||
progname: nil, formatter: nil, datetime_format: nil,
|
||||
shift_period_suffix: '%Y%m%d')
|
||||
self.level = level
|
||||
self.progname = progname
|
||||
@default_formatter = Formatter.new
|
||||
|
@ -381,7 +385,8 @@ class Logger
|
|||
@logdev = nil
|
||||
if logdev
|
||||
@logdev = LogDevice.new(logdev, :shift_age => shift_age,
|
||||
:shift_size => shift_size)
|
||||
:shift_size => shift_size,
|
||||
:shift_period_suffix => shift_period_suffix)
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -660,13 +665,14 @@ private
|
|||
attr_reader :filename
|
||||
include MonitorMixin
|
||||
|
||||
def initialize(log = nil, shift_age: nil, shift_size: nil)
|
||||
@dev = @filename = @shift_age = @shift_size = nil
|
||||
def initialize(log = nil, shift_age: nil, shift_size: nil, shift_period_suffix: nil)
|
||||
@dev = @filename = @shift_age = @shift_size = @shift_period_suffix = nil
|
||||
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'
|
||||
@next_rotate_time = next_rotate_time(Time.now, @shift_age) unless @shift_age.is_a?(Integer)
|
||||
end
|
||||
end
|
||||
|
@ -822,15 +828,15 @@ private
|
|||
end
|
||||
|
||||
def shift_log_period(period_end)
|
||||
postfix = period_end.strftime("%Y%m%d") # YYYYMMDD
|
||||
age_file = "#{@filename}.#{postfix}"
|
||||
suffix = period_end.strftime(@shift_period_suffix)
|
||||
age_file = "#{@filename}.#{suffix}"
|
||||
if FileTest.exist?(age_file)
|
||||
# try to avoid filename crash caused by Timestamp change.
|
||||
idx = 0
|
||||
# .99 can be overridden; avoid too much file search with 'loop do'
|
||||
while idx < 100
|
||||
idx += 1
|
||||
age_file = "#{@filename}.#{postfix}.#{idx}"
|
||||
age_file = "#{@filename}.#{suffix}.#{idx}"
|
||||
break unless FileTest.exist?(age_file)
|
||||
end
|
||||
end
|
||||
|
|
|
@ -293,6 +293,34 @@ class TestLogDevice < Test::Unit::TestCase
|
|||
end
|
||||
end
|
||||
|
||||
def test_shifting_period_suffix
|
||||
# shift_age other than 'daily', 'weekly', and 'monthly' means 'everytime'
|
||||
{
|
||||
"%Y%m%d" => Logger.new(@filename, 'now', 1048576), # default
|
||||
"%Y-%m-%d" => Logger.new(@filename, 'now', 1048576, shift_period_suffix: '%Y-%m-%d')
|
||||
}.each do |format, logger|
|
||||
begin
|
||||
yyyymmdd = Time.now.strftime(format)
|
||||
filename1 = @filename + ".#{yyyymmdd}"
|
||||
filename2 = @filename + ".#{yyyymmdd}.1"
|
||||
filename3 = @filename + ".#{yyyymmdd}.2"
|
||||
logger.info("0" * 15)
|
||||
logger.info("0" * 15)
|
||||
logger.info("0" * 15)
|
||||
logger.info("0" * 15)
|
||||
assert(File.exist?(@filename))
|
||||
assert(File.exist?(filename1))
|
||||
assert(File.exist?(filename2))
|
||||
assert(File.exist?(filename3))
|
||||
ensure
|
||||
logger.close if logger
|
||||
[filename1, filename2, filename3].each do |filename|
|
||||
File.unlink(filename) if File.exist?(filename)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def test_shifting_size_in_multiprocess
|
||||
tmpfile = Tempfile.new([File.basename(__FILE__, '.*'), '_1.log'])
|
||||
logfile = tmpfile.path
|
||||
|
|
Loading…
Add table
Reference in a new issue