0

I am writing a program that essentially pulls a line out of a logfile, parses it, and returns the parsed data back in a simplified form. My main problem currently is the method in which I should parse my datetime stirngs. Here's an example of a line from the log.

Example of Logfile:

2012-06-12 14:02:16,341 [main] INFO ---
2012-06-12 14:02:16,509 [main] INFO ---
2012-06-12 14:02:17,000 [main] INFO ---
2012-06-12 14:02:17,112 [main] INFO ---
2012-06-12 14:02:20,338 [main] INFO ---
2012-06-12 14:02:21,813 [main] INFO ---

My code to parse SO FAR ( very rough ):

class LogLine:

    SEVERITIES = ['EMERG','ALERT','CRIT','ERR','WARNING','NOTICE','INFO','DEBUG']
    severity = 1

    def __init__(self, line):
        try:
            t, s, self.filename, n, self.message =
                re.match(r"^(\d\d\d\d-\d\d-\d\d[ \t]\d\d:\d\d:\d\d,\d\d\d)", line)
            self.line = int(n)
            self.sev = self.SEVERITIES.index(s)
            self.time = time.strptime(t)


    def get_t(self):
        return

    def get_severity(self):
        return self.SEVERITIES.index(self)
    def get_message(self):
        return
    def get_filename(self):
        return
    def get_line(self):
        return

So basically (if you weren't able to deduce from my terrible code) I am parsing the string using a regular expression to obtain the datetime. I also have been reading about strptime as a possible solution to this as well. ULTIMATELY, I need to parse the datetime into milliseconds and then add it to the milliseconds integer in the datetime (separated by comma)

I'm sure this question is extremely convoluted and I apologize in advance. Thank you for your help.

Filburt
  • 16,221
  • 12
  • 59
  • 107
Raj
  • 81
  • 1
  • 8

2 Answers2

0
>>> datetime.datetime.strptime('2012-06-12 14:02:16,341' + '000', '%Y-%m-%d %H:%M:%S,%f')
datetime.datetime(2012, 6, 12, 14, 2, 16, 341000)
Ignacio Vazquez-Abrams
  • 699,552
  • 132
  • 1,235
  • 1,283
  • This is perfect. My only question is how would I apply this to my current code in the sense that I have the program open the actual logfile as opposed to simply supplying a string? – Raj Jul 09 '12 at 21:31
  • Just split on the second space [per line](http://stackoverflow.com/questions/8009882/how-to-read-large-file-line-by-line-in-python). – Ignacio Vazquez-Abrams Jul 09 '12 at 21:33
  • I see. Does this replace my regex? Still a bit confused about that as well. – Raj Jul 09 '12 at 21:41
0

Here's an example of how to parse a line:

>>> # A line from the logfile.
>>> line = "2012-06-12 14:02:16,341 [main] INFO ---"
>>> # Parse the line.
>>> m = re.match(r"^(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}),(\d{3}) \[([^]]*)\] (\S+) (.*)", line)
>>> timestamp, line_number, filename, severity, message = m.groups()
>>> # Show the various captured values.
>>> timestamp
'2012-06-12 14:02:16'
>>> line_number
'341'
>>> filename
'main'
>>> severity
'INFO'
>>> message
'---'
MRAB
  • 18,864
  • 5
  • 36
  • 31