Class: Logging::Appenders::RollingFile
- Inherits:
-
IO
- Object
- Logging::Appender
- IO
- Logging::Appenders::RollingFile
- Defined in:
- lib/logging/appenders/rolling_file.rb
Overview
An appender that writes to a file and ensures that the file size or age never exceeds some user specified level.
The goal of this class is to write log messages to a file. When the file age or size exceeds a given limit then the log file is closed, the name is changed to indicate it is an older log file, and a new log file is created.
The name of the log file is changed by inserting the age of the log file (as a single number) between the log file name and the extension. If the file has no extension then the number is appended to the filename. Here is a simple example:
/var/log/ruby.log => /var/log/ruby.1.log
New log messages will be appended to a newly opened log file of the same name (/var/log/ruby.log
in our example above). The age number for all older log files is incremented when the log file is rolled. The number of older log files to keep can be given, otherwise all the log files are kept.
The actual process of rolling all the log file names can be expensive if there are many, many older log files to process.
Constant Summary
Constants included from Buffering
Buffering::DEFAULT_BUFFER_SIZE
Instance Attribute Summary
Attributes included from Buffering
Attributes inherited from Logging::Appender
Instance Method Summary collapse
-
#filename ⇒ Object
Returns the path to the logfile.
-
#initialize(name, opts = {}) ⇒ RollingFile
constructor
call-seq: RollingFile.new( name, opts ).
Methods inherited from IO
Methods included from Buffering
Methods inherited from Logging::Appender
#<<, #append, #close, #closed?, #flush, #inspect
Constructor Details
#initialize(name, opts = {}) ⇒ RollingFile
call-seq:
RollingFile.new( name, opts )
Creates a new Rolling File Appender. The name is the unique Appender name used to retrieve this appender from the Appender hash. The only required option is the filename to use for creating log files.
[:filename] The base filename to use when constructing new log
filenames.
The following options are optional:
[:layout] The Layout that will be used by this appender. The Basic
layout will be used if none is given.
[:truncate] When set to true any existing log files will be rolled
immediately and a new, empty log file will be created.
[:size] The maximum allowed size (in bytes) of a log file before
it is rolled.
[:age] The maximum age (in seconds) of a log file before it is
rolled. The age can also be given as 'daily', 'weekly',
or 'monthly'.
[:keep] The number of rolled log files to keep.
[:safe] When set to true, extra checks are made to ensure that
only once process can roll the log files; this option
should only be used when multiple processes will be
logging to the same log file (does not work on Windows)
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 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/logging/appenders/rolling_file.rb', line 57 def initialize( name, opts = {} ) # raise an error if a filename was not given @fn = opts.getopt(:filename, name) raise ArgumentError, 'no filename was given' if @fn.nil? ::Logging::Appenders::File.assert_valid_logfile(@fn) # 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}" # grab our options @keep = opts.getopt(:keep, :as => Integer) @size = opts.getopt(:size, :as => Integer) @lockfile = if HAVE_LOCKFILE and opts.getopt(:safe, false) ::Lockfile.new( @fn + '.lck', :retries => 1, :timeout => 2 ) end code = 'def sufficiently_aged?() false end' @age_fn = @fn + '.age' case @age = opts.getopt(:age) when 'daily' FileUtils.touch(@age_fn) unless test(?f, @age_fn) code = <<-CODE def sufficiently_aged? now = Time.now start = ::File.mtime(@age_fn) if (now.day != start.day) or (now - start) > 86400 return true end false end CODE when 'weekly' FileUtils.touch(@age_fn) unless test(?f, @age_fn) code = <<-CODE def sufficiently_aged? if (Time.now - ::File.mtime(@age_fn)) > 604800 return true end false end CODE when 'monthly' FileUtils.touch(@age_fn) unless test(?f, @age_fn) code = <<-CODE def sufficiently_aged? now = Time.now start = ::File.mtime(@age_fn) if (now.month != start.month) or (now - start) > 2678400 return true end false end CODE when Integer, String @age = Integer(@age) FileUtils.touch(@age_fn) unless test(?f, @age_fn) code = <<-CODE def sufficiently_aged? if (Time.now - ::File.mtime(@age_fn)) > @age return true end false end CODE end = class << self; self end .class_eval code, __FILE__, __LINE__ # if the truncate flag was set to true, then roll roll_now = opts.getopt(:truncate, false) roll_files if roll_now super(name, open_logfile, opts) end |
Instance Method Details
#filename ⇒ Object
Returns the path to the logfile.
144 |
# File 'lib/logging/appenders/rolling_file.rb', line 144 def filename() @fn.dup end |