I've written a little command-line utility for filtering log files. It's like grep, except instead of operating on lines it operates on log4j-style messages, which may span multiple lines, with the first line always including the logging level (TRACE, DEBUG etc.).
Example usage on a file short.log with contents like this:
16:16:12 DEBUG - Something happened, here's a couple lines of info: debug line another debug line 16:16:14 - I'm being very verbose 'cause you've put me on TRACE trace info 16:16:15 TRACE - single line trace 16:16:16 DEBUG - single line debug
logrep -f short.log DEBUG
produces:
16:16:12 DEBUG - Something happened, here's a couple lines of info: debug line another debug line 16:16:16 DEBUG - single line debug
I think the main loop of the program could probably be simplified with some sort of parse and filter.
file = fileinput.input(options.file)
try:
line = file.next()
while True:
if any(s in line for s in loglevels):
if filter in line:
sys.stdout.write(line)
line = file.next()
while not any(s in line for s in loglevels):
sys.stdout.write(line)
line = file.next()
continue
line = file.next()
except StopIteration:
return