3

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.

2 Answers 2

1

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/

Sign up to request clarification or add additional context in comments.

Comments

1

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.