Python 定时提取实时日志的程序
2015-05-26 14:09
239 查看
这是一个定时读取 实时日志文件的程序 。 目标文件是target_file. 它是应用程序实时写入的。
我要做的是,每个5秒钟,提取一次该日志文件中的内容,然后生成另一个文件。最后把这些文件都汇总。 请多多指教。
我要做的是,每个5秒钟,提取一次该日志文件中的内容,然后生成另一个文件。最后把这些文件都汇总。 请多多指教。
[code]#!/usr/local/bin/python # coding:utf-8 import fileinput import time import os target_file = 'user.log' init_flag = True # 初次加载程序 time_kick = 5 record_count = 0 while True: print '当前读到了', record_count #没有日志文件,等待 if not os.path.exists(target_file): print 'target_file not exist' time.sleep(time_kick) continue try: ip = '10.10.1.100' easytime = time.strftime('%Y%m%d_%H%M%S', time.localtime()) file_name = '%s_user_%s.log' % (ip,easytime) f_w = open(file_name, 'w') if init_flag: #读取整个文件 for eachline in fileinput.input(target_file): print eachline f_w.write(eachline) record_count += 1 init_flag = False else: #如果总行数小于当前行,那么认为文件更新了,从第一行开始读。 total_count = os.popen('wc -l %s' % target_file).read().split()[0] total_count = int(total_count) if total_count < record_count: record_count = 0 for eachline in fileinput.input(target_file): line_no = fileinput.filelineno() if line_no > record_count: print eachline f_w.write(eachline) record_count += 1 f_w.close() except: pass time.sleep(time_kick)
相关文章推荐
- python实现定时提取实时日志程序
- 本文以python实现了一个日志文件中ip提取与统计程序
- 学习练习程序:根据日期提取日志的python脚本。
- 用70行代码实现日志分析程序-python
- Python 实时日志平台 Sentry
- python写的一个squid访问日志分析的小程序
- python写的树莓派实时监控温度,cpu使用率的小程序
- Sentry - Python 实时日志平台 - 开源中国 OSChina.NET
- Linux中运行python实现错误日志定时报警
- python写的日志监控程序,关键字统计,日志大小监控,关键字出现报警并截取日志
- python 从远程服务器下载日志文件的程序
- 定时清理日志文件-python实现
- 可定时执行的Web日志自动压缩清理程序
- python程序(scrapy爬虫)在windows环境下开机定时自启动
- 微信实时提醒python程序状态
- python: 从远程服务器下载日志程序
- 海量日志,提取出现最多的IP--python实现
- python 实时遍历日志文件
- Python写WEB日志分析程序的一些思路
- python小程序:以后台服务方式 定时 监视redis队列 (anaconda3.5.2)