Class: LogTwuncator::Truncator
- Inherits:
-
Object
- Object
- LogTwuncator::Truncator
- Defined in:
- lib/log_twuncator/truncator.rb
Constant Summary collapse
- TIMESTAMP_FORMAT =
"%Y%m%d%H%M%S"
- ATTRIBUTES =
[:name, :age, :size, :source_dir, :filter, :archive_dir, :archive_name, :logger]
- @@defaults =
{ :age => 7, :size => 1_000, :archive_name => "[basename]_[timestamp].[ext]" }
Instance Method Summary collapse
-
#initialize(options) ⇒ Truncator
constructor
A new instance of Truncator.
-
#truncate ⇒ Object
Find files using source directory and filter and then loop of files checking for files large than size in kilobytes or older than age in days.
Constructor Details
#initialize(options) ⇒ Truncator
Returns a new instance of Truncator.
21 22 23 24 25 26 27 28 29 |
# File 'lib/log_twuncator/truncator.rb', line 21 def initialize() @options = @@defaults.merge() ATTRIBUTES.each {|sym| instance_variable_set("@#{sym}".to_sym, @options[sym]) } if errors = self.class.(@options) raise InvalidTruncatorOptions, errors.join("\n") end end |
Instance Method Details
#truncate ⇒ Object
Find files using source directory and filter and then loop of files checking for files large than size in kilobytes or older than age in days.
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/log_twuncator/truncator.rb', line 33 def truncate path_filter = File.join(@source_dir, @filter) truncated = 0 time = Time.now - age_in_seconds Dir[path_filter].each do |file| next unless (@size > 0 && File.stat(file).size > size_in_bytes) || (@age > 0 && File.stat(file).ctime < time) archive_time = Time.now archive file, archive_time File.truncate(file, 0) set_truncated_time(file, archive_time) truncated += 1 log "Truncated #{file}" end truncated rescue => e log "Unknown error occurred: #{e}" end |