/ python

Log all teh data!

Log files, sys admins LOVE them.

They can be used to look busy, or you can be busy looking through them for data.

I remember setting an Asterisk server to show me very verbose data once, just so the boss would leave me alone to work on a project

But what if you are writing your own Python project and you want to create a log file? Well I've just had that experience and I found a great Python library that does the job.

Everything is zero these days!

LogZero is a no-nonsense Python library that offers...

  • Easy logging to console and/or (rotating) file.
  • Provides a fully configured standard Python logger object.
  • Pretty formatting, including level-specific colors in the console.
  • Windows colour output supported by colorama
  • 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 works across multiple Python files).
    *Global default logger with logzero.logger and custom loggers with logzero.setup_logger(..).
  • Compatible with Python 2 and 3.

The output to the Python shell is colour coded handy for live information.

  • Debug = Blue
  • Info = Green
  • Warning = Yellow
  • Error = Red

The output can also be sent to a logfile and is formatted as so.

[I 170824 17:19:23 door-security:17] 572050203913
[I 170824 17:19:23 door-security:18] The user is Sarah Rogers
[I 170824 17:19:29 door-security:17] 572050203913
[I 170824 17:19:29 door-security:18] The user is Sarah Rogers
[I 170824 17:26:24 door-security:17] 572050203913
[I 170824 17:26:24 door-security:18] The user is Sarah Rogers
[E 170824 17:35:29 door-security:45] 166790669609
[E 170824 17:35:32 door-security:45] 166790669609
[E 170824 17:35:33 door-security:45] 166790669609
[E 170824 17:45:59 door-security:44] 166790669609
[E 170824 17:45:59 door-security:44] 166790669609
[I 170824 17:46:04 door-security:16] 572050203913
[I 170824 17:46:04 door-security:17] The user is Sarah Rogers
[I 170824 17:46:16 door-security:16] 572050203913
[I 170824 17:46:16 door-security:17] The user is Sarah Rogers

How do I use it?

First install it!

sudo pip3 install logzero

It's simple to use, open your favourite Python editor and type the following.

import logzero
from logzero import logger

logzero.logfile("access.log", maxBytes=1e6, backupCount=3)

# Log messages
logger.debug("There is a bug here, doh!")
logger.info("Les loves Greggs")
logger.warn("Greggs has run out of sausage rolls")
logger.error("Les has no money for Greggs")

So what does this do? Well it imports the logzero library, then creates a log file called access.log in the same directory as the Python code. The file size is set to a maximum of 1MB (1e6) and there are up to three backups made of the file before it rotates and destroys the originals.

We then use the debug,info,warn and error functions to write data to the log. These will be printed to the Python shell, with pretty colours, and they will be written to the log file. Which you can open in your favourite text editor.

Give it a try!

If you need to write data to a log using Python, then give this a go. I love it! I used it with a Raspberry Pi project, and it installed in seconds!

The project has a github page and Readthedocs documentation