Class: Safis::Logging::FileRotator

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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.

Examples:

Hourly rotated log

doom_laser.2009-07-03-14.log
doom_laser.2009-07-03-15.log
doom_laser.2009-07-03-15.1.log

Daily rotated log

doom_laser.2009-07-03.log
doom_laser.2009-07-04.log
doom_laser.2009-07-04.1.log

No time based rotation

doom_laser.log
doom_laser.1.log
doom_laser.2.log

Parameters:

  • base_path (String)

    The base path and name of the file.

  • granularity (optional, :hourly, :daily, :none) (defaults to: :hourly)

    How often should the output be automatically rotated? Every hour? Every Day? Never?

  • max_size (optional, Number) (defaults to: 16777216)

    The maximum size of a file, in bytes, before it gets rotated. Defaults to 16MiB.

  • max_files (optional, Number) (defaults to: 100)

    The maximum number of files to keep around before deleting old ones. Defaults to 100 (so approx 1.5GiB maximum).



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_pathObject (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

#granularityObject (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_filesObject (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_sizeObject (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