#!/usr/bin/python
# -*- coding: utf-8 -*-
# @time : 2023/2/3 15:21
# @author : pugongying
# @description : 日志配置
# 获取日志目录
import logging
import os
from functools import lru_cache
from logging.handlers import RotatingFileHandler
from app.config import configs
log_dir_path = configs.WORK_DIR + os.sep + 'log'
if not os.path.isdir(log_dir_path):
os.makedirs(log_dir_path)
logger = logging.getLogger()
# 设置logger可输出日志级别范围
logger.setLevel(logging.INFO)
@lru_cache()
def log_init():
# 添加控制台handler,用于输出日志到控制台
console_handler = logging.StreamHandler()
# 添加日志文件handler,用于输出日志到文件中
file_handler = RotatingFileHandler(filename=log_dir_path + os.sep + 'link-data.log',
maxBytes=50 * 1024 * 1024,
backupCount=9,
encoding='UTF-8')
# 设置格式并赋予handler
formatter = logging.Formatter(
'[%(asctime)s] -- %(levelname)s - [%(thread)d][%(threadName)s] -- %(filename)s[line:%(lineno)d] %(name)s : %(message)s')
console_handler.setFormatter(formatter)
file_handler.setFormatter(formatter)
# 将handler添加到日志器中
logger.addHandler(console_handler)
logger.addHandler(file_handler)
logger.info("日志组件加载成功")
标签:插件,logging,log,FastAPI,handler,path,日志,logger
From: https://www.cnblogs.com/pgyLang/p/17195561.html