-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.py
More file actions
89 lines (82 loc) · 2.34 KB
/
util.py
File metadata and controls
89 lines (82 loc) · 2.34 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
import os
import sys
import time
import json
import hashlib
import pendulum
from pathlib import Path
from loguru import logger
def lumos(cmd, warning=False):
# res = 0
if warning == True:
logger.warning("➜ " + cmd)
else:
logger.debug("➜ " + cmd)
res = os.system(cmd)
return res
def set_datetime(record):
record["extra"]["datetime"] = pendulum.now("Asia/Shanghai")
def logConfig(
log_file="logs/default.log",
rotation="10 MB",
level="DEBUG",
lite=False,
):
"""
配置 Loguru 日志记录
:param log_level: 日志级别,如 "DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"
:param log_file: 日志文件路径
:param rotation: 日志文件轮换设置,如 "10 MB" 表示文件大小达到 10MB 时轮换
使用方法
# 在程序开始时配置日志
from model.util import logConfig, logger
logConfig(log_file="myapp.log", rotation="5 MB")
# 使用 logger 记录日志
logger.info("This is an info message")
logger.debug("This is a debug message")
"""
logger.remove()
if lite:
style = (
" [ <level>{level: <8}</level>] "
# + "<cyan>{name}</cyan>:<cyan>{function}</cyan>:<cyan>{line}</cyan>"
# + "<green>♻ </green>"
+ "<level>{message}</level>"
)
else:
style = (
"<green>{extra[datetime]}</green>"
+ " [ <level>{level: <8}</level>] "
+ "<cyan>{name}</cyan>:<cyan>{function}</cyan>:<cyan>{line}</cyan>"
+ "<green>♻ </green>"
+ "<level>{message}</level>"
)
# alternative ➲ ⛏ ☄ ➜ ♻ ✨
logger.configure(patcher=set_datetime)
logger.add(sys.stderr, level=level, colorize=True, format=style)
logger.add(
log_file, colorize=False, encoding="utf-8", format=style, rotation=rotation
)
logger.add(
log_file + ".rich",
colorize=True,
encoding="utf-8",
format=style,
rotation=rotation,
)
logger.add(
log_file + ".warning",
level="WARNING",
colorize=False,
encoding="utf-8",
format=style,
rotation=rotation,
)
logger.add(
log_file + ".warning.rich",
level="WARNING",
colorize=True,
encoding="utf-8",
format=style,
rotation=rotation,
)