Method: Logging::Appenders::RollingFile#initialize
- Defined in:
- lib/logging/appenders/rolling_file.rb
#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 “rolling” portion of the filename can be configured via some simple pattern templates. For numbered rolling, you can use Logging::Appenders::RollingFile.{{.%d}
"logname{{.%d}}.log" => ["logname.log", "logname.1.log", "logname.2.log" ...]
"logname.log{{-%d}}" => ["logname.log", "logname.log-1", "logname.log-2" ...]
And for date rolling you can use ‘strftime` patterns:
"logname{{.%Y%m%d}}.log" => ["logname.log, "logname.20130626.log" ...]
"logname{{.%Y-%m-%dT%H:%M:%S}}.log" => ["logname.log, "logname.2013-06-26T22:03:31.log" ...]
If the defaults suit you fine, just pass in the :roll_by option and use your normal log filename without any pattern template.
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.
[:roll_by] How to name the rolled log files. This can be 'number' or
'date'.
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 |
# File 'lib/logging/appenders/rolling_file.rb', line 86 def initialize( name, opts = {} ) @roller = Roller.new( opts.fetch(:filename, name), age: opts.fetch(:age, nil), size: opts.fetch(:size, nil), roll_by: opts.fetch(:roll_by, nil), keep: opts.fetch(:keep, nil) ) # grab our options @size = opts.fetch(:size, nil) @size = Integer(@size) unless @size.nil? @age_fn = self.filename + '.age' @age_fn_mtime = nil @age = opts.fetch(:age, nil) # create our `sufficiently_aged?` method build_singleton_methods FileUtils.touch(@age_fn) if @age && !::File.file?(@age_fn) # we are opening the file in read/write mode so that a shared lock can # be used on the file descriptor => http://pubs.opengroup.org/onlinepubs/009695399/functions/fcntl.html self.encoding = opts.fetch(:encoding, self.encoding) io = open_file super(name, io, opts) # if the truncate flag was set to true, then roll roll_now = opts.fetch(:truncate, false) if roll_now copy_truncate @roller.roll_files end end |