FileOutputter
RollingFileOutputter - subclass of FileOutputter that rolls files on size or time. Additional hash arguments are:
(Not documented)
# File log4r/outputter/rollingfileoutputter.rb, line 19 def initialize(_name, hash={}) @count = 0 super( _name, hash.merge({:create => false}) ) if hash.has_key?(:maxsize) || hash.has_key?('maxsize') _maxsize = (hash[:maxsize] or hash['maxsize']).to_i if _maxsize.class != Fixnum raise TypeError, "Argument 'maxsize' must be an Fixnum", caller end if _maxsize == 0 raise TypeError, "Argument 'maxsize' must be > 0", caller end @maxsize = _maxsize end if hash.has_key?(:maxtime) || hash.has_key?('maxtime') _maxtime = (hash[:maxtime] or hash['maxtime']).to_i if _maxtime.class != Fixnum raise TypeError, "Argument 'maxtime' must be an Fixnum", caller end if _maxtime == 0 raise TypeError, "Argument 'maxtime' must be > 0", caller end @maxtime = _maxtime @startTime = Time.now end @baseFilename = File.basename(@filename) # roll immediately so all files are of the form "000001-@baseFilename" roll # initialize the file size counter @datasize = 0 end
construct a new filename from the count and baseFilname
# File log4r/outputter/rollingfileoutputter.rb, line 64 def makeNewFilename # note use of hard coded 6 digit counter width - is this enough files? pad = "0" * (6 - @count.to_s.length) + count.to_s newbase = @baseFilename.sub(/(\.\w*)$/, pad + '\1') @filename = File.join(File.dirname(@filename), newbase) Logger.log_internal {"File #{@filename} created"} end
does the file require a roll?
# File log4r/outputter/rollingfileoutputter.rb, line 73 def requiresRoll if !@maxsize.nil? && @datasize > @maxsize @datasize = 0 return true end if !@maxtime.nil? && (Time.now - @startTime) > @maxtime @startTime = Time.now return true end false end
roll the file
# File log4r/outputter/rollingfileoutputter.rb, line 86 def roll begin # If @baseFilename == @filename, then this method is about to # try to close out a file that is not actually opened because # fileoutputter has been called with the parameter roll=true if ( @baseFilename != @filename ) then @out.close end rescue Logger.log_internal { "RollingFileOutputter '#{@name}' could not close #{@filename}" } end @count += 1 makeNewFilename @out = File.new(@filename, (@trunc ? "w" : "a")) end
perform the write
# File log4r/outputter/rollingfileoutputter.rb, line 55 def write(data) # we have to keep track of the file size ourselves - File.size doesn't # seem to report the correct size when the size changes rapidly @datasize += data.size + 1 # the 1 is for newline super roll if requiresRoll end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.