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

时间:2022-05-28 22:03:59

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

当我运行IPython的笔记本电脑里面,下面我没有看到任何输出:

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?

任何人都知道如何使它这样我就可以看到里面的笔记本“测试”的消息?

5 个解决方案

#1


67  

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.

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

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

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

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

似乎ipython notebook在某处调用了basicConfig(或set handler)。

#2


47  

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

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

import logging
reload(logging)
logging.basicConfig(format='%(asctime)s %(levelname)s:%(message)s', level=logging.DEBUG, datefmt='%I:%M:%S')

#3


17  

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会话的情况下重新运行它,它将向文件写入重复的条目,因为现在将定义两个文件处理程序

#4


6  

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

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

import logging
import sys

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

logging.info('Hello world!')

#5


6  

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

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

For more information, see IPython kernel options

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

#1


67  

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.

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

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

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

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

似乎ipython notebook在某处调用了basicConfig(或set handler)。

#2


47  

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

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

import logging
reload(logging)
logging.basicConfig(format='%(asctime)s %(levelname)s:%(message)s', level=logging.DEBUG, datefmt='%I:%M:%S')

#3


17  

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会话的情况下重新运行它,它将向文件写入重复的条目,因为现在将定义两个文件处理程序

#4


6  

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

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

import logging
import sys

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

logging.info('Hello world!')

#5


6  

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

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

For more information, see IPython kernel options

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