Kyl*_*ndt 109 python logging jupyter jupyter-notebook
当我在IPython Notebook中运行以下内容时,我看不到任何输出:
import logging
logging.basicConfig(level=logging.DEBUG)
logging.debug("test")
Run Code Online (Sandbox Code Playgroud)
任何人都知道如何制作它所以我可以看到笔记本内的"测试"消息?
fal*_*tru 106
试试以下:
import logging
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
logging.debug("test")
Run Code Online (Sandbox Code Playgroud)
通过使用默认Formatter创建StreamHandler并将其添加到根记录器来为日志记录系统执行基本配置.如果没有为根记录器定义处理程序,函数debug(),info(),warning(),error()和critical()将自动调用basicConfig().
如果根记录器已经为其配置了处理程序,则此函数不执行任何操作.
似乎ipython notebook在某处调用了basicConfig(或set handler).
Mar*_*old 58
如果您仍想使用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')
Run Code Online (Sandbox Code Playgroud)
sku*_*z00 25
我的理解是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)
Run Code Online (Sandbox Code Playgroud)
现在我跑的时候:
logging.error('hello!')
logging.debug('This is a debug message')
logging.info('this is an info message')
logging.warning('tbllalfhldfhd, warning.')
Run Code Online (Sandbox Code Playgroud)
我在与我的笔记本相同的目录中得到一个"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.
Run Code Online (Sandbox Code Playgroud)
请注意,如果在不重新启动IPython会话的情况下重新运行它,它将向文件写入重复的条目,因为现在将定义两个文件处理程序
小智 18
从日志记录版本 3.8 开始,force添加了一个参数,用于删除任何现有的处理程序,从而允许basicConfig工作。这适用于 IPython 版本 7.29.0 和 Jupyter Lab 版本 3.2.1。
import logging
logging.basicConfig(level=logging.DEBUG,
force = True)
logging.debug("test")
Run Code Online (Sandbox Code Playgroud)
重定向 REPL 控制台日志后,现在每次击键都可能发出自己的日志消息。例如,REPL ptpython在每次击键后开始发出这些日志消息。
>>> h
DEBUG:parso.python.diff:line_lengths old: 1; new: 1
DEBUG:parso.python.diff:diff parser start
DEBUG:parso.python.diff:-> code[replace] old[1:1] new[1:1]
DEBUG:parso.python.diff:line_lengths old: 1; new: 1
DEBUG:parso.python.diff:parse_part from 1 to 1 (to 0 in part parser)
DEBUG:parso.python.diff:-> code[replace] old[1:1] new[1:1] [F2] Menu - CPython 3.8.10
DEBUG:parso.python.diff:diff parser end
DEBUG:parso.python.diff:parse_part from 1 to 1 (to 0 in part parser)
DEBUG:parso.python.diff:diff parser end
DEBUG:asyncio:Using proactor: IocpProactor
>>> h
Run Code Online (Sandbox Code Playgroud)
REPL 变得非常嘈杂以至于无法使用。
通过将日志级别设置为不太详细的级别(例如logging.WARNING.
import logging
logging.basicConfig(level=logging.DEBUG,
force = True)
logging.debug("test")
Run Code Online (Sandbox Code Playgroud)
这可能适用于其他 REPL。只需在调用中替换适当的嘈杂记录器名称即可getLogger。
请记住,stderr是logging模块的默认流,因此在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!')
Run Code Online (Sandbox Code Playgroud)
您可以通过运行配置日志记录 %config Application.log_level="INFO"
有关更多信息,请参阅IPython内核选项
现在对我有用的(Jupyter,笔记本服务器是:5.4.1,IPython 7.0.1)
import logging
logging.basicConfig()
logger = logging.getLogger('Something')
logger.setLevel(logging.DEBUG)
Run Code Online (Sandbox Code Playgroud)
现在,我可以使用记录器来打印信息,否则,我只会看到默认级别(logging.WARNING)或更高级别的消息。
我为这两个文件设置了一个记录器,我希望它显示在笔记本上。结果添加文件处理程序会清除默认的流处理程序。
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")
Run Code Online (Sandbox Code Playgroud)
我想要一个简单直接的答案,并且输出样式漂亮,所以这是我的建议
import sys
import logging
logging.basicConfig(
format='%(asctime)s [%(levelname)s] %(name)s - %(message)s',
level=logging.INFO,
datefmt='%Y-%m-%d %H:%M:%S',
stream=sys.stdout,
)
log = logging.getLogger('notebook')
Run Code Online (Sandbox Code Playgroud)
然后,您可以在笔记本中的任何位置使用log.info()或任何其他日志记录级别,其输出如下所示
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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
54771 次 |
| 最近记录: |