Permalink
Browse files

* 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
  • Loading branch information...
sonots committed Apr 18, 2016
1 parent a040953 commit 2c6f15b1ad90af37d7e0eefff7b3f5262e0a4c0b
Showing with 41 additions and 7 deletions.
  1. +13 −7 lib/logger.rb
  2. +28 −0 test/logger/test_logdevice.rb
View
@@ -366,13 +366,17 @@ def fatal?; @level <= FATAL; end
# 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 @@ def initialize(logdev, shift_age = 0, shift_size = 1048576, level: DEBUG,
@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 @@ class LogDevice
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 @@ def shift_log_age
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 @@ def test_shifting_age
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

0 comments on commit 2c6f15b

Please sign in to comment.