Skip to main content

logzero

https://img.shields.io/pypi/v/logzero.svg https://img.shields.io/travis/metachris/logzero.svg Documentation Status Updates

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.

  • 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 with logzero.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.

Demo output in color

Example Usage

from logzero import logger

logger.debug("hello")
logger.info("info")
logger.warn("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)

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)

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

Installation

Install logzero with pip:

$ pip install -U logzero

Changelog

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

Future Features & Ideas

TODO

  • Tests

    • Custom handlers and reconfiguration

    • Strange behaviour: py.test with default logger - capturing err does not work if the logger is setup initially in logzero. Only works when setup from the py script.

Notes: How to release a new version

via https://cookiecutter-pypackage.readthedocs.io/en/latest/pypi_release_checklist.html

# Run the tests
py.test
make lint

# Update history
vi HISTORY.rst
git add HISTORY.rst
git commit -m "Changelog for upcoming release 0.1.1."

# Update version
bumpversion minor

# Push
git push && git push --tags

Credits

This package was created with Cookiecutter and the audreyr/cookiecutter-pypackage project template.

History

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.2.1.tar.gz (169.9 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.2.1-py2.py3-none-any.whl (12.4 kB view details)

Uploaded Python 2Python 3

File details

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

File metadata

  • Download URL: logzero-1.2.1.tar.gz
  • Upload date:
  • Size: 169.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for logzero-1.2.1.tar.gz
Algorithm Hash digest
SHA256 f2444e545201af879a4e26f8b58f2927b81a1656dd0aca5d4cb2c24026793fd7
MD5 98b379a63f3ca27171d723b7c1762a06
BLAKE2b-256 b14f27c355797b69581491a0eb638bce09fd57f849088c38c7cf6c3a1e3b0ecb

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for logzero-1.2.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 0fc8c4a1dac15d51f5ec2a32e07a842ca8972d47b3063ac8c1eae910e9d5e34c
MD5 676b2d5968e63b522337634d49121987
BLAKE2b-256 223c012524db2842e40ddd9de9cea7197a50374a64dadb844dbe595160b3aaf8

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