Module: Yologga::Patch::Ruby_2_4
- Included in:
- Ruby_2_5
- Defined in:
- lib/yologga/patch/ruby_2_4.rb
Overview
rubocop:disable Naming/ClassAndModuleCamelCase
Constant Summary collapse
- SiD =
rubocop:enable Naming/ClassAndModuleCamelCase
Logger::Period::SiD
Instance Method Summary collapse
- #initialize(log = nil, shift_age: nil, shift_size: nil, shift_period_suffix: nil) ⇒ Object
- #next_rotate_time(now, shift_age) ⇒ Object
- #previous_period_end(now, shift_age) ⇒ Object
- #shift_log_period(period_end) ⇒ Object
Instance Method Details
#initialize(log = nil, shift_age: nil, shift_size: nil, shift_period_suffix: nil) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/yologga/patch/ruby_2_4.rb', line 12 def initialize(log = nil, shift_age: nil, shift_size: nil, shift_period_suffix: nil) @dev = @filename = @shift_age = @shift_size = @shift_period_suffix = nil mon_initialize set_dev(log) if @filename @shift_age = shift_age || 7 @shift_size = shift_size || 1048576 suffix = shift_age == "hourly" ? "%Y%m%d%H" : (shift_period_suffix || "%Y%m%d") @shift_period_suffix = suffix 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 |
#next_rotate_time(now, shift_age) ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/yologga/patch/ruby_2_4.rb', line 50 def next_rotate_time(now, shift_age) case shift_age when "hourly" t = Time.mktime(now.year, now.month, now.mday, now.hour) + SiD / 24 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 * 32 return Time.mktime(t.year, t.month, 1) else return now end if t.min.nonzero? or t.sec.nonzero? min = t.min t = Time.mktime(t.year, t.month, t.mday, t.hour) t += (SiD / 24) if min > 30 end t end |
#previous_period_end(now, shift_age) ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/yologga/patch/ruby_2_4.rb', line 72 def previous_period_end(now, shift_age) case shift_age when "hourly" t = Time.mktime(now.year, now.month, now.mday, now.hour) - 1 when "daily" t = Time.mktime(now.year, now.month, now.mday) - 1 when "weekly" t = Time.mktime(now.year, now.month, now.mday) - (SiD * now.wday + 1) when "monthly" t = Time.mktime(now.year, now.month, 1) - 1 else return now end Time.mktime(t.year, t.month, t.mday, t.hour, 59, 59) end |
#shift_log_period(period_end) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/yologga/patch/ruby_2_4.rb', line 29 def shift_log_period(period_end) suffix = period_end.strftime(@shift_period_suffix) age_file = "#{@filename}.#{suffix}" if FileTest.exist?(age_file) # try to avoid filename crash caused by Timestamp change. idx = 0 # .99 can be overridden; avoid too much file search with 'loop do' while idx < 100 idx += 1 age_file = "#{@filename}.#{suffix}.#{idx}" break unless FileTest.exist?(age_file) end end @dev.close rescue nil File.rename(@filename.to_s, age_file) @dev = create_logfile(@filename) Yologga::Gzip.new(age_file).call if Yologga.gzip Yologga::LogsLifetime.new(@filename).call true end |