Class: Logging::Appenders::RollingFile::NumberedRoller

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

Overview

:stopdoc:

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(fn, opts) ⇒ NumberedRoller

Returns a new instance of NumberedRoller.



242
243
244
245
246
247
248
249
250
251
252
# File 'lib/logging/appenders/rolling_file.rb', line 242

def initialize( fn, opts )
  # grab the information we need to properly roll files
  ext = ::File.extname(fn)
  bn = ::File.join(::File.dirname(fn), ::File.basename(fn, ext))
  @rgxp = %r/\.(\d+)#{Regexp.escape(ext)}\z/
  @glob = "#{bn}.*#{ext}"
  @logname_fmt = "#{bn}.%d#{ext}"
  @fn_copy = fn + '._copy_'
  @keep = opts.getopt(:keep, :as => Integer)
  @roll = false
end

Instance Attribute Details

#rollObject

Returns the value of attribute roll.



240
241
242
# File 'lib/logging/appenders/rolling_file.rb', line 240

def roll
  @roll
end

Instance Method Details

#roll_filesObject



254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
# File 'lib/logging/appenders/rolling_file.rb', line 254

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

  files = Dir.glob(@glob).find_all {|fn| @rgxp =~ fn}
  unless files.empty?
    # sort the files in reverse order based on their count number
    files = files.sort do |a,b|
              a = Integer(@rgxp.match(a)[1])
              b = Integer(@rgxp.match(b)[1])
              b <=> a
            end

    # for each file, roll its count number one higher
    files.each do |fn|
      cnt = Integer(@rgxp.match(fn)[1])
      if @keep and cnt >= @keep
        ::File.delete fn
        next
      end
      ::File.rename fn, sprintf(@logname_fmt, cnt+1)
    end
  end

  # finally rename the copied log file
  ::File.rename(@fn_copy, sprintf(@logname_fmt, 1))
ensure
  @roll = false
end