Use logging.DEBUG in PyCharm debug run
Question:
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? I am using the native python logging module in python 2.7.
Answers:
Python logging level can be set inside the Python module, not outside from PyCharm. Your Python application must adjust its own logging level.
-
Call logging.getLogger()
to get the root logger (topmost in logging hierarchy)
-
Set environment variable in your run configuration in PyCharm. Detect this using os.environ.get("MYDEBUgVARNAME")
and then call root.setLevel(logging.DEBUG)
. I am not sure if PyCharm itself sets any variable based on if it’s normal or debug run.
For more information check my blog post for standard Python logging patters 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…
if __debug__:
logging.basicConfig(level=logging.DEBUG)
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? I am using the native python logging module in python 2.7.
Python logging level can be set inside the Python module, not outside from PyCharm. Your Python application must adjust its own logging level.
-
Call
logging.getLogger()
to get the root logger (topmost in logging hierarchy) -
Set environment variable in your run configuration in PyCharm. Detect this using
os.environ.get("MYDEBUgVARNAME")
and then callroot.setLevel(logging.DEBUG)
. I am not sure if PyCharm itself sets any variable based on if it’s normal or debug run.
For more information check my blog post for standard Python logging patters 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…
if __debug__:
logging.basicConfig(level=logging.DEBUG)