python 日志记录与使用配置文件

import configparser
import logging
from logging.handlers import RotatingFileHandler

config = configparser.ConfigParser()
config.read("config", encoding="utf-8")

def get_logger(config):
    logger = logging.getLogger()
    rotating_log_handler = RotatingFileHandler(filename=config["loggers"]["logfile"], maxBytes=1024*1024*int(config["loggers"]["logMaxSize"]), backupCount=config["loggers"]["logBackupCount"])
    rotating_log_handler.setLevel(logging.DEBUG)
    formatter = logging.Formatter("%(asctime)s-%(filename)s-[line:%(lineno)d]-%(levelname)s: %(message)s")
    rotating_log_handler.setFormatter(formatter)
    logger.addHandler(rotating_log_handler)
    logger.setLevel(logging.DEBUG)
    return logger
原文地址:https://www.cnblogs.com/wt11/p/13940464.html