Class: Logging::Appenders::RollingFile::DateRoller
- Inherits:
-
Object
- Object
- Logging::Appenders::RollingFile::DateRoller
- Defined in:
- lib/logging/appenders/rolling_file.rb
Instance Attribute Summary collapse
-
#roll ⇒ Object
Returns the value of attribute roll.
Instance Method Summary collapse
-
#initialize(fn, opts) ⇒ DateRoller
constructor
A new instance of DateRoller.
- #roll_files ⇒ Object
Constructor Details
#initialize(fn, opts) ⇒ DateRoller
Returns a new instance of DateRoller.
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 |
# File 'lib/logging/appenders/rolling_file.rb', line 287 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
#roll ⇒ Object
Returns the value of attribute roll.
285 286 287 |
# File 'lib/logging/appenders/rolling_file.rb', line 285 def roll @roll end |
Instance Method Details
#roll_files ⇒ Object
307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 |
# File 'lib/logging/appenders/rolling_file.rb', line 307 def roll_files return unless @roll and ::File.exist?(@fn_copy) # rename 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 |