-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathzip_log.py
More file actions
73 lines (57 loc) · 2.16 KB
/
zip_log.py
File metadata and controls
73 lines (57 loc) · 2.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#!/usr/bin/env python
# *-* coding:UTF-8 *-*
"""
# Author: yuyet
# Date: 2018/4/12
# Description:
"""
# -------------------
import os
import zipfile
import fnmatch
import datetime
# -------------------
class Ziplogfile(object):
"""
__getlogfile
"""
yesterday = (datetime.date.today() - datetime.timedelta(days=1)).strftime("%Y%m%d")
def __init__(self, logpath, ziplog):
self.__logpath = logpath
self.__loglist = []
self.__ziplog = ziplog
# 获取 Logpath 目录下的所有符合条件的日志名称
def __getlogfile(self):
for logname in os.listdir(self.__logpath):
if logname.startswith("info-") and fnmatch.fnmatch(logname, "info-%s.log.[0-9]*" % self.yesterday):
print logname
self.__loglist.append(logname)
elif logname.startswith("spring.log.") and fnmatch.fnmatch(logname, "spring.log.[0-9]*"):
print logname
self.__loglist.append(logname)
elif logname.startswith("error-") and fnmatch.fnmatch(logname, "error-%s.log.[0-9]*" % self.yesterday):
print logname
self.__loglist.append(logname)
elif logname.startswith("consumer-error-") and fnmatch.fnmatch(logname, "consumer-error-%s.log" % self.yesterday):
print logname
self.__loglist.append(logname)
elif logname.startswith("consumer-info-") and fnmatch.fnmatch(logname, "consumer-info-%s.log" % self.yesterday):
print logname
self.__loglist.append(logname)
# 删除日志
def __deletelogfile(self):
for logname in self.__loglist:
os.remove(os.path.join(self.__logpath, logname))
# 压缩日志文件
def __ziplogfile(self):
with zipfile.ZipFile(self.__ziplog, "w") as myzip:
for logname in self.__loglist:
myzip.write(os.path.join(self.__logpath, logname))
#
def startzip(self):
self.__getlogfile()
self.__ziplogfile()
self.__deletelogfile()
if __name__ == '__main__':
zf = Ziplogfile(r"d:\evbase", r"d:\evwork\ziplog.zip" )
zf.startzip()