touchmm 发表于 2013-2-7 09:52:14

小试python

应用前提: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:21import osimport stringimport 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, fileinput.filelineno(), type, string.replace(line, ",", "|")))      fileinput.close()output.closeprint "done, python is great!"
页: [1]
查看完整版本: 小试python