- 论坛徽章:
- 0
|
我写了一个python脚本来做事
def CheckLogs(path, interval=600, lock = None):
"""
path is a folder that have logs in it like tomcat\logs
interval of checking logs
lock is a threading.lock类型,用于多线程
"""
fileMap = {}
tr = re.compile(r'([eE]xception)')
trFile = re.compile(r'(-0.log|catelina.err|install_log.out)$')
while 1:
if lock:
lock.acquire()
#print 'acquire lock by log'
dirList=os.listdir(path)
for fname in dirList:
if len(trFile.findall(fname)) == 0:
continue
tf = open(path + fname, 'r')
if fname in fileMap:
tf.seek(fileMap[fname])
ts = tf.read()
tl = tr.findall(ts)
if len(tl):
print '[LOG]find ' + str(len(tl)) + ' exception(s) in: ' + fname
fileMap[fname] = tf.tell()
else:
ts = tf.read()
tl = re.findall(tr, ts)
if len(tl):
print '[LOG]find ' + str(len(tl)) + ' exception(s) in: ' + fname
fileMap[fname] = tf.tell()
tf.close()
if lock:
lock.release()
#print 'release lock by log'
time.sleep(interval) |
|