修正日志log目录不存在时无法运行的问题

This commit is contained in:
lusiyi 2023-02-21 15:51:03 +08:00
parent dadf850385
commit 465b097ae6
2 changed files with 13 additions and 2 deletions

View File

@ -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'

View File

@ -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)