flask 入门之 logging

如想看详细说明,请到:

1.https://www.cnblogs.com/yyds/p/6901864.html

2.https://docs.python.org/2/library/logging.html

我只做简单使用介绍

日志有等级设置

如果设置的等级为3,则高于3级的会输出,其它的不会输出,但是如何设置呢?

logger = logging.getLogger()
logger.setLevel(logging.CRITICAL)

logging.info("this is info")
logging.debug("this is debug")
logging.warning("this is warning")
logging.error("this is error")
logging.critical("this is critical")

但是,为了看日志得瞅着运行记录盯一天,眼睛实在受不了,这时候,我们可以把日志写到文档里,如何写呢?

logger = logging.getLogger(__name__)
logger.setLevel(level=logging.INFO)
handler = logging.FileHandler("ly.log")
handler.setLevel(logging.INFO)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
handler.setFormatter(formatter)

console = logging.StreamHandler()
console.setLevel(logging.INFO)

logger.addHandler(handler)
logger.addHandler(console)

logger.info("this is the blog of the system")

猜你喜欢

转载自blog.csdn.net/itlanyue/article/details/81199638