diff options
author | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2014-02-21 07:11:03 +0000 |
---|---|---|
committer | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2014-02-21 07:11:03 +0000 |
commit | 82d495134c4961d3d6099d1186da978f03bac988 (patch) | |
tree | bd5afaa64ed39a0abec3dc9490d242d71dd68b23 /lib/logger.rb | |
parent | e00bcd1df330bc70af8fa3259be074e861873476 (diff) |
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
Diffstat (limited to 'lib/logger.rb')
-rw-r--r-- | lib/logger.rb | 41 |
1 files changed, 34 insertions, 7 deletions
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 |