ruby-changes:42565
From: sonots <ko1@a...>
Date: Mon, 18 Apr 2016 23:10:56 +0900 (JST)
Subject: [ruby-changes:42565] sonots:r54639 (trunk): * lib/logger.rb: Add shift_period_suffix option [Fix GH-10772]
sonots 2016-04-19 00:07:31 +0900 (Tue, 19 Apr 2016) New Revision: 54639 https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=54639 Log: * lib/logger.rb: Add shift_period_suffix option [Fix GH-10772] Modified files: trunk/lib/logger.rb trunk/test/logger/test_logdevice.rb Index: lib/logger.rb =================================================================== --- lib/logger.rb (revision 54638) +++ lib/logger.rb (revision 54639) @@ -366,13 +366,17 @@ class Logger https://github.com/ruby/ruby/blob/trunk/lib/logger.rb#L366 # 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 https://github.com/ruby/ruby/blob/trunk/lib/logger.rb#L385 @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 https://github.com/ruby/ruby/blob/trunk/lib/logger.rb#L665 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 https://github.com/ruby/ruby/blob/trunk/lib/logger.rb#L828 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 Index: test/logger/test_logdevice.rb =================================================================== --- test/logger/test_logdevice.rb (revision 54638) +++ test/logger/test_logdevice.rb (revision 54639) @@ -293,6 +293,34 @@ class TestLogDevice < Test::Unit::TestCa https://github.com/ruby/ruby/blob/trunk/test/logger/test_logdevice.rb#L293 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 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/