Class: Iyyov::LogRotator

Inherits:
Object
  • Object
show all
Defined in:
lib/iyyov/log_rotator.rb

Overview

Support check for size and rotation of a log file.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(log = nil) {|_self| ... } ⇒ LogRotator

Returns a new instance of LogRotator.

Yields:

  • (_self)

Yield Parameters:



69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/iyyov/log_rotator.rb', line 69

def initialize( log = nil )
  @log = log
  self.max_size_mb = 256
  @count = 3
  @gzip = true
  @signal = "HUP"
  @check_period = 5 * 60.0
  @pid = nil

  yield self if block_given?

  nil
end

Instance Attribute Details

#check_periodObject

Period between subsequent checks for rotation (default: 300.0)

Float seconds



54
55
56
# File 'lib/iyyov/log_rotator.rb', line 54

def check_period
  @check_period
end

#countObject

Number of rotated logs in addition to active log.

Fixnum (default: 3)



38
39
40
# File 'lib/iyyov/log_rotator.rb', line 38

def count
  @count
end

#gzipObject

GZIP compress rotated logs?

Boolean (default: true)



43
44
45
# File 'lib/iyyov/log_rotator.rb', line 43

def gzip
  @gzip
end

#logObject

Full path to log file to check and rotate

String (required)



28
29
30
# File 'lib/iyyov/log_rotator.rb', line 28

def log
  @log
end

#max_sizeObject

Maximum log size triggering rotation

Fixnum bytes (default: 256M bytes)



33
34
35
# File 'lib/iyyov/log_rotator.rb', line 33

def max_size
  @max_size
end

#pid=(value) ⇒ Object (writeonly)

Process ID to signal (if known in advance/constant, i.e. 0 for this process)

Fixnum



60
61
62
# File 'lib/iyyov/log_rotator.rb', line 60

def pid=(value)
  @pid = value
end

#signalObject

The signal to use post rotation (but before gzip) requesting that the daemon reopen its logs.

String (default: “HUP”)



49
50
51
# File 'lib/iyyov/log_rotator.rb', line 49

def signal
  @signal
end

Instance Method Details

#check_rotate(pid = @pid) ⇒ Object

Check if log is over size and rotate if needed. Yield log name to block just before rotating



85
86
87
88
89
90
91
92
93
94
95
# File 'lib/iyyov/log_rotator.rb', line 85

def check_rotate( pid = @pid )
  if File.exist?( log ) && File.size( log ) > max_size
    yield log if block_given?
    opts  = { :count => count, :gzip => gzip }
    if signal && pid
      opts[ :post_rotate ] = lambda { Process.kill( signal, pid ) }
    end
    LogRotate.rotate_file( log, opts )
  end
  nil
end

#max_size_mb=(mb) ⇒ Object

Set max_size in megabytes

mb<Fixnum>

megabytes



65
66
67
# File 'lib/iyyov/log_rotator.rb', line 65

def max_size_mb=( mb )
  @max_size = mb * 1024 * 1024
end