Skip to main content

logzero

Build status for master branch Documentation Status Latest version on PyPi Anaconda-Server Badge

Robust and effective logging for Python 2 and 3.

Logo

Features

  • Easy logging to console and/or (rotating) file.
  • Provides a fully configured standard Python logger object.
  • No dependencies
  • Pretty formatting, including level-specific colors in the console.
  • Windows color 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 across multiple Python files and processes).
  • JSON logging support (with integrated python-json-logger)
  • Global default logger with logzero.logger and custom loggers with logzero.setup_logger(..).
  • Compatible with Python 2 and 3.
  • All contained in a single file.
  • Licensed under the MIT license.
  • Heavily inspired by the Tornado web framework.

Demo output in color

Example Usage

from logzero import logger

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

# This is how you'd log an exception
try:
    raise Exception("this is a demo exception")
except Exception as e:
    logger.exception(e)

Adding a rotating logfile is that easy:

import logzero
from logzero import logger

# Setup rotating logfile with 3 rotations, each with a maximum filesize of 1MB:
logzero.logfile("/tmp/rotating-logfile.log", maxBytes=1e6, backupCount=3)

# Log messages
logger.info("This log message goes to the console and the logfile")

Here are more examples which show how to use logfiles, custom formatters and setting a minimum loglevel:

import logging
import logzero
from logzero import logger

# This log message goes to the console
logger.debug("hello")

# Set a minimum log level
logzero.loglevel(logging.INFO)

# Set a logfile (all future log messages are also saved there)
logzero.logfile("/tmp/logfile.log")

# You can also set a different loglevel for the file handler
logzero.logfile("/tmp/logfile.log", loglevel=logging.ERROR)

# Set a rotating logfile (replaces the previous logfile handler)
logzero.logfile("/tmp/rotating-logfile.log", maxBytes=1000000, backupCount=3)

# Disable logging to a file
logzero.logfile(None)

# Set a custom formatter
formatter = logging.Formatter('%(name)s - %(asctime)-15s - %(levelname)s: %(message)s');
logzero.formatter(formatter)

# Log some variables
logger.info("var1: %s, var2: %s", var1, var2)

JSON logging

JSON logging can be enabled for the default logger with logzero.json(), or with setup_logger(json=True) for custom loggers:

>>> logzero.json()
>>> logger.info("test")
{"asctime": "2020-10-21 10:42:45,808", "filename": "<stdin>", "funcName": "<module>", "levelname": "INFO", "levelno": 20, "lineno": 1, "module": "<stdin>", "message": "test", "name": "logzero_default", "pathname": "<stdin>", "process": 76179, "processName": "MainProcess", "threadName": "MainThread"}

>>> my_logger = setup_logger(json=True)
>>> my_logger.info("test")
{"asctime": "2020-10-21 10:42:45,808", "filename": "<stdin>", "funcName": "<module>", "levelname": "INFO", "levelno": 20, "lineno": 1, "module": "<stdin>", "message": "test", "name": "logzero_default", "pathname": "<stdin>", "process": 76179, "processName": "MainProcess", "threadName": "MainThread"}

The logged JSON object has these fields:

{
  "asctime": "2020-10-21 10:43:40,765",
  "filename": "test.py",
  "funcName": "test_this",
  "levelname": "INFO",
  "levelno": 20,
  "lineno": 9,
  "module": "test",
  "message": "info",
  "name": "logzero",
  "pathname": "_tests/test.py",
  "process": 76204,
  "processName": "MainProcess",
  "threadName": "MainThread"
}```

An exception logged with `logger.exception(e)` has these:

```json
{
  "asctime": "2020-10-21 10:43:25,193",
  "filename": "test.py",
  "funcName": "test_this",
  "levelname": "ERROR",
  "levelno": 40,
  "lineno": 17,
  "module": "test",
  "message": "this is a demo exception",
  "name": "logzero",
  "pathname": "_tests/test.py",
  "process": 76192,
  "processName": "MainProcess",
  "threadName": "MainThread",
  "exc_info": "Traceback (most recent call last):\n  File \"_tests/test.py\", line 15, in test_this\n    raise Exception(\"this is a demo exception\")\nException: this is a demo exception"
}```

Take a look at the documentation for more information and examples:

* Documentation: https://logzero.readthedocs.io.


Installation
------------

Install `logzero` with [pip](https://pip.pypa.io):

```shell
$ pip install -U logzero

If you don't have pip installed, this Python installation guide can guide you through the process.

Alternatively, if you use the Anaconda distribution:

