Calmcode - logging: level

Python's logging level explained.

1 2 3 4 5 6 7

In this video we explain what the logging level means. There is a hierarchy that we can use while using the python loggers.

We've made a small change to the log.py file.

logger.py

Important: do not name this file logging.py.

import logging

logger = logging.getLogger(__name__)

# the handler determines where the logs go: stdout/file
handler = logging.StreamHandler()

# the formatter determines what our logs will look like
fmt = '%(levelname)s %(asctime)s [%(filename)s:%(funcName)s:%(lineno)d] %(message)s'
formatter = logging.Formatter(fmt)
handler.setFormatter(formatter)

logger.addHandler(handler)
# you can change this line to see different lines being logged
logger.setLevel(logging.WARNING)

logger.debug('this is a debug statement')
logger.info('this is a info statement')
logger.warning('this is a warning statement')
logger.error('this is a error statement')
logger.critical('this is a critical statement')

One thing to remember is that there is an order to the loglevels. If the loglevel is set to INFO then it will show all the INFO-level logs as well as all the logs that are more critical (WARNING, ERROR and CRITICAL). If you want to show everything then it's best to set the level to DEBUG.