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.
The :min_roll_check option can be used to specify the number of seconds between checking the file to determine if it needs to be rolled. The default is to check at most once per second.
Direct Known Subclasses
Constant Summary
Constants inherited from LogFile
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 LogFile
Methods inherited from Writer
#close, #datetime_format, #datetime_format=, #flush, #write
Methods inherited from Lumberjack::Device
#cleanup_files!, #close, #datetime_format, #datetime_format=, #do_once, #flush, #reopen, #write
Constructor Details
#initialize(path, options = {}) ⇒ RollingLogFile
Returns a new instance of RollingLogFile.
18 19 20 21 22 23 24 25 26 |
# File 'lib/lumberjack/device/rolling_log_file.rb', line 18 def initialize(path, = {}) @path = File.(path) @keep = [:keep] super(path, ) @file_inode = stream.lstat.ino rescue nil @@rolls = [] @next_stat_check = Time.now.to_f @min_roll_check = ([:min_roll_check] || 1.0).to_f end |
Instance Attribute Details
#keep ⇒ Object
Returns the value of attribute keep.
16 17 18 |
# File 'lib/lumberjack/device/rolling_log_file.rb', line 16 def keep @keep end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
15 16 17 |
# File 'lib/lumberjack/device/rolling_log_file.rb', line 15 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.
30 31 32 |
# File 'lib/lumberjack/device/rolling_log_file.rb', line 30 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.
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/lumberjack/device/rolling_log_file.rb', line 41 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.
35 36 37 |
# File 'lib/lumberjack/device/rolling_log_file.rb', line 35 def roll_file? raise NotImplementedError end |