Class: Safis::Logging::FileRotator
- Defined in:
- lib/safis/logging/file_rotator.rb
Overview
An IO-like object that writes its input out to disk, rotating files as they fill up or expire.
Instance Attribute Summary collapse
-
#base_path ⇒ Object
readonly
The base path & file name of this
LogRotator
‘s resultant files. -
#granularity ⇒ Object
readonly
How often files should be automatically rotated, regardless of their size.
-
#max_files ⇒ Object
readonly
The maximum number of files kept around (old files are deleted).
-
#max_size ⇒ Object
readonly
The maximum size of a file before it is rotated.
Instance Method Summary collapse
-
#initialize(base_path, granularity = :hourly, max_size = 16777216, max_files = 100) ⇒ FileRotator
constructor
Creates a new
FileRotator
. -
#write(output) ⇒ Object
Writes output to the current file.
Constructor Details
#initialize(base_path, granularity = :hourly, max_size = 16777216, max_files = 100) ⇒ FileRotator
Creates a new FileRotator
.
Depending on the values you choose, the file naming scheme differs slightly. In all the following examples, we use “doom_laser.log” as our base_path
.
62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/safis/logging/file_rotator.rb', line 62 def initialize(base_path, granularity = :hourly, max_size = 16777216, max_files = 100) @base_path = base_path @file_root = File.dirname(base_path) @file_ext = File.extname(base_path) @file_base = File.basename(base_path, @file_ext) @granularity = granularity @max_size = max_size @max_files = max_files @file_lock = Mutex.new @file = create_file end |
Instance Attribute Details
#base_path ⇒ Object (readonly)
The base path & file name of this LogRotator
‘s resultant files.
22 23 24 |
# File 'lib/safis/logging/file_rotator.rb', line 22 def base_path @base_path end |
#granularity ⇒ Object (readonly)
How often files should be automatically rotated, regardless of their size.
25 26 27 |
# File 'lib/safis/logging/file_rotator.rb', line 25 def granularity @granularity end |
#max_files ⇒ Object (readonly)
The maximum number of files kept around (old files are deleted)
31 32 33 |
# File 'lib/safis/logging/file_rotator.rb', line 31 def max_files @max_files end |
#max_size ⇒ Object (readonly)
The maximum size of a file before it is rotated
28 29 30 |
# File 'lib/safis/logging/file_rotator.rb', line 28 def max_size @max_size end |
Instance Method Details
#write(output) ⇒ Object
Writes output to the current file
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/safis/logging/file_rotator.rb', line 77 def write(output) @file_lock.synchronize do # Do we need to rotate due to file size? if ( @file.stat.size + output.length > @max_size ) then @file = create_file # Or do we need to rotate due to a time switchover? elsif ( @next_rotation and Time.now >= @next_rotation ) then @file = create_file end # aaand log it @file.write(output) end end |