class LogTail
Facilitates reading the most recent additions to a log file.
Example:
tail = ApacheLogTail.new "/var/log/apache2/access.log" tail.state_store.path_to_file = "/tmp/my-state.yml" # Optional: there is a default path tail.each_new_line {|line| puts line }
## A custom StateStore
tail.state_store = MyStateStore.new
A StateStore object must provide these methods:
- remember( state:Hash) - recall(): Hash
Attributes
state_store[W]
Public Class Methods
new(path_to_file)
click to toggle source
@param [String] path_to_file the path to the file to process
# File lib/apache_log_tail.rb, line 22 def initialize path_to_file @path_to_file = path_to_file end
Public Instance Methods
each_new_line(path_to_file = @path_to_file) { |line| ... }
click to toggle source
Goes through each line in the file that has not yet been processed and passes it to the block given.
@param [String] path_to_file the path to the file that should be
processed. This parameter is only intended for internal use ( processing rotated log files) and should be omitted for normal use
# File lib/apache_log_tail.rb, line 41 def each_new_line path_to_file = @path_to_file # Recall the cursor ( the location in the log file where we left off # reading last time) state = state_store.recall state[:cursor] ||= 0 File.open path_to_file do |stream| # Move the file reading "head" to the place where we left off reading # last time stream.seek state[:cursor] stream.each_line {|line| yield line } # Remember where the log file reading cursor is for next time: state[:cursor] = stream.tell state_store.remember state end end
state_store()
click to toggle source
The StateStore provides persistent storage of a Hash.
# File lib/apache_log_tail.rb, line 28 def state_store @state_store ||= FileStateStore.new end