[前][次][番号順一覧][スレッド一覧]

ruby-changes:32992

From: nobu <ko1@a...>
Date: Fri, 21 Feb 2014 16:11:08 +0900 (JST)
Subject: [ruby-changes:32992] nobu:r45071 (trunk): logger.rb: fix midnight log rotation miss

nobu	2014-02-21 16:11:03 +0900 (Fri, 21 Feb 2014)

  New Revision: 45071

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=45071

  Log:
    logger.rb: fix midnight log rotation miss
    
    * 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]

  Modified files:
    trunk/ChangeLog
    trunk/lib/logger.rb
    trunk/test/logger/test_logdevice.rb
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 45070)
+++ ChangeLog	(revision 45071)
@@ -1,3 +1,11 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Fri Feb 21 16:11:02 2014  Nobuyoshi Nakada  <nobu@r...>
+
+	* 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@g...>
 
 	*  test/monitor/test_monitor.rb: remove unused variables.
Index: lib/logger.rb
===================================================================
--- lib/logger.rb	(revision 45070)
+++ lib/logger.rb	(revision 45071)
@@ -551,6 +551,7 @@ private https://github.com/ruby/ruby/blob/trunk/lib/logger.rb#L551
         @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 https://github.com/ruby/ruby/blob/trunk/lib/logger.rb#L617
       ) 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 https://github.com/ruby/ruby/blob/trunk/lib/logger.rb#L625
         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 https://github.com/ruby/ruby/blob/trunk/lib/logger.rb#L698
       @dev = create_logfile(@filename)
       return true
     end
+  end
+
+  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)
-      case @shift_age
+    def previous_period_end(now, shift_age)
+      case shift_age
       when /^daily$/
         eod(now - 1 * SiD)
       when /^weekly$/
@@ -718,6 +741,10 @@ private https://github.com/ruby/ruby/blob/trunk/lib/logger.rb#L741
     end
   end
 
+  class LogDevice
+    include Period
+  end
+
 
   #
   # == Description
Index: test/logger/test_logdevice.rb
===================================================================
--- test/logger/test_logdevice.rb	(revision 45070)
+++ test/logger/test_logdevice.rb	(revision 45071)
@@ -326,6 +326,42 @@ class TestLogDevice < Test::Unit::TestCa https://github.com/ruby/ruby/blob/trunk/test/logger/test_logdevice.rb#L326
     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)

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]