$ conda config --add channels conda-forge
$ conda install logzero

You can also install logzero from the public Github repo:

$ git clone https://github.com/metachris/logzero.git
$ cd logzero
$ python setup.py install

On openSUSE you can install the current version from repos: python2-logzero, python3-logzero. In the newest openSUSE release you can install it with zypper: sudo zypper in python2-logzero.

Development

Notes:

Getting started

# Activate virtualenv
$ python3 -m venv venv
$ . venv/bin/activate

# Install main and dev dependencies
$ pip install -e .
$ pip install -r requirements_dev.txt

# Run the tests
$ make test

# Run the linter
$ make lint

# Generate the docs (will auto-open in Chrome)
$ make docs

To do

  • CI to publish package to PyPI

Changelog

See the changelog here: https://github.com/metachris/logzero/blob/master/HISTORY.md

Feedback

All kinds of feedback and contributions are welcome.

History

1.6.0 (2020-10-29)

  • JSON logging support (PR 344)
  • Ability to easily change colors (#82)
  • Allow creating a root logger (#342)
  • Bugfix: file logging with lower loglevel than stream (PR 338)
  • Running tests with Python up to 3.9
  • Dependency updates

1.5.0 (2018-03-07)

  • logzero.syslog(..) (PR 83)

1.4.0 (2018-03-02)

  • Allow Disabling stderr Output (PR 83)

1.3.0 (2017-07-19)

  • Color output now works in Windows (supported by colorama)

1.2.1 (2017-07-09)

  • Logfiles with custom loglevels (eg. stream handler with DEBUG and file handler with ERROR).

1.2.0 (2017-07-05)

  • Way better API for configuring the default logger with logzero.loglevel(..), logzero.logfile(..), etc.
  • Built-in rotating logfile support.
import logging
import logzero
from logzero import logger

# This log message goes to the console
logger.debug("hello")

# Set a minimum log level
logzero.loglevel(logging.INFO)

# Set a logfile (all future log messages are also saved there)
logzero.logfile("/tmp/logfile.log")

# Set a rotating logfile (replaces the previous logfile handler)
logzero.logfile("/tmp/rotating-logfile.log", maxBytes=1000000, backupCount=3)

# Disable logging to a file
logzero.logfile(None)

# Set a custom formatter
formatter = logging.Formatter('%(name)s - %(asctime)-15s - %(levelname)s: %(message)s');
logzero.formatter(formatter)

# Log some variables
logger.info("var1: %s, var2: %s", var1, var2)

1.1.2 (2017-07-04)

  • Better reconfiguration of handlers, doesn't remove custom handlers anymore

1.1.0 (2017-07-03)

  • Bugfix: Disabled color logging to logfile

1.1.0 (2017-07-02)

  • Global default logger instance (logzero.logger)
  • Ability to reconfigure the default logger with (logzero.setup_default_logger(..))
  • More tests
  • More documentation

1.0.0 (2017-06-27)

  • Cleanup and documentation

0.2.0 (2017-06-12)

  • Working logzero package with code and tests

0.1.0 (2017-06-12)

  • First release on PyPI.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

logzero-1.6.2.tar.gz (91.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

logzero-1.6.2-py2.py3-none-any.whl (15.7 kB view details)

Uploaded Python 2Python 3

File details

Details for the file logzero-1.6.2.tar.gz.

File metadata

  • Download URL: logzero-1.6.2.tar.gz
  • Upload date:
  • Size: 91.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.24.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.8.6

File hashes

Hashes for logzero-1.6.2.tar.gz
Algorithm Hash digest
SHA256 7d1234dbdef8126b6ace4d15cf8a60418506355b2ad967e117d907026cf4f2b5
MD5 f9191c55fb4ad9005d7996d483f49c68
BLAKE2b-256 2ea9819ca4521f706a0315c53286216d8aa2963efa011560c2b83f2ce0f65ab5

See more details on using hashes here.

File details

Details for the file logzero-1.6.2-py2.py3-none-any.whl.

File metadata

  • Download URL: logzero-1.6.2-py2.py3-none-any.whl
  • Upload date:
  • Size: 15.7 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.24.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.8.6

File hashes

Hashes for logzero-1.6.2-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 98621ae85280633a7f6ccc4f81278e9596c9b38666d76b1637dfb5d64894e4ed
MD5 a5d74b02d819398f380bf1070980bc1f
BLAKE2b-256 0da62151b5a1fa90ba5b30b8fb7ba204651d5e03afa6bf61cb54a79ec47627b0

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page