Class: ApacheLogTail

Inherits:
LogTail show all
Defined in:
lib/apache_log_tail.rb

Overview

Note that this class does no parse Apache log entries, only knows how Apache log files are rotated on Debian. I have enjoyed using the apachelogregex gem for parsing.

Instance Attribute Summary

Attributes inherited from LogTail

#state_store

Instance Method Summary collapse

Methods inherited from LogTail

#initialize

Constructor Details

This class inherits a constructor from LogTail

Instance Method Details

#each_new_lineObject

Note: This method must be invoked more frequently than the log file rotation period ( typically 1 week) otherwise an entire file will be missed.



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/apache_log_tail.rb', line 101

def each_new_line
  state = state_store.recall
  first_line_now = first_line_of @path_to_file
  # If the Apache log file has been rotated..
  # The file has been rotated if it has been read ( cursor is remembered) but
  # the first line is not as remembered
  file_has_been_rotated = lambda { state[:cursor] and first_line_now != state[:first_line] }
  if file_has_been_rotated[]
    # Check that the renamed file is as we expect before reading the rest of it:
    renamed_file = @path_to_file + ".1"
    if first_line_of( renamed_file) != state[:first_line]
      raise StandardError.new "Rotated file could not be found"
    end
    # Process the last lines of the rotated file:
    super renamed_file
    # Reset the cursor ready for the new file:
    state[:cursor] = 0
  end
  if first_line_now != state[:first_line]
    state[:first_line] = first_line_now
    state_store.remember state
  end
  # Process the lines that have since been added to the new file:
  super
end