Python 输出日志到文件的方法

def get_logger():
    LOG_FILE = r'C:\\PycharmProjects\book_tool.log'

    handler = logging.handlers.RotatingFileHandler(LOG_FILE, maxBytes=1024 * 1024, backupCount=5, encoding='utf-8')
    fmt = '%(asctime)s - %(levelname)s - %(message)s'

    formatter = logging.Formatter(fmt)
    handler.setFormatter(formatter)

    logger = logging.getLogger('book_tool')
    logger.addHandler(handler)
    logger.setLevel(logging.DEBUG)
    return logger

调用这个方法之后就可以将日志输出到指定的文件了

你可能感兴趣的:(Python基础)