logo

... logging.


Print statements can only do so much. You also need to log to files if you want to debug something that is in production. That is why in this series of videos we'll show you how to set up logging for your project and to also show you how it works.


Notes

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.critical('this is a critical statement')
logger.error('this is a error 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, CRITICAL and ERROR). If you want to show everything then it's best to set the level to DEBUG.


Feedback? See an issue? Something unclear? Feel free to mention it here.

If you want to be kept up to date, consider getting the newsletter.