如果我在PDB中,为什么不将Python记录模块显示我的记录到屏幕?

发布于 2025-02-08 12:57:41 字数 1613 浏览 1 评论 0原文

我想让我的logging.info Python Debugger(PDB)和文件。但是,它没有出现在屏幕上。它可以在单独的python控制台中工作。为什么它在PDB中不起作用?请参阅:

bot@18f71e53b3f5:~/pycoq/tutorial$ python brandos_pycoq_tutorial.py
> /home/bot/pycoq/tutorial/brandos_pycoq_tutorial.py(142)<module>()
-> main()
(Pdb) import logging
(Pdb)
(Pdb) level    = logging.INFO
(Pdb) format   = '  %(message)s'
(Pdb) handlers = [logging.FileHandler('./filename.log'), logging.StreamHandler()]
(Pdb)
(Pdb) logging.basicConfig(level = level, format = format, handlers = handlers)
(Pdb) logging.info('Hey, this is working!')
(Pdb) --KeyboardInterrupt--
(Pdb)

在控制台中的作品:

bot@18f71e53b3f5:~/pycoq/tutorial$ python
Python 3.9.12 (main, Jun  1 2022, 11:38:51)
[GCC 7.5.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import logging
>>>
>>> level    = logging.INFO
>>> format   = '  %(message)s'
>>> handlers = [logging.FileHandler('./filename.log'), logging.StreamHandler()]
>>>
>>> logging.basicConfig(level = level, format = format, handlers = handlers)
>>> logging.info('Hey, this is working!')
  Hey, this is working!
>>>
KeyboardInterrupt
>>>

该级别的设置正确如下所述:打印到屏幕上并同时写入文件

I want to have my logging.info show in the terminal/screen in the Python debugger (pdb) and on file. However, it does not appear on the screen. It works in a seperate python console just fine. Why does it not work in pdb? See:

bot@18f71e53b3f5:~/pycoq/tutorial$ python brandos_pycoq_tutorial.py
> /home/bot/pycoq/tutorial/brandos_pycoq_tutorial.py(142)<module>()
-> main()
(Pdb) import logging
(Pdb)
(Pdb) level    = logging.INFO
(Pdb) format   = '  %(message)s'
(Pdb) handlers = [logging.FileHandler('./filename.log'), logging.StreamHandler()]
(Pdb)
(Pdb) logging.basicConfig(level = level, format = format, handlers = handlers)
(Pdb) logging.info('Hey, this is working!')
(Pdb) --KeyboardInterrupt--
(Pdb)

works in console:

bot@18f71e53b3f5:~/pycoq/tutorial$ python
Python 3.9.12 (main, Jun  1 2022, 11:38:51)
[GCC 7.5.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import logging
>>>
>>> level    = logging.INFO
>>> format   = '  %(message)s'
>>> handlers = [logging.FileHandler('./filename.log'), logging.StreamHandler()]
>>>
>>> logging.basicConfig(level = level, format = format, handlers = handlers)
>>> logging.info('Hey, this is working!')
  Hey, this is working!
>>>
KeyboardInterrupt
>>>

the level is set right as refered here: Printing to screen and writing to a file at the same time

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

萤火眠眠 2025-02-15 12:57:41

这可能是因为您的brandos_pycoq_tutorial.py脚本对sys.stderr做点事。如果在pdb中,它可以正常工作,如果您尝试过:

C:\Users\Vinay>python -c "import pdb; pdb.set_trace()"
--Return--
> <string>(1)<module>()->None
(Pdb) import logging
(Pdb) level = logging.INFO
(Pdb) format   = '  %(message)s'
(Pdb) handlers = [logging.FileHandler('./filename.log'), logging.StreamHandler()]
(Pdb) logging.basicConfig(level = level, format = format, handlers = handlers)
(Pdb) logging.info('Hey, this is working!')
  Hey, this is working!
(Pdb)

我在Windows中进行了此操作,但是我希望它能在其他平台上以相同的方式工作。

It might be because your brandos_pycoq_tutorial.py script does something to sys.stderr. It works fine if in pdb if you try this:

C:\Users\Vinay>python -c "import pdb; pdb.set_trace()"
--Return--
> <string>(1)<module>()->None
(Pdb) import logging
(Pdb) level = logging.INFO
(Pdb) format   = '  %(message)s'
(Pdb) handlers = [logging.FileHandler('./filename.log'), logging.StreamHandler()]
(Pdb) logging.basicConfig(level = level, format = format, handlers = handlers)
(Pdb) logging.info('Hey, this is working!')
  Hey, this is working!
(Pdb)

I did this in Windows, but I expect it will work the same way on other platforms.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文