python logger 控制台,滚动文件

def setup_logger(logger_name, level=logging.INFO):
    logging.basicConfig(format='%(asctime)s - %(filename)s[line:%(lineno)d] - %(levelname)s: %(message)s',
                        level=logging.DEBUG,
                        filename='./logs/CanTo4G.log',
                        filemode='a')
    logger = logging.getLogger('CanTo4G')
    logger.setLevel(level=logging.DEBUG)

    formatter = logging.Formatter('%(asctime)s - %(filename)s[line:%(lineno)d] - %(levelname)s: %(message)s')
    stream_handler = logging.StreamHandler()
    stream_handler.setLevel(logging.DEBUG)
    stream_handler.setFormatter(formatter)
    logger.addHandler(stream_handler)
    handler = RotatingFileHandler(filename='./logs/CanTo4G.log', maxBytes=50*1024, backupCount=50)
    logger.addHandler(handler)
    return logger
    pass

 

posted on 2024-12-03 14:23  郭小四  阅读(3)  评论(0编辑  收藏  举报