應用前提:N台Web機,每天產生大量的日誌,先用python指令碼從伺服器取出,並按xxx_ip_yyyyMMdd_hhmmss.log格式收集,tar.gz後傳到本機,用python將主要的Cause by Error等重要錯誤資訊提取到csv檔案,供專人追蹤記錄檔。
#! /usr/bin/env python<br /># -*- coding: utf-8 -*-<br />#@author jinqinghua@gmail.com<br />#@version 2010-08-17 02:21<br />import os<br />import string<br />import fileinput<br />#日誌的位置<br />dir_log = r"D:/python/logs"<br />#日誌合并後的檔案位置<br />file_csv = os.path.join(r"F:", "log.csv" )<br />if os.path.exists(file_csv):<br /> os.remove(file_csv)</p><p>output = open(file_csv, 'w+')<br />output.write("ip,line number,error type, error cause/n")<br />for file in os.listdir(dir_log):<br /> if not file.endswith(".log"):<br /> print "WARN:%s is not a log file" %(file)<br /> continue<br /> print "INFO:process file %s" %(file)<br /> for line in fileinput.input(os.path.join(dir_log, file)):<br /> for type in ('Caused ', ): #'ERROR ', 'WARN '):<br /> if line.find(type) != -1 :<br /> output.write("%s,%s,%s,%s" %(file[4:16], fileinput.filelineno(), type, string.replace(line, ",", "|")))<br /> fileinput.close()<br />output.close<br />print "done, python is great!"</p><p>