Loguru is a Python package that replaces most stdlib logging setup with a single import and configurable sinks for stderr, files, or custom handlers.
Overview
Loguru centers on one preconfigured logger object so scripts can start emitting leveled messages immediately, usually to stderr, without defining formatters or handlers up front. The add() method registers sinks such as functions, file paths, or streaming objects, while remove() clears defaults when you want a clean slate.
File sinks support rotation, retention policies, and compression. Messages use brace-style formatting, optional JSON serialization, and contextual bind() or patch() data for structured fields. A catch() helper routes uncaught exceptions—including in threads—to the logger with expanded tracebacks.
Sinks are thread-safe by default and can enqueue records for multiprocess or asynchronous use. Libraries are expected to call disable() rather than add() so host apps control output, while configure() suits applications. The package interoperates with the standard logging module in both directions.
Environment variables can adjust defaults such as level and color behavior, and optional companion packages integrate configuration files or notification services.
Key features
- Single import with preconfigured logger
- add() and remove() sink management
- File rotation, retention, compression
- JSON serialization and context binding
- Colored terminal output when supported
- Exception catcher with rich tracebacks
- Thread-safe and enqueueable sinks
- Standard logging interoperability
Use cases
- Quick logging in Python scripts
- Rotating local log files with compression
- Structured JSON logs for parsing pipelines
- Library code that stays quiet until enabled