-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathutils.py
145 lines (127 loc) · 4.15 KB
/
utils.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
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
'''
Author: wangjia
Date: 2024-05-19 00:04:14
LastEditors: wangjia
LastEditTime: 2024-05-26 14:38:05
Description: file content
'''
from loguru import logger
from datetime import datetime
import time
import sys
class Logger:
@classmethod
def get_logger(self):
folder_ = "logs/"
prefix_ = "mylog-"
rotation_ = "10 MB"
retention_ = "30 days"
encoding_ = "utf-8"
backtrace_ = True
diagnose_ = True
# 格式里面添加了process和thread记录,方便查看多进程和线程程序
format_ = (
"<green>{time:YYYY-MM-DD HH:mm:ss.SSS}</green>"
"|<level>{level: <7}</level>"
"|<cyan>{name}</cyan>:<cyan>{function}</cyan>:<yellow>{line}</yellow>"
"|<level>{message}</level>")
# 这里面采用了层次式的日志记录方式,就是低级日志文件会记录比他高的所有级别日志,这样可以做到低等级日志最丰富,高级别日志更少更关键
# debug
logger.add(
folder_ + prefix_ + "debug.log",
level="DEBUG",
backtrace=backtrace_,
diagnose=diagnose_,
format=format_,
colorize=False,
rotation=rotation_,
retention=retention_,
encoding=encoding_,
filter=lambda record: record["level"].no >= logger.level("DEBUG").
no,
)
# info
logger.add(
folder_ + prefix_ + "info.log",
level="INFO",
backtrace=backtrace_,
diagnose=diagnose_,
format=format_,
colorize=False,
rotation=rotation_,
retention=retention_,
encoding=encoding_,
filter=lambda record: record["level"].no >= logger.level("INFO").
no,
)
# warning
logger.add(
folder_ + prefix_ + "warning.log",
level="WARNING",
backtrace=backtrace_,
diagnose=diagnose_,
format=format_,
colorize=False,
rotation=rotation_,
retention=retention_,
encoding=encoding_,
filter=lambda record: record["level"].no >= logger.level("WARNING")
.no,
)
# error
logger.add(
folder_ + prefix_ + "error.log",
level="ERROR",
backtrace=backtrace_,
diagnose=diagnose_,
format=format_,
colorize=False,
rotation=rotation_,
retention=retention_,
encoding=encoding_,
filter=lambda record: record["level"].no >= logger.level("ERROR").
no,
)
# critical
logger.add(
folder_ + prefix_ + "critical.log",
level="CRITICAL",
backtrace=backtrace_,
diagnose=diagnose_,
format=format_,
colorize=False,
rotation=rotation_,
retention=retention_,
encoding=encoding_,
filter=lambda record: record["level"].no >= logger.level("CRITICAL"
).no,
)
logger.add(
sys.stderr,
level="CRITICAL",
backtrace=backtrace_,
diagnose=diagnose_,
format=format_,
colorize=True,
filter=lambda record: record["level"].no >= logger.level("CRITICAL"
).no,
)
return logger
# 自定义日志输出
logger = Logger.get_logger()
# 获取当前时间,并格式化日期时间
current_time = datetime.now()
current_date = current_time.strftime("%Y-%m-%d")
formatted_time = current_time.strftime("%Y-%m-%d %H:%M:%S")
# 计算时间函数
def timeit(func):
def wrapper(*args, **kw):
start_time = time.time()
result = func(*args, **kw)
cost_time = time.time() - start_time
print("==" * 25)
print("Current Function [%s] run time is %s s" %
(func.__name__, cost_time))
print("==" * 25)
return result
return wrapper