1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

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]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@45071 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2014-02-21 07:11:03 +00:00
parent e00bcd1df3
commit 82d495134c
3 changed files with 78 additions and 7 deletions

View file

@ -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.

View file

@ -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

View file

@ -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)