大家都知道tomcat主要有几类日志,如catalina开头的,localhost开头的,localhost_accesslog开头的、error开头的等等,我下面的代码主要针对这几种,代码主要用到python正则表达式去匹配文件名,筛选出日期,然后和系统6个月前的日期进行比较,从而得出应该要删除的日志文件名字,下面先贴出我的代码
#!/usr/bin/python
import datetime
import os
import re
#localtime = time.strftime("%Y-%m-%d", time.localtime())
logsfilename = ""
nowtime = datetime.datetime.now()
if nowtime.month <= 6:
nowtime = datetime.datetime((nowtime.year-1), (nowtime.month+12), nowtime.day)
lastsixmontime = datetime.datetime(nowtime.year, (nowtime.month-6), nowtime.day)
lastsixmondate = lastsixmontime.strftime("%Y-%m-%d")
#catalinadatelog = "catalina." + lastsixmondate + ".log"
#localhost_accessdate_log = "localhost_access_log." + lastsixmondate + ".txt"
#errordatelog = "error.log" + lastsixmondate + ".log"
#hostmanagedatelog = "host-manager." + lastsixmondate + ".log"
#localhost_log = "localhost." + lastsixmondate + ".log"
#manage_log = "manager." + lastsixmondate + ".log"
filename = os.popen('ls -l /rs/tomcat/logs/').readlines()
for i in filename:
cata_searchObj = re.search(r'catalina.(.*).log', i, re.M)
if cata_searchObj:
if cata_searchObj.group(1) < lastsixmondate:
currentfilename = "catalina." + cata_searchObj.group(1) + ".log" + " "
logsfilename = currentfilename + logsfilename
localhostlog_searchObj = re.search(r'localhost.(.*).log' , i , re.M)
if localhostlog_searchObj:
if localhostlog_searchObj.group(1) < lastsixmondate:
currentfilename = "localhost." + localhostlog_searchObj.group(1) + ".log" + " "
logsfilename = currentfilename + logsfilename
localhost_access_searchObj = re.search(r'localhost_access_log.(.*).txt', i, re.M)
if localhost_access_searchObj:
if localhost_access_searchObj.group(1) < lastsixmondate:
currentfilename = "localhost_access_log." + localhost_access_searchObj.group(1) + ".txt" + " "
logsfilename = currentfilename + logsfilename
error_searchObj = re.search(r'error.log(.*).log', i, re.M)
if error_searchObj:
if error_searchObj.group(1) <lastsixmondate:
currentfilename = "error.log" + error_searchObj.group(1) + ".log" + " "
logsfilename = currentfilename + logsfilename
hostmanage_searchObj = re.search(r'host-manager.(.*).log', i , re.M)
if hostmanage_searchObj:
if hostmanage_searchObj.group(1) < lastsixmondate:
currentfilename = "host-manager." + hostmanage_searchObj.group(1) + ".log" + " "
logsfilename = currentfilename + logsfilename
manage_searchObj = re.search(r'manager.(.*).log', i, re.M)
if manage_searchObj:
if manage_searchObj.group(1) < lastsixmondate:
currentfilename = "manager." + manage_searchObj.group(1) + ".log" + " "
logsfilename = currentfilename + logsfilename
os.chdir('/rs/tomcat/logs')
if logsfilename:
os.system('rm -rf %s' %(logsfilename))
#print logsfilename
给大家解释一下上面的程序:
首先logsfilename是定义6个月前的文件名,就是要清除的文件名,nowtime是现在系统的日期,lastsixmontime是6个月前的日期,就是nowtime减去6个月,最后lastsixmondate是以 年-月-日 的格式输出日期,如2017-08-30。注意,我在算出当前时间nowtime之后加了一个条件,就是如果月份小于或者等于6的时候要把当前年份减去1,然后月份加12来当作当前时间,例如2017-06-07,如果要清除6个月前的日志,那么要把当前时间变成2016-18-07,然后后面再在月份减去6才能正确得出6个月前的日期。下面注释的不用看,知识我写程序用来调试的,filename存储tomcat的所有日志文件名,用os模块调用linux的ls命令读取tomcat日志文件名,紧接着下面有一个for循环,for i in filename的意思是逐个读取日志文件名,接着调用re模块使用search正则表达式去匹配这个文件名含有的日期,例如localhost_access_log.2017-8-30.txt这个日志文件,我们通过localhost_access_searchObj=re.search(r'localhost_access_log.(.*).txt',i,re.M)这个代码来匹配中间的日期,其中(.*)的意思是匹配前面的任意字符,也就是匹配localhost_access. 和 .txt中间省略的内容,注意这里是有两个“.”的,相当于截取中间的日期,其他日志文件也同理,然后判断匹配的内容是否为空,不为空就使用localhost_access_searchObj.group(1)截取匹配的第一组当然这里只有唯一一组内容,也就是这个日志文件对应的日期,re.search的用法不懂的推荐大家去看下菜鸟教程的Python基础教程的正则表达式篇。
在每截取一个日志文件的日期,我们都让这个日期跟lastsixmondate比较,若小于6个月前的日期,我们就该截取的日期再在前面和后面加上对应的字符串组成一个完整的日志文件名,注意在加字符串组成日志文件名的时候要在最后面加上空字符" ",不然的话你后面累加的其他符合条件的日志名就会和这个日志名拼接在一起,进而会影响后面的rm命令删除日志的操作,然后放到currentfilename变量里面,然后户通过循环累加到logsfilename里面,最终我们就可以得到 所有6个月前的日志文件名,然后保存在logsfilename里面,你可以使用print logsfilename查看下里面的内容,里面应该整齐地排列着符合条件的日志文件名,都是对应一个日志文件名加上空格隔开,然后接着是另外一个日志文件名……如此类推。
最后,我们采用os.chdir方法切换当前目录到存放tomcat日志文件名的目录,注意这里tomcat目录是我的服务器上面的,具体的要根据大家的tomcat的日志文件放在哪里而定,这里可能会有个疑问,在保存该python文件后,使用”python 文件名.py” 去执行这个文件,我们发现看不出来切换到tomcat日志文件目录下面,其实这个时候已经切换进去了,我们可以在os.chdir()这行代码后面紧接着加一条dir=os.system('pwd'),print dir,输出当前的工作目录,再保存退出执行该文件就可以看到目录已经切换到/rs/tomcat/logs下面,最后就是使用os.system调用linux的rm命令删除Logsfilename里面的6个月前的日志文件名了
好了,以上代码介绍完毕,有什么不懂的在下面评论留言把