TailFromSentinel
Parses a file, looking for a sentinel line that satisfies a block, and returns
all lines from there until the end of the file (including the sentinel itself).
This can be handy when you are looking for information in a log file only after
a certain condition (eg. a date) has been met.
You can either use it on it's own, or subclass it to build your own custom
tail-like classes.
Standalone:
File.open('/var/log/messages', 'r') do |file|
tfs=TailFromSentinel::Base.new(file) { |line| line =~ /Dec 20 11:2\d+/ }
end
tfs.data # => [ 'Dec 20 11:20:00 ...', 'Dec 20 11:21:00 ...', ... ]
Subclassed:
class TailMyFile < TailFromSentinel::Base
DATA_ROOT="/path/to/data/"
def initialize(data_file)
file=File.open(File.join(DATA_ROOT, data_file), 'r') do |file|
super(file, &select_sentinel_criteria(data_file))
end
end
def select_sentinel_criteria(data_file)
case data_file
when ''production.log'
Proc.new { |line| line =~ /pattern/ }
when 'development.log'
Proc.new { |line| line =~ /another pattern/ }
else
raise "Unhandled file: #{data_file}"
end
end
end
t=TailMyFile.new('production.log')
t.data # => [ 'Started GET "/pattern ...", " Processing ... ", ... ]
Project
tail_from_sentinel
Reads a file, but only return content once a marker (sentinel) has been found.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
Development
Dependencies
Project Readme