diff options
-rw-r--r-- | ChangeLog | 8 | ||||
-rw-r--r-- | lib/logger.rb | 41 | ||||
-rw-r--r-- | test/logger/test_logdevice.rb | 36 |
3 files changed, 78 insertions, 7 deletions
@@ -1,3 +1,11 @@ +Fri Feb 21 16:11:02 2014 Nobuyoshi Nakada <nobu@ruby-lang.org> + + * lib/logger.rb (Logger::LogDevice#check_shift_log): compare the + current time with the time for the next rotation to fix rotation + miss when date changed between the comparison and log writing. + based on the patch by megayu <yuhg2310 AT gmail.com>. + [Fixes GH-539] + Fri Feb 21 10:39:33 2014 SHIBATA Hiroshi <shibata.hiroshi@gmail.com> * test/monitor/test_monitor.rb: remove unused variables. diff --git a/lib/logger.rb b/lib/logger.rb index df15214797..ef9c75d068 100644 --- a/lib/logger.rb +++ b/lib/logger.rb @@ -551,6 +551,7 @@ private @filename = log @shift_age = opt[:shift_age] || 7 @shift_size = opt[:shift_size] || 1048576 + @next_rotate_time = next_rotate_time(Time.now, @shift_age) unless @shift_age.is_a?(Integer) end end @@ -616,8 +617,6 @@ private ) if file.size == 0 end - SiD = 24 * 60 * 60 - def check_shift_log if @shift_age.is_a?(Integer) # Note: always returns false if '0'. @@ -626,9 +625,9 @@ private end else now = Time.now - period_end = previous_period_end(now) - if @dev.stat.mtime <= period_end - lock_shift_log { shift_log_period(period_end) } + if now >= @next_rotate_time + @next_rotate_time = next_rotate_time(now, @shift_age) + lock_shift_log { shift_log_period(previous_period_end(now, @shift_age)) } end end end @@ -699,9 +698,33 @@ private @dev = create_logfile(@filename) return true end + end - def previous_period_end(now) - case @shift_age + module Period + module_function + + SiD = 24 * 60 * 60 + + def next_rotate_time(now, shift_age) + case shift_age + when /^daily$/ + t = Time.mktime(now.year, now.month, now.mday) + SiD + when /^weekly$/ + t = Time.mktime(now.year, now.month, now.mday) + SiD * (7 - now.wday) + when /^monthly$/ + t = Time.mktime(now.year, now.month, 1) + SiD * 31 + mday = (1 if t.mday > 1) + if mday + t = Time.mktime(t.year, t.month, mday) + end + else + return now + end + t + end + + def previous_period_end(now, shift_age) + case shift_age when /^daily$/ eod(now - 1 * SiD) when /^weekly$/ @@ -718,6 +741,10 @@ private end end + class LogDevice + include Period + end + # # == Description diff --git a/test/logger/test_logdevice.rb b/test/logger/test_logdevice.rb index 7999135dd9..3db3a5da45 100644 --- a/test/logger/test_logdevice.rb +++ b/test/logger/test_logdevice.rb @@ -326,6 +326,42 @@ class TestLogDevice < Test::Unit::TestCase end end unless /mswin|mingw/ =~ RUBY_PLATFORM + def test_shifting_midnight + Dir.mktmpdir do |tmpdir| + assert_ruby_status([*%W"--disable=gems -rlogger -C#{tmpdir} -"], <<-'end;') + begin + module FakeTime + attr_accessor :now + end + + class << Time + prepend FakeTime + end + + log = "log" + File.open(log, "w") {} + File.utime(*[Time.mktime(2014, 1, 1, 23, 59, 59)]*2, log) + + Time.now = Time.mktime(2014, 1, 2, 23, 59, 59, 999000) + dev = Logger::LogDevice.new(log, shift_age: 'daily') + dev.write("#{Time.now} hello-1\n") + + File.utime(*[Time.mktime(2014, 1, 3, 0, 0, 0, 121000)]*2, log) + Time.now = Time.mktime(2014, 1, 3, 1, 1, 1) + dev.write("#{Time.now} hello-2\n") + ensure + dev.close + end + end; + + bug = '[GH-539]' + log = File.join(tmpdir, "log") + assert_match(/hello-2/, File.read(log)) + assert_file.for(bug).exist?(log+".20140102") + assert_match(/hello-1/, File.read(log+".20140102"), bug) + end + end + private def run_children(n, args, src) |