当前位置:网站首页>Jupyter notebook solves the problem that printing information cannot be viewed after closing the browser
Jupyter notebook solves the problem that printing information cannot be viewed after closing the browser
2022-07-27 23:01:00 【Go crazy first.】
One 、 Problem description
In the browser, we close the model we are training after printing , Open it again and you can see that there is no information output . In addition to handling cache methods , We can use python Of logging Output log .
Two 、 Code writing
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")Print out each time, such as :
my_logger.info("XXX")Another example is to record the last cell Time and output of , Print to the log file , For example, in the first cell in :
%%capture out
%%timeit
a = 1+1And then next cell in :
my_logger.info("capture & timeit: " + out.stdout)You can know the last cell Running time and content .
Reference resources :
logging — Logging facility for Python — Python 3.10.5 documentation
边栏推荐
猜你喜欢

jvm组成及内存模型

Complete Guide to IOT architecture

Three consecutive high-frequency interview questions of redis online celebrity: cache penetration? Cache breakdown? Cache avalanche?

Analysis of cloud native application security organization structure

Quartus:Instantiation of ‘sdram_model_plus‘ failed. The design unit was not found.

Parameter transmission of components

catch all in one draft! Introduction to 10 data visualization software

51 MCU internal peripherals: real time clock (SPI)

云计算服务主要安全风险及应对措施

图论的小技巧以及扩展
随机推荐
2022/4/11 考试总结
leetcode-461.汉明距离
What is the b+tree index of MySQL? How does the cluster index grow?
Promise solves asynchrony
已有6名员工确诊!三星第三度关闭龟尾手机工厂!
cron 表达式
Chrome realizes automated testing: recording and playback web page actions
The wave of smart home is coming, how to make machines understand the world [there is information at the end]
Eight years of love between me and the message queue
SparkSQL的UDF及分析案例,220726,,
美国官员建议特朗普阻止英飞凌收购赛普拉斯
Pyqt5 rapid development and practice 4.9 dialog controls
2022/3/10 exam summary
Jstack stuff
视频人体行为检测
干货|语义网、Web3.0、Web3、元宇宙这些概念还傻傻分不清楚?(中)
Take byte offer in four rounds and answer the interview questions
Shandong football match
jstack那些事
ADI、世健、骏龙科技共同捐赠230万元助力湖北抗疫