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

ruby-changes:44742

From: nobu <ko1@a...>
Date: Wed, 16 Nov 2016 15:59:48 +0900 (JST)
Subject: [ruby-changes:44742] nobu:r56815 (trunk): logger.rb: fix next rotate time

nobu	2016-11-16 15:59:42 +0900 (Wed, 16 Nov 2016)

  New Revision: 56815

  https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=56815

  Log:
    logger.rb: fix next rotate time
    
    * lib/logger.rb (Logger::LogDevice#initialize): calculate next
      rotate time based on the mtime of the last existing file.
      [ruby-dev:49881] [Bug #12948]
    
    Author: Tsukasa Oishi <tsukasa.oishi@g...>

  Modified files:
    trunk/lib/logger.rb
    trunk/test/logger/test_logdevice.rb
Index: lib/logger.rb
===================================================================
--- lib/logger.rb	(revision 56814)
+++ lib/logger.rb	(revision 56815)
@@ -673,7 +673,11 @@ private https://github.com/ruby/ruby/blob/trunk/lib/logger.rb#L673
         @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)
+
+        unless @shift_age.is_a?(Integer)
+          base_time = @dev.respond_to?(:stat) ? @dev.stat.mtime : Time.now
+          @next_rotate_time = next_rotate_time(base_time, @shift_age)
+        end
       end
     end
 
Index: test/logger/test_logdevice.rb
===================================================================
--- test/logger/test_logdevice.rb	(revision 56814)
+++ test/logger/test_logdevice.rb	(revision 56815)
@@ -448,14 +448,16 @@ class TestLogDevice < Test::Unit::TestCa https://github.com/ruby/ruby/blob/trunk/test/logger/test_logdevice.rb#L448
 
           log = "log"
           File.open(log, "w") {}
-          File.utime(*[Time.mktime(2014, 1, 1, 23, 59, 59)]*2, log)
+          File.utime(*[Time.mktime(2014, 1, 2, 0, 0, 0)]*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.now, Time.now, log)
 
           Time.now = Time.mktime(2014, 1, 3, 1, 1, 1)
           dev.write("#{Time.now} hello-2\n")
+          File.utime(Time.now, Time.now, log)
         ensure
           dev.close
         end
@@ -488,8 +490,9 @@ class TestLogDevice < Test::Unit::TestCa https://github.com/ruby/ruby/blob/trunk/test/logger/test_logdevice.rb#L490
 
           log = "log"
           File.open(log, "w") {}
-
           Time.now = Time.utc(2015, 12, 14, 0, 1, 1)
+          File.utime(Time.now, Time.now, log)
+
           dev = Logger::LogDevice.new("log", shift_age: 'weekly')
 
           Time.now = Time.utc(2015, 12, 19, 12, 34, 56)
@@ -497,8 +500,8 @@ class TestLogDevice < Test::Unit::TestCa https://github.com/ruby/ruby/blob/trunk/test/logger/test_logdevice.rb#L500
           File.utime(Time.now, Time.now, log)
 
           Time.now = Time.utc(2015, 12, 20, 0, 1, 1)
-          File.utime(Time.now, Time.now, log)
           dev.write("#{Time.now} hello-2\n")
+          File.utime(Time.now, Time.now, log)
         ensure
           dev.close if dev
         end
@@ -532,8 +535,9 @@ class TestLogDevice < Test::Unit::TestCa https://github.com/ruby/ruby/blob/trunk/test/logger/test_logdevice.rb#L535
 
           log = "log"
           File.open(log, "w") {}
-
           Time.now = Time.utc(2015, 12, 14, 0, 1, 1)
+          File.utime(Time.now, Time.now, log)
+
           dev = Logger::LogDevice.new("log", shift_age: 'monthly')
 
           Time.now = Time.utc(2015, 12, 31, 12, 34, 56)
@@ -541,8 +545,8 @@ class TestLogDevice < Test::Unit::TestCa https://github.com/ruby/ruby/blob/trunk/test/logger/test_logdevice.rb#L545
           File.utime(Time.now, Time.now, log)
 
           Time.now = Time.utc(2016, 1, 1, 0, 1, 1)
-          File.utime(Time.now, Time.now, log)
           dev.write("#{Time.now} hello-2\n")
+          File.utime(Time.now, Time.now, log)
         ensure
           dev.close if dev
         end
@@ -585,8 +589,8 @@ class TestLogDevice < Test::Unit::TestCa https://github.com/ruby/ruby/blob/trunk/test/logger/test_logdevice.rb#L589
           File.utime(*[Time.mktime(2014, 3, 30, 0, 2, 3)]*2, log)
 
           Time.now = Time.mktime(2014, 3, 31, 0, 1, 1)
-          File.utime(Time.now, Time.now, log)
           dev.write("#{Time.now} hello-2\n")
+          File.utime(Time.now, Time.now, log)
         ensure
           dev.close
         end
@@ -615,10 +619,12 @@ class TestLogDevice < Test::Unit::TestCa https://github.com/ruby/ruby/blob/trunk/test/logger/test_logdevice.rb#L619
 
           log = "log"
           File.open(log, "w") {}
-
           Time.now = Time.mktime(2015, 10, 25, 0, 1, 1)
+          File.utime(Time.now, Time.now, log)
+
           dev = Logger::LogDevice.new("log", shift_age: 'weekly')
           dev.write("#{Time.now} hello-1\n")
+          File.utime(Time.now, Time.now, log)
         ensure
           dev.close if dev
         end
@@ -644,8 +650,9 @@ class TestLogDevice < Test::Unit::TestCa https://github.com/ruby/ruby/blob/trunk/test/logger/test_logdevice.rb#L650
 
           log = "log"
           File.open(log, "w") {}
-
           Time.now = Time.utc(2016, 9, 1, 0, 1, 1)
+          File.utime(Time.now, Time.now, log)
+
           dev = Logger::LogDevice.new("log", shift_age: 'monthly')
 
           Time.now = Time.utc(2016, 9, 8, 7, 6, 5)
@@ -653,12 +660,12 @@ class TestLogDevice < Test::Unit::TestCa https://github.com/ruby/ruby/blob/trunk/test/logger/test_logdevice.rb#L660
           File.utime(Time.now, Time.now, log)
 
           Time.now = Time.utc(2016, 10, 9, 8, 7, 6)
-          File.utime(Time.now, Time.now, log)
           dev.write("#{Time.now} hello-2\n")
+          File.utime(Time.now, Time.now, log)
 
           Time.now = Time.utc(2016, 10, 9, 8, 7, 7)
-          File.utime(Time.now, Time.now, log)
           dev.write("#{Time.now} hello-3\n")
+          File.utime(Time.now, Time.now, log)
         ensure
           dev.close if dev
         end
@@ -677,6 +684,144 @@ class TestLogDevice < Test::Unit::TestCa https://github.com/ruby/ruby/blob/trunk/test/logger/test_logdevice.rb#L684
     end
   end if env_tz_works
 
+  def test_shifting_midnight_exist_file
+    Dir.mktmpdir do |tmpdir|
+      assert_in_out_err([*%W"--disable=gems -rlogger -C#{tmpdir} -"], "#{<<-"begin;"}\n#{<<-'end;'}")
+      begin;
+        begin
+          module FakeTime
+            attr_accessor :now
+          end
+
+          class << Time
+            prepend FakeTime
+          end
+
+          log = "log"
+          File.open(log, "w") {}
+          File.utime(*[Time.mktime(2014, 1, 2, 0, 0, 0)]*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")
+          dev.close
+          File.utime(Time.now, Time.now, log)
+
+          Time.now = Time.mktime(2014, 1, 3, 1, 1, 1)
+          dev = Logger::LogDevice.new(log, shift_age: 'daily')
+          dev.write("#{Time.now} hello-2\n")
+          File.utime(Time.now, Time.now, log)
+        ensure
+          dev.close
+        end
+      end;
+
+      bug = '[GH-539]'
+      log = File.join(tmpdir, "log")
+      cont = File.read(log)
+      assert_match(/hello-2/, cont)
+      assert_not_match(/hello-1/, cont)
+      assert_file.for(bug).exist?(log+".20140102")
+      assert_match(/hello-1/, File.read(log+".20140102"), bug)
+    end
+  end
+
+  env_tz_works = /linux|darwin|freebsd/ =~ RUBY_PLATFORM # borrow from test/ruby/test_time_tz.rb
+
+  def test_shifting_weekly_exist_file
+    Dir.mktmpdir do |tmpdir|
+      assert_in_out_err([{"TZ"=>"UTC"}, *%W"-rlogger -C#{tmpdir} -"], "#{<<-"begin;"}\n#{<<-'end;'}")
+      begin;
+        begin
+          module FakeTime
+            attr_accessor :now
+          end
+
+          class << Time
+            prepend FakeTime
+          end
+
+          log = "log"
+          File.open(log, "w") {}
+          Time.now = Time.utc(2015, 12, 14, 0, 1, 1)
+          File.utime(Time.now, Time.now, log)
+
+          dev = Logger::LogDevice.new("log", shift_age: 'weekly')
+
+          Time.now = Time.utc(2015, 12, 19, 12, 34, 56)
+          dev.write("#{Time.now} hello-1\n")
+          dev.close
+          File.utime(Time.now, Time.now, log)
+
+          Time.now = Time.utc(2015, 12, 20, 0, 1, 1)
+          dev = Logger::LogDevice.new("log", shift_age: 'weekly')
+          dev.write("#{Time.now} hello-2\n")
+          File.utime(Time.now, Time.now, log)
+        ensure
+          dev.close if dev
+        end
+      end;
+      log = File.join(tmpdir, "log")
+      cont = File.read(log)
+      assert_match(/hello-2/, cont)
+      assert_not_match(/hello-1/, cont)
+      log = Dir.glob(log+".*")
+      assert_equal(1, log.size)
+      log, = *log
+      cont = File.read(log)
+      assert_match(/hello-1/, cont)
+      assert_equal("2015-12-19", cont[/^[-\d]+/])
+      assert_equal("20151219", log[/\d+\z/])
+    end
+  end if env_tz_works
+
+  def test_shifting_monthly_exist_file
+    Dir.mktmpdir do |tmpdir|
+      assert_in_out_err([{"TZ"=>"UTC"}, *%W"-rlogger -C#{tmpdir} -"], "#{<<-"begin;"}\n#{<<-'end;'}")
+      begin;
+        begin
+          module FakeTime
+            attr_accessor :now
+          end
+
+          class << Time
+            prepend FakeTime
+          end
+
+          log = "log"
+          File.open(log, "w") {}
+          Time.now = Time.utc(2015, 12, 14, 0, 1, 1)
+          File.utime(Time.now, Time.now, log)
+
+          dev = Logger::LogDevice.new("log", shift_age: 'monthly')
+
+          Time.now = Time.utc(2015, 12, 31, 12, 34, 56)
+          dev.write("#{Time.now} hello-1\n")
+          dev.close
+          File.utime(Time.now, Time.now, log)
+
+          Time.now = Time.utc(2016, 1, 1, 0, 1, 1)
+          dev = Logger::LogDevice.new("log", shift_age: 'monthly')
+          dev.write("#{Time.now} hello-2\n")
+          File.utime(Time.now, Time.now, log)
+        ensure
+          dev.close if dev
+        end
+      end;
+      log = File.join(tmpdir, "log")
+      cont = File.read(log)
+      assert_match(/hello-2/, cont)
+      assert_not_match(/hello-1/, cont)
+      log = Dir.glob(log+".*")
+      assert_equal(1, log.size)
+      log, = *log
+      cont = File.read(log)
+      assert_match(/hello-1/, cont)
+      assert_equal("2015-12-31", cont[/^[-\d]+/])
+      assert_equal("20151231", log[/\d+\z/])
+    end
+  end if env_tz_works
+
   private
 
   def run_children(n, args, src)

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

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