From 6f5802551f6a21df4987f187aca252a6aa6b746e Mon Sep 17 00:00:00 2001 From: Rock Chin <1010553892@qq.com> Date: Tue, 28 Feb 2023 23:02:09 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=88=86=E6=96=87=E4=BB=B6=E5=AD=98?= =?UTF-8?q?=E5=82=A8=E6=97=A5=E5=BF=97=20(#190)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 3 ++- main.py | 24 +++++++++++++++++++++++- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index d93fc56..3fd3bea 100644 --- a/.gitignore +++ b/.gitignore @@ -8,4 +8,5 @@ config.py plugins/ !plugins/__init__.py /revcfg.py -prompts/ \ No newline at end of file +prompts/ +logs/ \ No newline at end of file diff --git a/main.py b/main.py index a09af70..394d5ca 100644 --- a/main.py +++ b/main.py @@ -45,8 +45,28 @@ def init_db(): known_exception_caught = False +log_file_name = "qchatgpt.log" + + +def init_runtime_log_file(): + """为此次运行生成日志文件 + 格式: qchatgpt-yyyy-MM-dd-HH-mm-ss.log + """ + global log_file_name + + # 检查logs目录是否存在 + if not os.path.exists("logs"): + os.mkdir("logs") + + # 检查本目录是否有qchatgpt.log,若有,移动到logs目录 + if os.path.exists("qchatgpt.log"): + shutil.move("qchatgpt.log", "logs/qchatgpt.legacy.log") + + log_file_name = "logs/qchatgpt-%s.log" % time.strftime("%Y-%m-%d-%H-%M-%S", time.localtime()) + def reset_logging(): + global log_file_name assert os.path.exists('config.py') config = importlib.import_module('config') @@ -60,7 +80,7 @@ def reset_logging(): logging.getLogger().removeHandler(handler) logging.basicConfig(level=config.logging_level, # 设置日志输出格式 - filename='qchatgpt.log', # log日志输出的文件位置和文件名 + filename=log_file_name, # log日志输出的文件位置和文件名 format="[%(asctime)s.%(msecs)03d] %(filename)s (%(lineno)d) - [%(levelname)s] : %(message)s", # 日志输出的格式 # -8表示占位符,让输出左对齐,输出长度都为8位 @@ -98,6 +118,8 @@ def main(first_time_init=False): import pkg.utils.context pkg.utils.context.set_config(config) + init_runtime_log_file() + sh = reset_logging() # 检查是否设置了管理员