logzero - Simplified logging for Python 2 and 3
I’ve just published logzero
, a small Python package which simplifies logging with Python 2 and 3.
It is easy to use and robust, and heavily inspired by the Tornado web framework. I’ve recently released python-boilerplate.com which included this module as a file, and people have been asking for it to be published as a standalone package. Finally I’ve found some time to do it, and here it is!
logzero
is a simple and effective logging module for Python 2 and 3:
- Easy logging to console and/or file.
- Provides a fully configured standard Python logger object.
- Pretty formatting, including level-specific colors in the console.
- Robust against str/bytes encoding problems, works with all kinds of character encodings and special characters.
- Multiple loggers can write to the same logfile (also across multiple Python files).
- Global default logger with
logzero.logger
and custom loggers withlogzero.setup_logger(..)
. - Compatible with Python 2 and 3.
- All contained in a single file.
- No further Python dependencies.
- Licensed under the MIT license.
- Heavily inspired by the Tornado web framework.
Usage
from logzero import logger
logger.debug("hello")
logger.info("info")
logger.warn("warn")
logger.error("error")
If logger.info(..)
was called from a file called demo.py
, the output will look like this:
[D 170628 09:30:53 demo:4] hello
[I 170628 09:30:53 demo:5] info
[W 170628 09:30:53 demo:6] warn
[E 170628 09:30:53 demo:7] error
You can also easily log to a file as well:
logzero.setup_default_logger(logfile="/tmp/test.log")
This is how you can log variables too:
logger.debug("var1: %s, var2: %s", var1, var2)
This is how you can set the minimum logging level (default is DEBUG
):
logzero.setup_default_logger(level=logging.INFO)
You can also easily set up rotating log files, custom logger, custom formatter and much more. Just take a look at the documentation for more details and options.
Installation
Install logzero
with pip
:
$ pip install -U logzero
If you don’t have pip
installed, this Python installation guide can guide
you through the process. Alternatively you can also install logzero
from the Github repository with python setup.py install
.
Take a look at the full documentation: https://logzero.readthedocs.io
If you have comments, feedback or suggestions, I’m happy to hear from you. Please reach out via @metachris!