Python Logging Module Tutorial – Complete Guide

Have you ever wondered how to trace what’s happening within your code, especially when it’s not behaving as you’d expect? Or curious about how to create detailed records of a program’s operation, so errors can be identified and dealt with more efficiently? That’s where Python’s logging module comes to the rescue!

What is Python’s Logging Module?

Python’s logging module is an efficient, flexible, and robust system for handling logging in Python applications. It is a standard part of the Python library, so it’s readily available for use.

The purpose of the logging module is to provide a consistent and easy-to-use way of producing log output from Python programs. With logging, you can record important information about what the software is doing, which is incredibly helpful in debugging!

Every coder, from beginner to experienced, can benefit from knowing how to work with Python’s logging module. Not only does it allow you to catch and analyze exceptions but it also lets you log the flow of your application. Essentially, it’s like having a helpful assistant that thoroughly documents your code’s journey, making it easier to trace and resolve any unexpected behaviours.

Imagine you’re a wizard in training, and you’ve been asked to create magical spells. However, your spells have a mind of their own and do not always work as planned. To make improvements, you need to know exactly what each spell does and when it fails. Wouldn’t it be great to have a faithful quill that records everything? That’s how Python’s logging module functions in the world of coding! Let’s get started in understanding this magical quill.

CTA Small Image
FREE COURSES AT ZENVA
LEARN GAME DEVELOPMENT, PYTHON AND MORE
ACCESS FOR FREE
AVAILABLE FOR A LIMITED TIME ONLY

Basic Use of Python’s Logging Module

To start with, you have to import the logging module into your Python code.

import logging

The simplest way to log a message can be done as:

logging.warning('Beware! You have used a risky spell!')

The warning message will be outputted to the console by default. The logging levels or categories that you can use are:

logging.critical('This is a critical message')
logging.error('This is an error message')
logging.warning('This is a warning message')
logging.info('This is an information message')
logging.debug('This is a debug message')

Note that ‘warning’, ‘error’, and ‘critical’ messages will display by default, while ‘debug’ and ‘info’ messages won’t show unless you set the logging level.

Configuring the Logging Module

You can configure the logging module to better suit your needs. This can be done by using the basicConfig(**kwargs) method. Here’s an example:

logging.basicConfig(filename='magicbook.log', level=logging.DEBUG)
logging.warning('The log file will save this warning!')

This will create a log file named “magicbook.log” and will log any messages with a level ‘DEBUG’ or higher.

Log Formatting

Formatting your logs will help in making them more readable and structured. Let’s explore how:

logging.basicConfig(format='%(asctime)s - %(message)s', level=logging.INFO)
logging.info('This message will include timestamp information')

The output will display the timestamp along with the log message.

Using Handlers

In Python’s logging module, handlers send the log messages to their appropriate destination (file, console, email, etc.). This is seen in the below example:

logger = logging.getLogger()
handler = logging.StreamHandler()
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
handler.setFormatter(formatter)
logger.addHandler(handler)
logger.setLevel(logging.WARNING)

logger.error('This is an error message')

This script will log an error message with detailed information, such as timestamp, logger’s name, log level, and the log message. That’s the magic of Python’s logging module! By the end of this tutorial, you’d have learned a magical quill’s worth of logging spells.

Logging to Multiple Destinations

There may be scenarios where you want to send logs to multiple destinations, such as a console and a file. Python’s logging module handles this very smoothly!

logger = logging.getLogger('MagicLogger')
console_handler = logging.StreamHandler()
file_handler = logging.FileHandler('file.log')
logger.addHandler(console_handler)
logger.addHandler(file_handler)
logger.warning('This warning will go to both console and file')

In this example, ‘MagicLogger’ has two handlers: console and file. So, log messages will be sent to both destinations.

Using Loggers

Python’s logging module allows you to use different loggers within your application. Each logger can have a different log level, and they can independently route logs to various destinations.

logger1 = logging.getLogger('logger1')
logger2 = logging.getLogger('logger2')

logger1.warning('This is from logger1')
logger2.debug('This is from logger2')
logger2.setLevel(logging.DEBUG)
logger2.debug('Now logger2 prints debug messages too!')

Creating Custom Logging Levels

The logging module also lets you establish your own logging levels that suit your application’s specific needs.

CUSTOM_LEVEL = 25
logging.addLevelName(CUSTOM_LEVEL, 'CUSTOM')
def custom(self, message, *args, **kws):
    if self.isEnabledFor(CUSTOM_LEVEL):
        self._log(CUSTOM_LEVEL, message, args, **kws)
logging.Logger.custom = custom

logger = logging.getLogger('Test')
logger.setLevel(CUSTOM_LEVEL)
logger.custom('This is a custom level log message')

This code allows for a new custom level, named “CUSTOM”, which resides between WARNING and ERROR.

Context-specific Logging Data

Sometimes, you may need your log records to carry extra context-specific data.

extra_info = {'user': 'Hermione', 'ip':'192.168.0.1'}
logger = logging.getLogger('ContextLogger')
logger.warning('Protocol problem: %s', 'connection reset', extra=extra_info)

The resulting log message will include the information that the ‘protocol problem’ occurred for the user ‘Hermione’ from the IP ‘192.168.0.1’.

Exception Logging

Python’s logging module is also phenomenal for getting detailed information about exceptions during a program’s execution.

logger = logging.getLogger("ExceptionLogger")
try:
    magic_spell.raise_exception()
except Exception as e:
    logger.error("Failed to cast the spell.", exc_info=True)

In this case, you’ll get the error message with a complete traceback of the exception. All these powerful features make Python’s logging module an essential tool for every developer’s toolkit!

Where to Go Next?

What a powerful tool you’ve discovered in Python’s logging module! This is an essential aspect of Python programming, but there is so much more to learn and discover. The key to mastery is continuous learning.

At Zenva, we offer a comprehensive course designed to take your Python skills to the next level. We invite you to check out our Python Mini-Degree. This unique program is a thorough collection of courses that covers a broad array of Python topics including:

– Coding fundamentals
– Algorithms
– Object-Oriented Programming (OOP)
Game Development
– App Development

Through these courses, you’ll learn how to build your own games, create powerful algorithms, and develop real-world apps using Python. No matter your current level of Python knowledge, our Mini-Degree is designed to cater to various learning levels. We offer live coding lessons, quizzes, and challenges to reinforce your learning, along with flexible learning options for your convenience.

All of our courses can be accessed 24/7 and are compatible with any modern device. Our instructors, certified by reputable organizations like Unity Technologies and CompTIA, ensure high-quality instruction. Upon completing a course, you’ll receive a certificate, potentially paving your way to new job opportunities or even starting your own business.

For a wider selection of courses, please check our collection of Python Courses.

Conclusion

Armed with the magic of Python’s logging module, you now have the ability to delve deeper into your code’s behavior, understand it better, and troubleshoot any issues with remarkable ease. This powerful tool can make all the difference as you continue exploring the depths of Python programming.

Whether you’re just starting out or polishing your existing skills, we invite you to leverage the comprehensive learning resources available in our Python Mini-Degree. It’s an adventurous journey that will prepare you to weave intricate spells of code with Python. The world of coding awaits your audacious exploration, so don’t hesitate – dive right in!

Did you come across any errors in this tutorial? Please let us know by completing this form and we’ll look into it!

FREE COURSES
Python Blog Image

FINAL DAYS: Unlock coding courses in Unity, Godot, Unreal, Python and more.