tail 实现 +注释
2016-03-15 21:07
489 查看
自己平时工作经常需要tail日志实时入库,近期学python,发现没有系统模块是实现tail功能的。 偶然在在github 上看到一份代码,有人自己写了 python-tail模块实现tail功能。 介绍如下: 先跳到文件末尾,然后不断的打印出新行,直到文件结束,然后再更新指针位置,最后再死循环重复上述过程 。 模块代码: #!/usr/bin/env python ''' Python-Tail - Unix tail follow implementation in Python. python-tail can be used to monitor changes to a file. Example: import tail # Create a tail instance t = tail.Tail('file-to-be-followed') # Register a callback function to be called when a new line is found in the followed file. # If no callback function is registerd, new lines would be printed to standard out. t.register_callback(callback_function) # Follow the file with 5 seconds as sleep time between iterations. # If sleep time is not provided 1 second is used as the default time. t.follow(s=5) ''' # Author - Kasun Herath <kasunh01 at gmail.com> # Source - https://github.com/kasun/python-tail import os import sys import time class Tail(object): ''' Represents a tail command. ''' def __init__(self, tailed_file): ''' Initiate a Tail instance. Check for file validity, assigns callback function to standard out. Arguments: tailed_file - File to be followed. ''' self.check_file_validity(tailed_file) self.tailed_file = tailed_file self.callback = sys.stdout.write def follow(self, s=1): ''' Do a tail follow. If a callback function is registered it is called with every new line. Else printed to standard out. Arguments: s - Number of seconds to wait between each iteration; Defaults to 1. ''' with open(self.tailed_file) as file_: # Go to the end of file file_.seek(0,2) while True: curr_position = file_.tell() line = file_.readline() if not line: file_.seek(curr_position) else: self.callback(line) time.sleep(s) def register_callback(self, func): ''' Overrides default callback function to provided function. ''' self.callback = func def check_file_validity(self, file_): ''' Check whether the a given file exists, readable and is a file ''' if not os.access(file_, os.F_OK): raise TailError("File '%s' does not exist" % (file_)) if not os.access(file_, os.R_OK): raise TailError("File '%s' not readable" % (file_)) if os.path.isdir(file_): raise TailError("File '%s' is a directory" % (file_)) class TailError(Exception): def __init__(self, msg): self.message = msg def __str__(self): return self.message 使用模块案例: #!/usr/bin/env python ''' python-tail example. Does a tail follow against /var/log/syslog with a time interval of 5 seconds. Prints recieved new lines to standard out ''' import tail def print_line(txt): ''' Prints received text ''' print(txt) t = tail.Tail('/var/log/syslog') t.register_callback(print_line) t.follow(s=5)
相关文章推荐
- HDU1039 Easier Done Than Said?
- RAID简介与软RAID实现
- Codeforces 300D Painting Square (FFT)
- Codeforces 472G Design Tutorial: Increase the Constraints(分块+FFT)
- HDU.1023 Train Problem II【大数除法、卡特兰数】--用于求出栈的n种方式(3.15)
- 属性readwrite,readonly,assign,retain,copy,nonatomic 各是什么作用,在那种情况下用
- Error:Execution failed for task ':processDebugResources'.
- HDFS API文件上传报错 Exception in thread "main" .......: Wrong FS: file:///
- SGI STL (7) :: why stl containers have their own iterators?
- sakai作业系统
- HDU 2473 Junk-Mail Filter(并查集删点)
- http://blog.csdn.net/pizi0475/article/details/7768597
- 220. Contains Duplicate III
- PowerDesigner打开设计文件后提示failed to read the fileXXX的解决办法
- Batch Normalization: Accelerating Deep Network Training by Reducing Internal Covariate Shift
- 使用Genymotion调试出现错误INSTALL_FAILED_CPU_ABI_INCOMPATI
- B - Pairs Forming LCM——(LightOJ 1236)
- mybaits中的<trim>标签的作用
- sakai创建工具的过程,以roster2为例
- IBM openblockchain学习(一)--obc-peer环境搭建