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 logzeroVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates basic console and rotating file logging, setting a global log level, and handling exceptions with `logger.exception()`.
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`.
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)`).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.
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.
Install the logzero library using pip: `pip install logzero`
Use the lowercase method for logging levels: `logger.warning("This is a warning")`Configure log rotation by specifying `maxBytes` and `backupCount` when calling `logzero.logfile()`: `logzero.logfile("/tmp/rotating-logfile.log", maxBytes=1e6, backupCount=3)`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()`.