当我们在jupyter notebook使用logging的时候,发现logging.info,logging.debug都不输出信息,经过google找到了一个example 代码:
import logging
logger = logging.getLogger()
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
# Setup file handler
fhandler = logging.FileHandler('my.log')
fhandler.setLevel(logging.DEBUG)
fhandler.setFormatter(formatter)
# Configure stream handler for the cells
chandler = logging.StreamHandler()
chandler.setLevel(logging.DEBUG)
chandler.setFormatter(formatter)
# Add both handlers
logger.addHandler(fhandler)
logger.addHandler(chandler)
logger.setLevel(logging.DEBUG)
# Show the handlers
logger.handlers
# Log Something
logger.info("Test info")
logger.debug("Test debug")
logger.error("Test error")
在我的电脑里面运行成功了
参考文献
[1]. Get Output From the logging Module in IPython Notebook. https://stackoverflow.com/questions/18786912/get-output-from-the-logging-module-in-ipython-notebook