-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcleanLog_kilimall.py
More file actions
161 lines (137 loc) · 4.44 KB
/
Copy pathcleanLog_kilimall.py
File metadata and controls
161 lines (137 loc) · 4.44 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# -*- coding: UTF-8 -*-
# by gzq
# date :2017/9/22 0022 10:28
# -*- coding: UTF-8 -*-
# by gzq
# date :2017/8/7
import subprocess
import time
import os
import sys
import datetime
import re
import optparse
import shutil
reload(sys)
sys.setdefaultencoding('utf-8')
"""
日志三天 或者 日志文件大于1G 清理
"""
s = 'rewrite logs'
#log_size_max = 1 * (1024 * 1024) # 1G 最大上限
log_size_max = 50 * (1024 * 1024) # 50M 最大上限
# log_size_max = 10*(1024*1024) #10G 最大上限
# 当前时间
now_date = str(datetime.datetime.now()).split(' ')[0]
remove_log_dir = '/tmp/rm_logs/' # 删除记录日志路径
rw_log = now_date + "_rw.log" #文件名
# 设置 多个删除文件路径 和单独的小时设置
SerivceNameLogDict = {
#"/var/log/py/": 2,
"/app/project/test/": 720,
}
def _init():
# 初始化,检查 应用路径是否正确,返回存在的应用日志路径。
dictTmp={}
if not os.path.exists(remove_log_dir):
os.mkdir(remove_log_dir)
for logPath, days in SerivceNameLogDict.iteritems():
if os.path.exists(logPath):
dictTmp[logPath] = days
else:
print "logPath:%s is not exits,please check path" % (logPath)
continue
return dictTmp
def listDirName(service_dir):
# 列出 应用目录下的所以文件,返回列表
fileList=[]
for p, d, f in os.walk(service_dir):
for i in f:
fileList.append(os.path.join(p, i))
return fileList
"""
判断文件是否为多少小时前文件 以修改时间为准
"""
def fileNameMtime(filename, hours):
mtime = time.ctime(os.path.getmtime(filename))
ctime = time.ctime(os.path.getctime(filename))
mtime_s = (os.path.getmtime(filename))
ctime_s = (os.path.getctime(filename))
# print "Last modified : %s, last created time: %s" % (mtime, ctime)
now = datetime.datetime.now()
#只进行时间小时比较
daydelta = datetime.timedelta(hours=hours)
print "daydelat",daydelta
days_ago = now - daydelta
print "daysage",days_ago
# time.strftime("%Y-%m-%d %H:%M:%S", localTime)
print "s",datetime.datetime.fromtimestamp(mtime_s)
if datetime.datetime.fromtimestamp(mtime_s) <= days_ago:
print "sss"
return True
else:
return False
"""
获取文件大小
"""
def countFile(file):
if os.path.exists(file):
fileSize = os.path.getsize(file)
return fileSize
def printHuamSize(file):
# 对文件大小 进行良好的显示
fileSize = countFile(file)
if 1 < fileSize < 1024:
#print '%sB %s ' % (fileSize, file)
return '%s B' % (fileSize)
elif 1024 < fileSize < 1024 * 1024:
#print '%.2fK %s' % (float(fileSize / 1024.0), file)
return '%.2f K' % (float(fileSize / 1024.0))
elif 1024 * 1024 < fileSize < 1024 * 1024 * 1024:
#print '%.2fM %s' % (float(fileSize / 1024 / 1024.0), file)
return '%.2f M' % (float(fileSize / 1024 / 1024.0))
elif 1024 * 1024 * 1024 < fileSize < 1024 * 1024 * 1024 * 1024:
#print '%.2fG %s' % (float(fileSize / 1024 / 1024 / 1024.0), file)
return '%.2f G' % (float(fileSize / 1024 / 1024 / 1024.0))
"""
删除 重新 日志操作记录
"""
def writeLog(info, file):
if os.path.exists(file):
with open(file, 'a')as fd:
fd.write(info)
else:
with open(file, 'w') as fd:
fd.write(info)
"""
删除 日志 将操作记录写入文件
"""
def cleanLog(filename):
now = datetime.datetime.now()
fileSize = printHuamSize(filename)
if os.path.exists(filename):
try:
print 'clean log file %s' % filename
# os.remove(filename)
remove_info = 'remove file:%s size:%s remove at time:%s;\n' % (filename,fileSize, now)
remove_log = os.path.join(remove_log_dir, rw_log)
writeLog(remove_info, remove_log)
print remove_info
except Exception,e:
print e
print'remove fail rewrite logfile'
else:
pass
def main():
# 主函数调用
serNameLogdict = _init()
for logPath, days in serNameLogdict.iteritems():
for filepath in listDirName(logPath):
if fileNameMtime(filepath, days):
#print printHuamSize(filepath)
cleanLog(filepath)
else:
pass
# print "%s not need to remove" % filepath
if __name__ == '__main__':
main()