Registry / observability / logzero

logzero

JSON →
library1.7.0pypypi✓ verified 22d ago

logzero is a Python library (version 1.7.0) that provides robust and effective logging for both Python 2 and 3. It simplifies logging to the console with pretty, level-specific colored output and supports logging to rotating files. The library also includes built-in JSON logging capabilities, aiming for a low-boilerplate approach to standard Python logging. Its release cadence is generally stable with occasional updates.

pip install logzero
INSTALL
IMPORT
SIG · LOGZERO
L
logzero
observabilitypythonv1.7.0
Install
1.6s avg
Import
69ms
Disk
16MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v1.7.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.072s · 17.9MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 1.6s · import 0.066s · 18MB
16MB installed
● package 16MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

logger
from logzero import logger
logzero module functions
import logzero logzero.logfile('/path/to/file.log')
from logzero import logfile # While possible, direct import of module-level functions is less common than importing 'logger' and accessing functions via 'logzero.' prefix.
Many configuration functions like `logfile`, `loglevel`, `json` are typically called as `logzero.function_name()` after `import logzero`.

This quickstart demonstrates basic console and rotating file logging, setting a global log level, and handling exceptions with `logger.exception()`.

from logzero import logger, logfile import os # Configure a logfile that rotates after 1MB, keeping 3 backups log_file_path = os.path.join(os.getcwd(), "app.log") logfile(log_file_path, maxBytes=1_000_000, backupCount=3) # Set the global minimum log level to INFO logger.setLevel(os.environ.get('LOG_LEVEL', 'INFO')) logger.debug("This is a debug message - won't show with INFO level.") logger.info("Application started successfully.") logger.warning("Disk space is getting low.") try: 1 / 0 except ZeroDivisionError as e: logger.exception(f"An error occurred: {e}")
Debug
Known issues
gotchalogzero outputs all log messages to `stderr` by default, not `stdout`. Users expecting `print()`-like behavior to `stdout` might find their logs redirected, requiring explicit redirection if they want `stdout` for other application output.
fix
Be aware that log output goes to stderr. If you need it in stdout, you might need to redirect stderr to stdout at the shell level, or configure a custom handler to explicitly output to `sys.stdout`.
affects: All versions
gotchaWhen using `logzero.logfile()`, the default parameters `maxBytes=0` and `backupCount=0` disable log file rotation. This means log files will grow indefinitely unless these parameters are explicitly configured to enable rotation.
fix
Always set `maxBytes` and `backupCount` to non-zero values when calling `logzero.logfile()` if log file rotation is desired (e.g., `logzero.logfile('/path/to/app.log', maxBytes=1_000_000, backupCount=3)`).
affects: All versions
gotchaFunctions like `logzero.loglevel()`, `logzero.logfile()`, and `logzero.formatter()` globally reconfigure the *default* `logzero.logger` instance and all its internal handlers. This can unintentionally affect logging behavior across different modules or parts of a larger application if not managed carefully.
fix
For specific logging configurations in different parts of your application, use `logzero.setup_logger(name=...)` to create isolated logger instances instead of relying solely on the global `logzero.logger` and its module-level configuration functions.
affects: All versions
gotchaIf a custom formatter (set via `logzero.formatter()`) includes ANSI color codes and is applied to file handlers without stripping them, log files may contain these escape sequences, making them harder to read in plain text editors.
fix
When using custom formatters for file logging, ensure that color codes are either removed from the format string or handled by a custom `Formatter` class that strips them for file output, or configure separate formatters for stream and file handlers.
affects: All versions
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'logzero'
The logzero library has not been installed in your Python environment or is not accessible within your current project's interpreter.
fix
Install the logzero library using pip: `pip install logzero`
AttributeError: 'Logger' object has no attribute 'WARNING'
You are attempting to call a log level method (e.g., `warning`, `info`, `debug`) using its uppercase constant name (e.g., `WARNING`, `INFO`, `DEBUG`) instead of its lowercase method name.
fix
Use the lowercase method for logging levels: `logger.warning("This is a warning")`
logzero log file not rotating / growing indefinitely
By default, `logzero.logfile()` creates a file that grows indefinitely (no rotation) unless `maxBytes` and `backupCount` parameters are explicitly set.
fix
Configure log rotation by specifying `maxBytes` and `backupCount` when calling `logzero.logfile()`: `logzero.logfile("/tmp/rotating-logfile.log", maxBytes=1e6, backupCount=3)`
logzero output to console and file simultaneously, how to disable console output?
By default, logzero logs to both the console (specifically stderr) and any configured log file. Developers often want to disable console output when logging to a file.
fix
Disable the default stderr logger when setting up the logger: `logger = logzero.setup_default_logger(disableStderrLogger=True)` or `logzero.logfile("your.log", disableStderrLogger=True)` if you are just using `logzero.logfile()`.
Upgrade
Version history
1.7.0latest on PyPI · released Mar 17, 2021
Audit
Dependencies
python-json-loggeroptionalUsed for JSON logging functionality (logzero.json() or setup_logger(json=True)).
coloramaoptionalEnables colored console output on Windows systems.
Agent activity
21 hits · last 30 days
node
18
OpenAI (training)
2
Resources