简体   繁体   English

如何在 Jupyter 笔记本中隐藏记录器 output 但仍继续后端记录

[英]How to hide logger output in Jupyter notebook but still continue the backend logging

Is there a way to hide the printed output inside Jupyter notebook (the red background section encircled in green) while still maintaining the info logging capabilities such as saving the logs into a.log file?有没有办法在 Jupyter 笔记本中隐藏打印的 output (红色背景部分被绿色包围),同时仍然保持信息记录功能,例如将日志保存到 a.log 文件中?

在此处输入图像描述

I saw threads talking about changing the LEVEL, but that is not what I am looking for since I want to keep the level at INFO to continue logging the INFO logs.我看到线程谈论更改级别,但这不是我想要的,因为我想将级别保持在 INFO 以继续记录 INFO 日志。

FYI the display in red in Jupyter is from the sys.stderr stream.仅供参考,Jupyter 中的红色显示来自sys.stderr stream。 What you are looking for is to perform logging that writes to file without writing to STDERR.您正在寻找的是执行写入文件而不写入 STDERR 的日志记录。

Following docs for the logging standard library , you can set up a logger that writes to a FileHandler, as below:按照日志记录标准库的文档,您可以设置一个写入 FileHandler 的记录器,如下所示:

import logging
logger = logging.getLogger('my_application')
logger.setLevel(logging.DEBUG)

fh = logging.FileHandler('my_log_file.log')
fh.setLevel(logging.DEBUG)
logger.addHandler(fh)

# Does not write to sys.stderr
logger.info("These logs do not show up on Jupyter!")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM