
In general, a module should emit log messages as a best practice and should not configure how those messages are handled. Still, unless you’re intentionally developing reusable modules inside your application, you’re likely using modules available from PyPI and modules that you write yourself specifically for your application. Sometimes these modules are intended to be used by other programs. NOTSET and DEBUG messages will not be included here.Ī well-organized Python application is likely composed of many modules. Setting the log level to INFO will include INFO, WARNING, ERROR, and CRITICAL messages. When you set a logging level in Python using the standard module, you’re telling the library you want to handle all events from that level up.

Logging levels are listed here in the Python documentation we’ll include them here for reference. In the following sections, we’ll review example implementations and best practices for using this module. The handler will then use a Formatter to turn the LogRecord into a string and emit that string. Internally, the message is turned into a LogRecord object and routed to a Handler object registered for this logger. While it might sound complicated, it can be as simple as this: This logger can then be used to emit simply-formatted messages at different log levels (DEBUG, INFO, ERROR, etc.), which can be used by the application to handle messages of higher priority other than those of a lower priority. The application can use the name to configure different rules for different loggers. To emit a log message, a caller first requests a named logger. This can allow for a highly flexible configuration to manage many different use cases. The module provides a way for applications to configure different log handlers and a way of routing log messages to these handlers.


This module is widely used by libraries and is often the first go-to point for most developers when it comes to logging. Python comes with a logging module in the standard library that can provide a flexible framework for emitting log messages from Python programs. After reading this, you should be able to easily integrate logging into your Python application. This article covers the basics of using the standard logging module that ships with all Python distributions. Analyzing and Troubleshooting Python Logs.Python Logging Libraries and Frameworks.
