简体   繁体   中英

Python3 & PyCharm - Debug logging levels in run/debug

I'm just starting with PyCharm, is there a way to show debug & info warnings?

import logging
logger = logging.getLogger('tipper')
logger.setLevel(logging.DEBUG)
logger.debug('debug message')
logger.info('info message')
logger.warn('warn message')
logger.error('error message')
logger.critical('critical message')

warn, error, critical all show:

/home/username/someproject/.someprojectenv/bin/python3/home/username/someproject/go.py
warn message
error message
critical message

Process finished with exit code 0

However debug, info do not show.

The problem has nothing to do with PyCharm, but with how logging configuration works. If you try to write the code you have shown in a normal python interactive session you get the same output:

>>> import logging
>>> logger = logging.getLogger('tipper')
>>> logger.setLevel(logging.DEBUG)
>>> logger.debug('debug message')
>>> logger.info('info message')
>>> logger.warn('warn message')
warn message
>>> logger.error('error message')
error message
>>> logger.critical('critical message')
critical message

The problem is that setting the logger 's level isn't enough ! You must also add a handler to the logger otherwise the logger will simply forward the message up the chain. The messages will end up at the root logger, which has, by default, a level of logging.WARN and thus discards DEBUG level messages.

However if you add a handler to logger all works fine:

>>> logger.addHandler(logging.StreamHandler())
>>> logger.debug('test')
test

You can set more than one handler for each logger and each handler can have a different logging level.

See this question for a bit of further information about logger's and handler's levels. I'd suggest also to read carefully the documentation for the logging module and the various guides (eg the logging How-To , because it has a really advanced configuration.

Also from python3.2 there's a dictConfig function which allow you to specify the configuration for your logging hierarchy as dictionary, without having to manually create every handler and logger by hand.

Very dirty workaround just to get it run is to overwrite the logging functions. It may come in handy sometimes you just want to take a quick look without setting the whole debug level.

!DONT USE THIS IN PRODUCTION!

logging.debug = print
logging.info = print
import logging
import datetime

logging.basicConfig(
    level=logging.DEBUG,
    format="%(asctime)s [%(levelname)8.8s] %(message)s",
    handlers=[logging.StreamHandler(),
              logging.FileHandler(f'log/{datetime.datetime.now().isoformat().replace(":", "-")}.log', encoding='utf-8')],
)
logger = logging.getLogger(__name__)

Ad Bakuriu said, Python's default log level is WARNING , because otherwise the output would just get swamped.

Complementing their answer, one can also use basicConfig as demonstrated above. This modifies the settings of the root logger which all other loggers inherit from, meaning you don't have to set DEBUG in every file in your project. Note that libraries you import will also log on this level (unless you explicitly exclude them). This will do nothing if the root logger has already been initialized, unless you pass force=True .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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