The `logging` module is a robust and flexible event logging system for Python applications and libraries, integrated into the Python Standard Library since version 2.3. It enables all Python modules to participate in logging, facilitating the integration of messages from applications and third-party modules into a unified log. The module provides extensive functionality to produce structured log messages and direct them to various destinations such as the console, files, or network sockets.
pip install loggingNo compatibility data collected yet for this library.
Verified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates both basic `basicConfig` usage for simple scripts and a more robust application-level setup using a named logger with a `FileHandler` and custom formatter. It also shows how to prevent propagation to avoid duplicate messages.
Do not install a package named 'logging' from PyPI. The standard library `logging` module is always available without installation. If encountered, uninstall the PyPI package (`pip uninstall logging`).
Always call `logging.basicConfig(level=logging.DEBUG)` (or `INFO`, etc.) at the start of your application to ensure all desired message levels are processed.
For application code, prefer `logger = logging.getLogger(__name__)` and then `logger.info(...)`. For library code, defer configuration to the application using your library, usually by adding only a `NullHandler`.
When developing a library, add `logging.getLogger(__name__).addHandler(logging.NullHandler())` to prevent emitting messages if no other configuration is present, and avoid adding any other handlers.
For async applications, consider using `logging.handlers.QueueHandler` and `logging.handlers.QueueListener` to offload actual logging I/O to a separate thread, preventing the main event loop from blocking.
Consider using structured logging (e.g., JSON output with `python-json-logger`) where messages are explicitly contained within a field, or ensure custom formatters properly escape or sanitize potentially disruptive characters from user-provided input.
Ensure that handlers are not added multiple times to a logger. If `logging.basicConfig()` is used, call it only once at the application's entry point. For custom loggers, consider setting `logger.propagate = False` if you do not want messages to be passed to parent loggers, or manually clear existing handlers using `logger.handlers.clear()` before reconfiguring.
To fix this, explicitly import the `logging.handlers` submodule: `import logging.handlers`.
Ensure `logging.basicConfig()` is called early in your application's lifecycle, preferably only once. Verify that the `level` parameter in `basicConfig` or on your specific logger/handler is set to capture the desired messages (e.g., `logging.DEBUG`). Also, check the file path and directory permissions for the log file.
You must configure at least one handler to process log messages. For a simple setup, call `logging.basicConfig()` at the start of your application. For more complex setups, create and add specific handlers (e.g., `logging.StreamHandler()` or `logging.FileHandler()`) to your logger instance using `logger.addHandler(handler)`.
No dependency data recorded yet.