Class: Logsly::Logging182::Appenders::RollingFile::NumberedRoller
- Inherits:
-
Object
- Object
- Logsly::Logging182::Appenders::RollingFile::NumberedRoller
- Defined in:
- lib/logsly/logging182/appenders/rolling_file.rb
Overview
:stopdoc:
Instance Attribute Summary collapse
-
#roll ⇒ Object
Returns the value of attribute roll.
Instance Method Summary collapse
-
#initialize(fn, opts) ⇒ NumberedRoller
constructor
A new instance of NumberedRoller.
- #roll_files ⇒ Object
Constructor Details
#initialize(fn, opts) ⇒ NumberedRoller
Returns a new instance of NumberedRoller.
251 252 253 254 255 256 257 258 259 260 261 |
# File 'lib/logsly/logging182/appenders/rolling_file.rb', line 251 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
#roll ⇒ Object
Returns the value of attribute roll.
249 250 251 |
# File 'lib/logsly/logging182/appenders/rolling_file.rb', line 249 def roll @roll end |
Instance Method Details
#roll_files ⇒ Object
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 |
# File 'lib/logsly/logging182/appenders/rolling_file.rb', line 263 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 |