简体   繁体   English

在PyCharm调试运行中使用logging.DEBUG

[英]Use logging.DEBUG in PyCharm debug run

What is a good way to set the logging level for a module in pycharm so that debugging statements will print during a debug session? 在pycharm中设置模块日志级别的好方法是什么,以便在调试会话期间打印调试语句? I am using the native python logging module in python 2.7. 我正在python 2.7中使用本机python日志记录模块。

Python logging level can be set inside the Python module, not outside from PyCharm. 可以在Python模块内部而不是PyCharm外部设置Python日志记录级别。 Your Python application must adjust its own logging level. 您的Python应用程序必须调整自己的日志记录级别。

  • Call logging.getLogger() to get the root logger (topmost in logging hierarchy) 调用logging.getLogger()以获取根记录器(在记录层次结构中最顶层)

  • Set environment variable in your run configuration in PyCharm. 在PyCharm中的运行配置中设置环境变量。 Detect this using os.environ.get("MYDEBUgVARNAME") and then call root.setLevel(logging.DEBUG) . 使用os.environ.get("MYDEBUgVARNAME") ,然后调用root.setLevel(logging.DEBUG) I am not sure if PyCharm itself sets any variable based on if it's normal or debug run. 我不确定PyCharm本身是否根据正常运行或调试运行来设置任何变量。

For more information check my blog post for standard Python logging patters https://opensourcehacker.com/2016/05/22/python-standard-logging-pattern/ 有关更多信息,请查看我的博客文章以了解标准的Python日志记录模式https://opensourcehacker.com/2016/05/22/python-standard-logging-pattern/

A more straightforward implementation of the previous answer might be to use the DEBUG variable since this is already available to your code... 上一个答案的更直接的实现可能是使用DEBUG变量,因为您的代码已经可以使用它了。

if __debug__:
logging.basicConfig(level=logging.DEBUG)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM