, Johann Schmitz

Thanks to the incredible pyparsing module it is really easy to parse arbitrary files without the hassle of regular expressions.

The following code parses a standard syslog-ng logfile:

from pyparsing import Word, alphas, Suppress, Combine, nums, string, Optional, Regex

month = Word(string.uppercase, string.lowercase, exact=3)
integer = Word(nums)
serverDateTime = Combine(month + " " + integer + " " + integer + ":" + integer + ":" + integer)
hostname = Word(alphas + nums + "_" + "-")
daemon = Word(alphas + "/" + "-" + "_") + Optional(Suppress("[") + integer + Suppress("]")) + Suppress(":")
message = Regex(".*")
bnf = serverDateTime + hostname + daemon + message

with open('/path/to/logfile') as syslogFile:
    for line in syslogFile:
        fields = bnf.parseString(line)
        print fields