Class: Lumberjack::Device::RollingLogFile
- Inherits:
-
LogFile
- Object
- Lumberjack::Device
- Writer
- LogFile
- Lumberjack::Device::RollingLogFile
- Defined in:
- lib/lumberjack/device/rolling_log_file.rb
Overview
This is an abstract class for a device that appends entries to a file and periodically archives the existing file and starts a one. Subclasses must implement the roll_file? and archive_file_suffix methods.
The :keep
option can be used to specify a maximum number of rolled log files to keep. Older files will be deleted based on the time they were created. The default is to keep all files.
Direct Known Subclasses
Constant Summary
Constants inherited from Writer
Writer::DEFAULT_ADDITIONAL_LINES_TEMPLATE, Writer::DEFAULT_FIRST_LINE_TEMPLATE
Instance Attribute Summary collapse
-
#keep ⇒ Object
Returns the value of attribute keep.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Attributes inherited from Writer
Instance Method Summary collapse
-
#archive_file_suffix ⇒ Object
Returns a suffix that will be appended to the file name when it is archived..
-
#initialize(path, options = {}) ⇒ RollingLogFile
constructor
A new instance of RollingLogFile.
-
#roll_file! ⇒ Object
Roll the log file by renaming it to the archive file name and then re-opening a stream to the log file path.
-
#roll_file? ⇒ Boolean
Return
true
if the file should be rolled.
Methods inherited from Writer
Methods inherited from Lumberjack::Device
#cleanup_files!, #close, #do_once, #flush, #write
Constructor Details
#initialize(path, options = {}) ⇒ RollingLogFile
Returns a new instance of RollingLogFile.
13 14 15 16 17 18 19 |
# File 'lib/lumberjack/device/rolling_log_file.rb', line 13 def initialize(path, = {}) @path = File.(path) @keep = [:keep] super(path, ) @file_inode = stream.lstat.ino rescue nil @@rolls = [] end |
Instance Attribute Details
#keep ⇒ Object
Returns the value of attribute keep.
11 12 13 |
# File 'lib/lumberjack/device/rolling_log_file.rb', line 11 def keep @keep end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
10 11 12 |
# File 'lib/lumberjack/device/rolling_log_file.rb', line 10 def path @path end |
Instance Method Details
#archive_file_suffix ⇒ Object
Returns a suffix that will be appended to the file name when it is archived.. The suffix should change after it is time to roll the file. The log file will be renamed when it is rolled.
23 24 25 |
# File 'lib/lumberjack/device/rolling_log_file.rb', line 23 def archive_file_suffix raise NotImplementedError end |
#roll_file! ⇒ Object
Roll the log file by renaming it to the archive file name and then re-opening a stream to the log file path. Rolling a file is safe in multi-threaded or multi-process environments.
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/lumberjack/device/rolling_log_file.rb', line 34 def roll_file! #:nodoc: do_once(stream) do archive_file = "#{path}.#{archive_file_suffix}" stream.flush current_inode = File.stat(path).ino rescue nil if @file_inode && current_inode == @file_inode && !File.exist?(archive_file) && File.exist?(path) begin File.rename(path, archive_file) after_roll cleanup_files! rescue SystemCallError # Ignore rename errors since it indicates the file was already rolled end end reopen_file end rescue => e STDERR.write("Failed to roll file #{path}: #{e.inspect}\n#{e.backtrace.join("\n")}\n") end |
#roll_file? ⇒ Boolean
Return true
if the file should be rolled.
28 29 30 |
# File 'lib/lumberjack/device/rolling_log_file.rb', line 28 def roll_file? raise NotImplementedError end |