A Quick Guide to Effective Logging in Python Using Structlog

Reading time: 58 – 96 minutes

Logging is a fundamental practice in software development for tracking and debugging applications. While Python’s built-in logging module gets the job done, Structlog takes logging to a new level by providing enhanced flexibility and customization options. In this guide, we’ll explore the basics of using Structlog for powerful and tailored logging in Python.

Installation

Start by installing Structlog using the following command:

pip install structlog

Basic Logging Setup

Structlog simplifies the process of setting up logging and offers versatile customization features. Here’s a basic example of how you can configure Structlog for logging:

import structlog
import logging
import os

level = os.environ.get("LOG_LEVEL", "INFO").upper()
LOG_LEVEL = getattr(logging, level)

structlog.configure(
    wrapper_class=structlog.make_filtering_bound_logger(LOG_LEVEL))
logger = structlog.get_logger()

logger.debug("Database connection established")
logger.info("Processing data from the API")
logger.warning("Resource usage is nearing capacity")
logger.error("Failed to save the file. Please check permissions")
logger.critical("System has encountered a critical failure. Shutting down")

Output Example:

2023-08-28T10:00:00Z [INFO] Processing data from the API
2023-08-28T10:00:00Z [WARNING] Resource usage is nearing capacity
2023-08-28T10:00:00Z [ERROR] Failed to save the file. Please check permissions
2023-08-28T10:00:00Z [CRITICAL] System has encountered a critical failure. Shutting down

Asynchronous Logging with asyncio

Structlog seamlessly supports asynchronous operations, making it compatible with asyncio:

import asyncio
import structlog

async def async_function():
    await logger.asyncinfo("Async log message")

asyncio.run(async_function())

Logging Exceptions with Tracebacks

Logging exceptions and their tracebacks is straightforward with Structlog:

import structlog

try:
    result = 1 / 0
except ZeroDivisionError:
    logger.exception("Cannot divide one by zero!")

Traceback Output Example:

{
    "event": "Cannot divide one by zero!",
    "level": "error",
    "timestamp": "2023-07-31T07:00:31.526266Z",
    "exception": [
        {
            "exc_type": "ZeroDivisionError",
            "exc_value": "division by zero",
            "syntax_error": null,
            "is_cause": false,
            "frames": [
                {
                    "filename": "/home/stanley/structlog_demo/app.py",
                    "lineno": 16,
                    "name": "<module>",
                    "line": "",
                    "locals": {
                        "__name__": "__main__",
                        "__doc__": "None",
                        "__package__": "None",
                        "__loader__": "<_frozen_importlib_external.SourceFileLoader object at 0xffffaa2f3410>",
                        "__spec__": "None",
                        "__annotations__": "{}",
                        "__builtins__": "<module 'builtins' (built-in)>",
                        "__file__": "/home/stanley/structlog_demo/app.py",
                        "__cached__": "None",
                        "structlog": "\"<module 'structlog' from '/home/stanley/structlog_demo/venv/lib/python3.11/site-\"+32",
                        "logger": "'<BoundLoggerLazyProxy(logger=None, wrapper_class=None, processors=None, context_'+55"
                    }
                }
            ]
        }
    ]
}

Conclusion

Logging plays a vital role in maintaining and troubleshooting Python applications. Structlog empowers developers with its versatile capabilities and powerful features for effective logging. By referring to the examples provided in this guide, you’ll be well-equipped to implement Structlog in your projects. For more detailed exploration, consult the official Structlog documentation.

Happy logging!

Últimas entradas

Archivo
Scroll to Top