Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/277.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从IPython笔记本中的日志模块获取输出_Python_Logging_Jupyter Notebook_Jupyter - Fatal编程技术网

从IPython笔记本中的日志模块获取输出

从IPython笔记本中的日志模块获取输出,python,logging,jupyter-notebook,jupyter,Python,Logging,Jupyter Notebook,Jupyter,在IPython笔记本中运行以下命令时,我没有看到任何输出: import logging logging.basicConfig(level=logging.DEBUG) logging.debug("test") 有人知道如何制作它,以便我可以看到笔记本中的“测试”消息吗?请尝试以下操作: import logging logger = logging.getLogger() logger.setLevel(logging.DEBUG) logging.debug("test") 根据:

在IPython笔记本中运行以下命令时,我没有看到任何输出:

import logging
logging.basicConfig(level=logging.DEBUG)
logging.debug("test")
有人知道如何制作它,以便我可以看到笔记本中的“测试”消息吗?

请尝试以下操作:

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

通过创建 具有默认格式设置程序并将其添加到根目录的StreamHandler 记录器。函数debug()、info()、warning()、error()和 如果没有处理程序,critical()将自动调用basicConfig() 为根记录器定义

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


似乎ipython笔记本电脑在某处调用了basicConfig(或set handler)。

如果仍要使用
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')
2020-10-28 17:07:08 [INFO] notebook - Hello world
2020-10-28 17:12:22 [INFO] notebook - More info here
2020-10-28 17:12:22 [INFO] notebook - And some more

我的理解是IPython会话启动日志记录,因此basicConfig无法工作。以下是适合我的设置(我希望这不是那么恶心,因为我想在几乎所有笔记本电脑上使用它):

现在当我跑步时:

logging.error('hello!')
logging.debug('This is a debug message')
logging.info('this is an info message')
logging.warning('tbllalfhldfhd, warning.')
我在笔记本的同一目录中得到一个“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.

请注意,如果在不重新启动IPython会话的情况下重新运行此操作,它将向文件写入重复的条目,因为现在将定义两个文件处理程序。请记住,stderr是
日志记录
模块的默认流,因此,在IPython和Jupyter笔记本中,除非将流配置为标准输出,否则可能看不到任何内容:

import logging
import sys

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

logging.info('Hello world!')

您可以通过运行
%config Application.log\u level=“INFO”

有关更多信息,请参见《我现在的工作原理》(Jupyter,笔记本服务器是:5.4.1,IPython 7.0.1)


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

我为这两个文件都设置了一个logger,并希望它显示在笔记本上。事实证明,添加文件处理程序会清除默认的流处理程序

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

我想要一个简单而直接的答案,有很好的风格输出,所以这里是我的建议

导入系统 导入日志记录 logging.basicConfig( 格式='%(asctime)s[%(levelname)s]%(name)s-%(message)s', 级别=logging.INFO, datefmt=“%Y-%m-%d%H:%m:%S”, stream=sys.stdout, ) log=logging.getLogger('笔记本') 然后,您可以在笔记本的任何位置使用
log.info()
或其他类似的输出

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')
2020-10-28 17:07:08 [INFO] notebook - Hello world
2020-10-28 17:12:22 [INFO] notebook - More info here
2020-10-28 17:12:22 [INFO] notebook - And some more

你使用的是哪个版本的IPython,因为它在1.0中工作?@ViktorKerkez
ipython3笔记本电脑--version
返回
1.0.0
我在尝试你的代码时得到了这个结果。@ViktorKerkez:是的,我不明白,我想我应该提出一个问题……在普通IPython控制台中也会出现同样的情况:它不打印任何东西,除非创建根
记录器
。此解决方案在
ipykernel
4.5(可能早在4.4)中再次起作用,否则将不再起作用。不适用于Jupyter笔记本电脑5.3.0适用于以下Jupyter安装:``$Jupyter--版本Jupyter core:4.6.3 Jupyter笔记本电脑:未安装qtconsole:未安装ipython:7.18.1 ipykernel:5.3.4 Jupyter客户端:6.1.7 Jupyter实验室:未安装nbconvert:未安装ipywidgets:not installed nbformat:not installed traitlets:5.0.5``注意,
logging.basicConfig
中的更改生效之前,似乎需要重新启动笔记本内核。对于任何试图在Python 3中执行此操作的人:从Python 3.5开始,您应该使用imp模块,因为imp模块已被弃用。如果任何人在Spyder的日志记录方面遇到问题(所有修改日志程序行为的尝试均未成功),这就结束了一天的白费力气。谢谢!为了减少这种“粗俗的外观”,将代码放在python路径上的一个模块中,然后导入它。更漂亮,更易于将来升级。或者使用logging.config.fileConfig('logging.conf')并将所有设置放在其中。欢迎使用StackOverflow,感谢您的帮助。你可能想通过添加一些解释来使你的答案更好。这实际上是对我最有用的答案!你能用一个例子加几行吗?打印日志消息需要调用什么记录器句柄?至少ipython 7.9.0(或jupyter 6.0.2)会忽略建议的代码,因为它不支持运行控制台中的此类。运行
%config
查看受支持的类,
应用程序
不在其中。这里是ipython 7.9.0。是的,这很有效。必须运行
basicConfig()
tp才能使其工作。