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.