【Python Loguru】实现日志工具和日志饶接
【Python Loguru】实现日志工具和日志饶接
说明
代码使用第三方库Loguru实现了logging工具,可以直接改名字后就使用,并实现了日志绕接,使用前需要先安装loguru工具。
Code
import os
import json
from loguru import logger# config_file = "basic_config.json" # for production
config_file = "basic_config_dev.json" # for local developmentdef get_logger(name: str) -> logger:r"""Gets a standard logger with a file sink.Rotate when the log file is larger than 500M; Keep the last 10 log files;Compress the log file when it is too big."""current_dir = os.path.dirname(os.path.abspath(__file__))basic_config = os.path.join(current_dir, '../config/', config_file)with open(basic_config,"r") as f:config = json.load(f)log_file = config["log_file_path"] + config["project_name"].lower() + ".log"logger.configure(handlers=[{"sink": log_file,"format": "{time} - {level} - {name} - {function} - {message}","level": "INFO","rotation": "500 MB","retention": 10,"compression": "zip"}])return logger