-
Notifications
You must be signed in to change notification settings - Fork 0
/
log.py
76 lines (69 loc) · 2.38 KB
/
log.py
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
import datetime
import os
import platform
from config import get_admin_zkzh
frmt = "%Y-%m-%d %H:%M:%S"
# 日志和输出的分隔符
def fenGe(msgtext):
fenge = "\n"
fenge += "*" * 30 + datetime.datetime.now().strftime(frmt) + "*" * 50 + "\n"
fenge += msgtext + "\n"
return fenge
def log(zkzh, content):
content = fenGe(content)
print(content)
if platform.system() == 'Windows':
Separator = "\\"
else:
Separator = "/"
path = "logs" + Separator
if not os.path.exists(path) and not os.path.isdir(path):
os.mkdir(path)
if zkzh != "admin":
if zkzh == get_admin_zkzh():
zkzh = "admin"
path += zkzh + Separator
nowtime = datetime.datetime.now()
if os.path.exists(path) and os.path.isdir(path):
files_list = []
for files in os.walk(path):
for file in files[2]:
if "log-" in file:
data_time = file[file.find("-", 4)+1:-4]
files_list.append(data_time)
if len(files_list) > 0:
time1 = datetime.datetime.strptime("1900-01-01-00-00-00", "%Y-%m-%d-%H-%M-%S")
for data_time in files_list:
time2 = datetime.datetime.strptime(data_time, "%Y-%m-%d-%H-%M-%S")
if time2 > time1:
time1 = time2
else:
time1 = nowtime
file_name = path + "log-" + zkzh + time1.strftime("-%Y-%m-%d-%H-%M-%S") + ".txt"
if os.path.exists(file_name):
h = open(file_name, 'r', encoding="utf-8")
line_count = 0
for count, line in enumerate(h):
line_count += 1
h.close()
else:
line_count = 0
f = open(file_name, "a+", encoding="utf-8")
if line_count >= 1000:
f.close()
file_name = path + "log-" + zkzh + nowtime.strftime("-%Y-%m-%d-%H-%M-%S") + ".txt"
g = open(file_name, "a", encoding="utf-8")
g.write(content)
g.close()
return True
elif line_count < 1000:
f.write(content)
f.close()
return True
elif not os.path.exists(path):
os.mkdir(path)
file_name = path + "log-" + zkzh + nowtime.strftime("-%Y-%m-%d-%H-%M-%S") + ".txt"
f = open(file_name, "a", encoding="utf-8")
f.write(content)
f.close()
return True