我正在编写一个程序,它基本上从日志文件中拉出一行,解析它,然后以简化的形式返回解析后的数据。我目前的主要问题是我应该解析日期时间的方法。这是日志中一行的示例。
日志文件示例:
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 ---
我解析SO FAR的代码(非常粗糙):
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
所以基本上(如果你无法从我可怕的代码中推断出来)我正在使用正则表达式解析字符串以获取日期时间。我也一直在阅读strptime作为一种可能的解决方案。最后,我需要将datetime解析为毫秒,然后将其添加到datetime中的毫秒整数(以逗号分隔)
我确信这个问题非常复杂,我提前道歉。谢谢您的帮助。
答案 0 :(得分: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)
答案 1 :(得分:0)
以下是如何解析一行的示例:
>>> # 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
'---'