Class: Log4r::RollingFileOutputter

Inherits:
FileOutputter show all
Defined in:
lib/log4r/outputter/rollingfileoutputter.rb

Overview

RollingFileOutputter - subclass of FileOutputter that rolls files on size or time. So, given a filename of “error.log”, the first log file will be “error000001.log”. When its check condition is exceeded, it’ll create and log to “error000002.log”, etc.

Additional hash arguments are:

:maxsize

Maximum size of the file in bytes.

[:maxtime] Maximum age of the file in seconds.

:max_backups

Maxium number of prior log files to maintain. If max_backups is a positive number,

then each time a roll happens, RollingFileOutputter will delete the oldest backup log files in excess
of this number (if any).  So, if max_backups is 10, then a maximum of 11 files will be maintained (the current
log, plus 10 backups). If max_backups is 0, no backups will be kept. If it is negative (the default),
there will be no limit on the number of files created. Note that the sequence numbers will continue to escalate;
old sequence numbers are not reused.
:trunc

If true, deletes ALL existing log files (based on :filename) upon initialization,

and the sequence numbering will start over at 000001. Otherwise continues logging where it left off
last time (i.e. either to the file with the highest sequence number, or a new file, as appropriate).

Instance Attribute Summary collapse

Attributes inherited from FileOutputter

#filename, #trunc

Attributes inherited from Outputter

#formatter, #level, #name

Instance Method Summary collapse

Methods inherited from IOOutputter

#close, #closed?

Methods inherited from Outputter

[], []=, each, each_outputter, #flush, #only_at, stderr, stdout

Constructor Details

#initialize(_name, hash = {}) ⇒ RollingFileOutputter

Returns a new instance of RollingFileOutputter.



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/log4r/outputter/rollingfileoutputter.rb', line 33

def initialize(_name, hash={})
  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
  end
  if hash.has_key?(:max_backups) || hash.has_key?('max_backups') 
    _max_backups = (hash[:max_backups] or hash['max_backups']).to_i
    if _max_backups.class != Fixnum
      raise TypeError, "Argument 'max_backups' must be an Fixnum", caller
    end
    @max_backups = _max_backups
  else
    @max_backups = -1
  end
  # @filename starts out as the file (including path) provided by the user, e.g. "\usr\logs\error.log".
  #   It will get assigned the current log file (including sequence number)   
  # @log_dir is the directory in which we'll log, e.g. "\usr\logs"
  # @file_extension is the file's extension (if any) including any period, e.g. ".log"
  # @core_file_name is the part of the log file's name, sans sequence digits or extension, e.g. "error"
  @log_dir = File.dirname(@filename)
  @file_extension = File.extname(@filename)   # Note: the File API doc comment states that this doesn't include the period, but its examples and behavior do include it. We'll depend on the latter.
  @core_file_name = File.basename(@filename, @file_extension)
  if (@trunc)
    purge_log_files(0)
  end
  @current_sequence_number = get_current_sequence_number()
  makeNewFilename
  # Now @filename points to a properly sequenced filename, which may or may not yet exist.
  open_log_file('a')
  
  # Note: it's possible we're already in excess of our time or size constraint for the current file;
  # no worries -- if a new file needs to be started, it'll happen during the write() call. 
end

Instance Attribute Details

#current_sequence_numberObject (readonly)

Returns the value of attribute current_sequence_number.



31
32
33
# File 'lib/log4r/outputter/rollingfileoutputter.rb', line 31

def current_sequence_number
  @current_sequence_number
end

#max_backupsObject (readonly)

Returns the value of attribute max_backups.



31
32
33
# File 'lib/log4r/outputter/rollingfileoutputter.rb', line 31

def max_backups
  @max_backups
end

#maxsizeObject (readonly)

Returns the value of attribute maxsize.



31
32
33
# File 'lib/log4r/outputter/rollingfileoutputter.rb', line 31

def maxsize
  @maxsize
end

#maxtimeObject (readonly)

Returns the value of attribute maxtime.



31
32
33
# File 'lib/log4r/outputter/rollingfileoutputter.rb', line 31

def maxtime
  @maxtime
end

#start_timeObject (readonly)

Returns the value of attribute start_time.



31
32
33
# File 'lib/log4r/outputter/rollingfileoutputter.rb', line 31

def start_time
  @start_time
end