Python中logging模块的使用

def get_logger():
    """同时输出至文件及console
    """

    logger = logging.getLogger("test")
    formatter = logging.Formatter(
        '%(asctime)s [%(levelname)s] %(message)s', '[%Y-%m-%d %H:%M:%S]')

    log_file = os.path.join(os.getcwd(), "analyze_log.txt")
    
    fh = logging.handlers.RotatingFileHandler(log_file, 'w', 1048576, 4)
    fh.setFormatter(formatter)

    ch = logging.StreamHandler()
    ch.setFormatter(formatter)

    logger.addHandler(fh)
    logger.addHandler(ch)
    logger.setLevel(logging.INFO)

    return logger

logger=get_logger()
logger.info("info")
logger.warn("warn")

你可能感兴趣的:(Python中logging模块的使用)