Class: Logging::Appenders::RollingFile::DateRoller

Inherits:
Object
  • Object
show all
Defined in:
lib/logging/appenders/rolling_file.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(fn, opts) ⇒ DateRoller

Returns a new instance of DateRoller.



284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
# File 'lib/logging/appenders/rolling_file.rb', line 284

def initialize( fn, opts )
  @fn_copy = fn + '._copy_'
  @roll = false
  @keep = opts.getopt(:keep, :as => Integer)

  ext = ::File.extname(fn)
  bn = ::File.join(::File.dirname(fn), ::File.basename(fn, ext))

  if @keep
    @rgxp = %r/\.(\d+)(-\d+)?#{Regexp.escape(ext)}\z/
    @glob = "#{bn}.*#{ext}"
  end

  if %w[daily weekly monthly].include?(opts.getopt(:age)) and !opts.getopt(:size)
    @logname_fmt = "#{bn}.%Y%m%d#{ext}"
  else
    @logname_fmt = "#{bn}.%Y%m%d-%H%M%S#{ext}"
  end
end

Instance Attribute Details

#rollObject

Returns the value of attribute roll.



282
283
284
# File 'lib/logging/appenders/rolling_file.rb', line 282

def roll
  @roll
end

Instance Method Details

#roll_filesObject



304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
# File 'lib/logging/appenders/rolling_file.rb', line 304

def roll_files
  return unless @roll and ::File.exist?(@fn_copy)

  # reanme the copied log file
  ::File.rename(@fn_copy, Time.now.strftime(@logname_fmt))

  # prune old log files
  if @keep
    files = Dir.glob(@glob).find_all {|fn| @rgxp =~ fn}
    length = files.length
    if length > @keep
      files.sort {|a,b| b <=> a}.last(length-@keep).each {|fn| ::File.delete fn}
    end
  end
ensure
  @roll = false
end