一、问题描述
在浏览器中我们关闭正在训练的模型打印后,再次打开可以看到没有任何信息输出了。除了处理缓存方法,我们可以使用python的logging输出日志。
二、代码编写
import logging
import sys
import datetime
def init_logger(filename, logger_name):
'''
@brief:
initialize logger that redirect info to a file just in case we lost connection to the notebook
@params:
filename: to which file should we log all the info
logger_name: an alias to the logger
'''
# get current timestamp
timestamp = datetime.datetime.utcnow().strftime('%Y%m%d_%H-%M-%S')
logging.basicConfig(
level=logging.INFO,
format='[%(asctime)s] %(name)s {%(filename)s:%(lineno)d} %(levelname)s - %(message)s',
handlers=[
logging.FileHandler(filename=filename),
logging.StreamHandler(sys.stdout)
]
)
# Test
logger = logging.getLogger(logger_name)
logger.info('### Init. Logger {} ###'.format(logger_name))
return logger
# Initialize
my_logger = init_logger("./ml_notebook.log", "ml_logger")
每次打印输出如:
my_logger.info("XXX")
再如记录上一个cell的时间和输出,打印到日志的文件里,比如在第一个cell里:
%%capture out
%%timeit
a = 1+1
然后在下一个cell里:
my_logger.info("capture & timeit: " + out.stdout)
就能知道上一个cell运行的时间和内容了。
参考:
jupyter notebook 如何在关闭浏览器后后台执行并保存结果? - 知乎 (zhihu.com)
logging — Logging facility for Python — Python 3.10.5 documentation