平时在查看日志的时候打开满屏的日志,看上去有点凌乱.于是写个Python脚本过滤出想要看的错误的日志.直接上脚本 脚本示例 def read_log(logname,keyword): tell = #文件指针 i=#行号 while True: f = open(logname) f.seek(tell)#移动文件指针 res=[]#存所有的结果 for line in f: if keyword in line: print(line) res.append('行号[%s] 内容:%s \n
a. 利用sys.stdout将print行导向到你定义的日志文件中,例如: import sys # make a copy of original stdout route stdout_backup = sys.stdout # define the log file that receives your log info log_file = open("message.log", "w") # redirect print output to log fi
通过NXlog将Windows事件日志保存为json格式文件,然后在Python中使用json.loads()进行处理. NXlog在将Windows事件日志保存为json格式文件,文件中带入了BOM编码格式,所以需要使用decode("utf-8-sig")先对源数据进行处理,否则json.loads()会提示 "No JSON object could be decoded" 错误 文件中每一条事件日志包含有中文.\r\n\t字符,所以在通过json.loads