应用前提:N台Web机,每天产生大量的日志,先用python脚本从服务器取出,并按xxx_ip_yyyyMMdd_hhmmss.log格式收集,tar.gz后传到本机,用python将主要的Cause by Error等重要错误信息提取到csv文件,供专人跟踪日志。
#! /usr/bin/env python
# -*- coding: utf-8 -*-
#@author jinqinghua@gmail.com
#@version 2010-08-17 02:21
import os
import string
import fileinput
#日志的位置
dir_log = r"D:/python/logs"
#日志合并后的文件位置
file_csv = os.path.join(r"F:", "log.csv" )
if os.path.exists(file_csv):
os.remove(file_csv)
output = open(file_csv, 'w+')
output.write("ip,line number,error type, error cause/n")
for file in os.listdir(dir_log):
if not file.endswith(".log"):
print "WARN:%s is not a log file" %(file)
continue
print "INFO:process file %s" %(file)
for line in fileinput.input(os.path.join(dir_log, file)):
for type in ('Caused ', ): #'ERROR ', 'WARN '):
if line.find(type) != -1 :
output.write("%s,%s,%s,%s" %(file[4:16], fileinput.filelineno(), type, string.replace(line, ",", "|")))
fileinput.close()
output.close
print "done, python is great!"
原文链接: http://blog.csdn.net/kimsoft/article/details/5817828