sakurashinken
sakurashinken

Reputation: 4078

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? I am using the native python logging module in python 2.7.

Upvotes: 3

Views: 3321

Answers (2)

naren8642
naren8642

Reputation: 51

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)

Upvotes: 1

Mikko Ohtamaa
Mikko Ohtamaa

Reputation: 83716

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/

Upvotes: 1

Related Questions