diff --git a/.gitignore b/.gitignore index d93fc56a..3fd3beab 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 a09af708..394d5cab 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() # 检查是否设置了管理员