小试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: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!"

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值