我是python的新手程序员。我目前正在构建一个解析日志文件并完成该类的所有元素的类。尽管如此,由于 python 中的大多数东西都对我不利,我要么错误地格式化了我的类,要么搞砸了我的语义。我想知道是否存在用于构建类的明确格式,以及我所写的格式是否遵循所述格式。
以下是日志中的一些行:
2012-06-12 14:02:21,813 [main] INFO ConnectionManager.java (line 238) Initializing the ConnectionManager.
2012-06-12 14:02:21,844 [main] INFO CimListener.java (line 142) Starting listener at http://127.0.0.1:7012
2012-06-12 14:02:21,974 [main] INFO CimListener.java (line 158) Listening at http://127.0.0.1:7012
2012-06-12 14:02:23,209 [main] INFO RmiServiceExporter.java (line 393) Looking for RMI registry at port '10099'
2012-06-12 14:02:23,232 [main] INFO RmiServiceExporter.java (line 404) Could not detect RMI registry - creating new one
这是课程:
import re
import time
import calendar
from datetime import datetime
f = open("C:\Users\-----\Desktop\Real Logs\controllersvc.log", "r")
while True:
line = f.readline()
if not line:
break
class LogLine:
SEVERITIES = ['EMERG','ALERT','CRIT','ERR','WARNING','NOTICE','INFO','DEBUG']
severity = 1
def __init__(self, line):
try:
timestr, msstr, sevstr, self.filename, linestr, self.message = re.match(r"^(\d\d\d\d-\d\d-\d\d[ \t]\d\d:\d\d:\d\d),(\d\d\d),(?i[a-z]+),([A-Za-z]{1,.}),([(]\[lL]ine\>\s+\d+[)]),^(?<=\)\s?\w+$)", line).groups()
self.line = int(linestr)
self.sev = self.SEVERITIES.index(sevstr)
self.time = float(calendar.timegm(time.strptime(timestr, "%Y-%m-%d %H:%M:%S,%f"))) + float(msstr)/1000.0
dt = datetime.strptime(t, "%Y-%m-%d %H:%M:%S,%f")
except Exception:
print 'error',self.filename
def get_time(self):
return self.time
def get_severity(self):
return self.sev
def get_message(self):
return message
def get_filename(self):
return filename
def get_line(self):
return line