From 465b097ae662e33ec6455544be7fa10371d8b56d Mon Sep 17 00:00:00 2001 From: lusiyi Date: Tue, 21 Feb 2023 15:51:03 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=AD=A3=E6=97=A5=E5=BF=97log?= =?UTF-8?q?=E7=9B=AE=E5=BD=95=E4=B8=8D=E5=AD=98=E5=9C=A8=E6=97=B6=E6=97=A0?= =?UTF-8?q?=E6=B3=95=E8=BF=90=E8=A1=8C=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config.py | 13 ++++++++++++- utils.py | 2 +- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/config.py b/config.py index 8b7a939..3afe25e 100644 --- a/config.py +++ b/config.py @@ -1,12 +1,23 @@ import os +import pathlib from datetime import datetime from utils import create_logger +def ensure_exist(filePath): + dir = pathlib.Path(os.path.dirname(filePath)) + if not dir.exists(): + os.makedirs(os.path.dirname(filePath)) + file=pathlib.Path(filePath) + if file.is_file() and not file.exists(): + file.touch() # 项目根目录 root_path = os.path.abspath(os.path.dirname(__file__)) # 创建日志对象 -logger = create_logger(root_path + '/logs/' + str(datetime.date(datetime.now())) + '.log') +log_file = root_path + '/logs/' + str(datetime.date(datetime.now())) + '.log' +ensure_exist(log_file) +logger = create_logger(log_file) + # PostgreSQL连接配置 host = '118.31.13.117' diff --git a/utils.py b/utils.py index 7584d7b..323fe62 100644 --- a/utils.py +++ b/utils.py @@ -12,7 +12,7 @@ def create_logger(log_path): logger = logging.getLogger(__name__) logger.setLevel(logging.INFO) - formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s') + formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(filename)s[%(lineno)d]- %(message)s') # 创建一个handler,用于写入日志文件 file_handler = logging.FileHandler(filename=log_path)