Use logzero for simple logging in Python

A quick primer on the handy log library that can help you master this important programming concept.
140 readers like this.
Raspberry Pi and Python

Raspberry Pi Foundation. CC BY-SA 4.0.

The logzero library makes logging as easy as a print statement, which is quite a feat of simplicity. I'm not sure whether logzero took its name to fit in with the series of "zero boilerplate" libraries like pygame-zero, GPIO Zero, and guizero, but it's certainly in that category. It's a Python library that makes logging straightforward.

You can just use its basic logging to stdout the same way you might use print for information and debugging purposes, and it has a smooth learning curve towards more advanced logging, like logging to a file.

To start, install logzero with pip:

$ sudo pip3 install logzero

Now in a Python file, import logger and try one or all of these logging examples:

from logzero import logger

logger.debug("hello")
logger.info("info")
logger.warning("warning")
logger.error("error")

The output is automatically colored in an easy-to-read way:

Python, Raspberry Pi: import logger

So now, instead of using print to figure out what's going on, use logger instead, with the relevant log level.

Writing logs to a file in Python

If you only read this far and make that one change in the way you write code, that's good enough for me. If you want to go further, read on!

Writing to stdout is fun for testing a new program, but it is only useful if you are logged into the computer where the script is running. Many times when using an application you'll want to execute the code remotely and review errors after the fact. That's when it's helpful to log to a file instead. Let's try it:

from logzero import logger, logfile

logfile('/home/pi/test.log')

Now your log entries will be logged into the file test.log. Remember to make sure that the script has permission to write to that file and its directory structure.

You can specify some more options too:

logfile(’/home/pi/test.log’, maxBytes=1e6, backupCount=3)

Now when the file provided to logfile reaches 1MB (1×106 bytes), it will rotate entries through test.log.1, test.log.2, and so on. This behavior is nice to avoid generating a massive log file that is I/O intensive for the system to open and close. You might also want to log to /var/log like a pro. Assuming you're on Linux, you a directory and make your user the owner so they can write to it:

$ sudo mkdir /var/log/test
$ sudo chown pi /var/log/test

Then in your Python code, change the logfile path:

logfile(’/var/log/test/test.log’, maxBytes=1e6, backupCount=3)

When it comes to catching exceptions in your logfile, you can either use logging.exception:

try:
    c = a / b
except Exception as e:
    logger.exception(e)

This will produce the following (in the case that b is zero):

[E 190422 23:41:59 test:9] division by zero
     Traceback (most recent call last):
       File "test.py", line 7, in 
         c = a / b
     ZeroDivisionError: division by zero

You get the log entry, followed by the full traceback. Alternatively, you could use logging.error and hide the traceback:

try:
    c = a / b
except Exception as e:
    logger.error(f"{e.__class__.__name__}: {e}")

Now this will produce the more succinct:

[E 190423 00:04:16 test:9] ZeroDivisionError: division by zero

Logging output

There are plenty more options which you can read in the docs at logzero.readthedocs.io.

logzero shines for education

Logging can be a challenging concept for a new programmer. Most frameworks depend on flow control and lots of variable manipulation to make a meaningful log, but logzero is different. With its syntax being similar to a print statement, it is a big win for education as it saves from explaining another concept. Give it a try on your next project.

--

This article was originally written on my blog and is republished with permission.

What to read next
Tags
User profile image.
Ben is a software engineer for BBC News Labs, and formerly Raspberry Pi's Community Manager. He's into Linux, Python and all things open source! Follow Ben on Twitter @ben_nuttall.

3 Comments

Excellent overview and very simple to implement!

"Writing to stdout is fun for testing a new program, but it is only useful if you are logged into the computer where the script is running."

Thanks to the systemd journal, this isn't (on most distros) entirely true! If your program (or a program using your module) is run as a systemd service, anything it prints to stdout gets logged to the journal. So for simple 'real' logging in this situation, probably logzero's basic mode would be enough.

I just tried this. The ANSI-colored errors are cool to look at. I'm not sure how to define custom colors though.

Creative Commons LicenseThis work is licensed under a Creative Commons Attribution-Share Alike 4.0 International License.