Class: Yologga::LogsLifetime

Inherits:
Object
  • Object
show all
Defined in:
lib/yologga/logs_lifetime.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename) ⇒ LogsLifetime

Returns a new instance of LogsLifetime.



8
9
10
# File 'lib/yologga/logs_lifetime.rb', line 8

def initialize(filename)
  self.pathname = Pathname.new(filename)
end

Instance Attribute Details

#pathnameObject

Returns the value of attribute pathname.



6
7
8
# File 'lib/yologga/logs_lifetime.rb', line 6

def pathname
  @pathname
end

Instance Method Details

#callObject



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/yologga/logs_lifetime.rb', line 12

def call
  return unless Yologga.logs_lifetime
  entries = Dir.glob(pathname.dirname.join("#{pathname.basename}*")).map { |f| Pathname.new(f) }
  entries.each do |entry|
    next unless entry.file?
    old_enough = entry.mtime < (Time.now - Yologga.logs_lifetime)

    if entry.basename.to_s =~ /\.log\.\d+(?:\.gz)?\z/ && old_enough
      entry.delete
    end
  end
end