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

 

posted @ 2020-11-07 11:41  明天OoO你好  阅读(179)  评论(0编辑  收藏  举报