mirror of
https://github.com/RockChinQ/QChatGPT.git
synced 2024-11-16 11:42:44 +08:00
57 lines
1.5 KiB
Python
57 lines
1.5 KiB
Python
import logging
|
||
import os
|
||
import sys
|
||
import time
|
||
|
||
import colorlog
|
||
|
||
|
||
log_colors_config = {
|
||
"DEBUG": "green", # cyan white
|
||
"INFO": "white",
|
||
"WARNING": "yellow",
|
||
"ERROR": "red",
|
||
"CRITICAL": "cyan",
|
||
}
|
||
|
||
|
||
async def init_logging() -> logging.Logger:
|
||
level = logging.INFO
|
||
|
||
if "DEBUG" in os.environ and os.environ["DEBUG"] in ["true", "1"]:
|
||
level = logging.DEBUG
|
||
|
||
log_file_name = "logs/qcg-%s.log" % time.strftime(
|
||
"%Y-%m-%d-%H-%M-%S", time.localtime()
|
||
)
|
||
|
||
qcg_logger = logging.getLogger("qcg")
|
||
|
||
qcg_logger.setLevel(level)
|
||
|
||
color_formatter = colorlog.ColoredFormatter(
|
||
fmt="%(log_color)s[%(asctime)s.%(msecs)03d] %(pathname)s (%(lineno)d) - [%(levelname)s] :\n %(message)s",
|
||
datefmt="%Y-%m-%d %H:%M:%S",
|
||
log_colors=log_colors_config,
|
||
)
|
||
|
||
stream_handler = logging.StreamHandler(sys.stdout)
|
||
|
||
log_handlers: logging.Handler = [stream_handler, logging.FileHandler(log_file_name)]
|
||
|
||
for handler in log_handlers:
|
||
handler.setLevel(level)
|
||
handler.setFormatter(color_formatter)
|
||
qcg_logger.addHandler(handler)
|
||
|
||
logging.basicConfig(
|
||
level=logging.INFO, # 设置日志输出格式
|
||
format="[DEPR][%(asctime)s.%(msecs)03d] %(pathname)s (%(lineno)d) - [%(levelname)s] :\n%(message)s",
|
||
# 日志输出的格式
|
||
# -8表示占位符,让输出左对齐,输出长度都为8位
|
||
datefmt="%Y-%m-%d %H:%M:%S", # 时间输出的格式
|
||
handlers=[logging.NullHandler()],
|
||
)
|
||
|
||
return qcg_logger
|