从 IPython Notebook 中的日志记录模块获取输出

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18786912/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 11:50:49  来源:igfitidea点击:

Get Output From the logging Module in IPython Notebook

pythonloggingjupyter-notebookjupyter

提问by Kyle Brandt

When I running the following inside IPython Notebook I don't see any output:

当我在 IPython Notebook 中运行以下命令时,我看不到任何输出:

import logging
logging.basicConfig(level=logging.DEBUG)
logging.debug("test")

Anyone know how to make it so I can see the "test" message inside the notebook?

任何人都知道如何制作它以便我可以看到笔记本内的“测试”消息?

采纳答案by falsetru

Try following:

尝试以下操作:

import logging
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
logging.debug("test")

According to logging.basicConfig:

根据logging.basicConfig

Does basic configuration for the logging system by creating a StreamHandler with a default Formatter and adding it to the root logger. The functions debug(), info(), warning(), error() and critical() will call basicConfig() automatically if no handlers are defined for the root logger.

This function does nothing if the root logger already has handlers configured for it.

通过创建带有默认格式化程序的 StreamHandler 并将其添加到根记录器,为日志记录系统进行基本配置。如果没有为根记录器定义处理程序,函数 debug()、info()、warning()、error() 和 critical() 将自动调用 basicConfig()。

如果根记录器已经为其配置了处理程序,则此函数不执行任何操作。

It seems like ipython notebook call basicConfig (or set handler) somewhere.

似乎 ipython notebook 在某处调用 basicConfig (或设置处理程序)。

回答by Marigold

If you still want to use basicConfig, reload the logging module like this

如果您仍然想使用basicConfig,请像这样重新加载日志记录模块

from importlib import reload  # Not needed in Python 2
import logging
reload(logging)
logging.basicConfig(format='%(asctime)s %(levelname)s:%(message)s', level=logging.DEBUG, datefmt='%I:%M:%S')

回答by skulz00

My understanding is that the IPython session starts up logging so basicConfig doesn't work. Here is the setup that works for me (I wish this was not so gross looking since I want to use it for almost all my notebooks):

我的理解是 IPython 会话启动日志记录,因此 basicConfig 不起作用。这是对我有用的设置(我希望这看起来不那么粗糙,因为我想将它用于我几乎所有的笔记本):

import logging
logger = logging.getLogger()
fhandler = logging.FileHandler(filename='mylog.log', mode='a')
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
fhandler.setFormatter(formatter)
logger.addHandler(fhandler)
logger.setLevel(logging.DEBUG)

Now when I run:

现在当我运行时:

logging.error('hello!')
logging.debug('This is a debug message')
logging.info('this is an info message')
logging.warning('tbllalfhldfhd, warning.')

I get a "mylog.log" file in the same directory as my notebook that contains:

我在与笔记本相同的目录中得到一个“mylog.log”文件,其中包含:

2015-01-28 09:49:25,026 - root - ERROR - hello!
2015-01-28 09:49:25,028 - root - DEBUG - This is a debug message
2015-01-28 09:49:25,029 - root - INFO - this is an info message
2015-01-28 09:49:25,032 - root - WARNING - tbllalfhldfhd, warning.

Note that if you rerun this without restarting the IPython session it will write duplicate entries to the file since there would now be two file handlers defined

请注意,如果您在不重新启动 IPython 会话的情况下重新运行它,它将向文件写入重复的条目,因为现在将定义两个文件处理程序

回答by Ataxias

Bear in mind that stderr is the default stream for the loggingmodule, so in IPython and Jupyter notebooks you might not see anything unless you configure the stream to stdout:

请记住,stderr 是logging模块的默认流,因此在 IPython 和 Jupyter notebook 中,除非您将流配置为 stdout,否则您可能看不到任何内容:

import logging
import sys

logging.basicConfig(format='%(asctime)s | %(levelname)s : %(message)s',
                     level=logging.INFO, stream=sys.stdout)

logging.info('Hello world!')

回答by Garvey

You can configure logging by running %config Application.log_level="INFO"

您可以通过运行来配置日志记录 %config Application.log_level="INFO"

For more information, see IPython kernel options

有关更多信息,请参阅IPython 内核选项

回答by mcsim

What worked for me now (Jupyter, notebook server is: 5.4.1, IPython 7.0.1)

现在对我有用(Jupyter,笔记本服务器是:5.4.1,IPython 7.0.1)

import logging
logging.basicConfig()
logger = logging.getLogger('Something')
logger.setLevel(logging.DEBUG)

Now I can use logger to print info, otherwise I would see only message from the default level (logging.WARNING) or above.

现在我可以使用记录器打印信息,否则我只会看到来自默认级别 ( logging.WARNING) 或更高级别的消息。

回答by Brig

I setup a logger for both file and I wanted it to show up on the notebook. Turns out adding a filehandler clears out the default stream handlder.

我为这两个文件设置了一个记录器,我希望它显示在笔记本上。原来添加文件处理程序会清除默认的流处理程序。

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")

回答by stason

It seems that solutions that worked for older versions of ipython/jupyter no longer work.

似乎适用于旧版本 ipython/jupyter 的解决方案不再有效。

Here is a working solution for ipython 7.9.0 (also tested with jupyter server 6.0.2):

这是 ipython 7.9.0 的工作解决方案(也使用 jupyter server 6.0.2 进行了测试):

import logging
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
logging.debug("test message")

DEBUG:root:test message