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)