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 logo

logzero is a simple and effective logging module for Python 2 and 3:

  • Easy logging to console and/or file.
  • Pretty formatting, including level-specific colors in the console.
  • Robust against str/bytes encoding problems, works well with all kinds of character encodings and special characters.
  • Compatible with Python 2 and 3.
  • All contained in a single file.
  • Licensed under the MIT license.

Usage

from logzero import setup_logger
logger = setup_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:

example output with colors

[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:

logger = setup_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):

setup_logger(level=logging.INFO)

See also the documentation of setup_logger(..) 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!