Class: Lumberjack::Device::DateRollingLogFile
- Inherits:
-
RollingLogFile
- Object
- Lumberjack::Device
- Writer
- LogFile
- RollingLogFile
- Lumberjack::Device::DateRollingLogFile
- Defined in:
- lib/lumberjack/device/date_rolling_log_file.rb
Overview
This log device will append entries to a file and roll the file periodically by date. Files are rolled at midnight and can be rolled daily, weekly, or monthly. Archive file names will have the date appended to them in the format “.YYYY-MM-DD” for daily, “.week-of-YYYY-MM-DD” for weekly and “.YYYY-MM” for monthly. It is not guaranteed that log messages will break exactly on the roll period as buffered entries will always be written to the same file.
Constant Summary
Constants inherited from LogFile
Constants inherited from Writer
Writer::DEFAULT_ADDITIONAL_LINES_TEMPLATE, Writer::DEFAULT_FIRST_LINE_TEMPLATE
Instance Attribute Summary
Attributes inherited from RollingLogFile
Attributes inherited from LogFile
Attributes inherited from Writer
Instance Method Summary collapse
-
#archive_file_suffix ⇒ String
The date based suffix for file.
-
#initialize(path, options = {}) ⇒ DateRollingLogFile
constructor
Create a new logging device to the specified file.
-
#roll_file? ⇒ Boolean
Check if the file should be rolled.
Methods inherited from RollingLogFile
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 = {}) ⇒ DateRollingLogFile
Create a new logging device to the specified file. The period to roll the file is specified with the :roll option which may contain a value of :daily, :weekly, or :monthly.
19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/lumberjack/device/date_rolling_log_file.rb', line 19 def initialize(path, = {}) @manual = [:manual] @file_date = Date.today if [:roll]&.to_s&.match(/(daily)|(weekly)|(monthly)/i) @roll_period = $~[0].downcase.to_sym .delete(:roll) else raise ArgumentError.new("illegal value for :roll (#{[:roll].inspect})") end super end |
Instance Method Details
#archive_file_suffix ⇒ String
The date based suffix for file.
34 35 36 37 38 39 40 41 42 43 |
# File 'lib/lumberjack/device/date_rolling_log_file.rb', line 34 def archive_file_suffix case @roll_period when :weekly @file_date.strftime("week-of-%Y-%m-%d").to_s when :monthly @file_date.strftime("%Y-%m").to_s else @file_date.strftime("%Y-%m-%d").to_s end end |
#roll_file? ⇒ Boolean
Check if the file should be rolled.
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/lumberjack/device/date_rolling_log_file.rb', line 48 def roll_file? if @manual true else date = Date.today if date.year > @file_date.year true elsif @roll_period == :daily && date.yday > @file_date.yday true elsif @roll_period == :weekly && date.cweek != @file_date.cweek true elsif @roll_period == :monthly && date.month > @file_date.month true else false end end end |