# Python log entry search utility

I wrote a small utility for searching our web app log files for certain strings. The idea is that if the search string is found somewhere in the entry, the entire entry should be printed.

The constraints are:

• One log entry can have one to many lines (stacktraces for example)
• Each log entry starts with an uppercase string specifying log type, followed by a pipe character ("ERROR|...")
• Each log entry ends with a pipe character followed by an exclamation mark ("|!")
• The utility should work much like a regular grep command, meaning that it takes input either from a list of files or from STDIN.

The script works as expected, but it is quite slow. Writing the same logic in an AWK script is about twice as fast when processing a large amount of data. Any suggestions on how to improve the performance?

logentrygrep.py

#!/bin/python2.7

from __future__ import print_function
import argparse
import fileinput
import re
import signal
import sys

# Disable printing stacktrace on KeyboardInterrupt
signal.signal(signal.SIGINT, lambda x,y: sys.exit(1))

# Parse arguments
parser = argparse.ArgumentParser(description='Tool for greping log entry contents')
parser.add_argument('-i', '--ignore-case', dest="ignorecase", action='store_true', help='Case insensitive greping')
parser.add_argument('-v', '--verbose', dest="verbose", action='store_true', help='Print verbose messages')
args = parser.parse_args()

VERBOSE = args.verbose

if VERBOSE:
print("Pattern to search for: " + args.pattern)
print("File(s): " + (str(args.file) if args.file else "STDIN"))

class LogDataProcessor: