Skip to main content

Logging made simple, no excuse for any debug print call.

Project description

Simple Logging

PyPI PyPI Travis ReadTheDocs Code style: Black Downloads Coverage Status Maintainability

Logging made simple, no excuse for any debug print call.

Features

  • Logging management (debug, information or error messages)
  • Simple logging setup
  • Based on Python logging module of the standard library
  • Based on colorlog for colored log on console

For advanced users:

  • The provided logger is one of those from logging, this means it can be configured so that log messages are sent by email, HTTP, or any of the options available in https://docs.python.org/3/library/logging.handlers.html.
  • The StreamHandler and the associated Formatter are those provided by colorlog.

Example

Basic usage

import simplelogging

# log = simplelogging.get_logger(console_level=simplelogging.DEBUG)
# log = simplelogging.get_logger(file_name="log.txt")
log = simplelogging.get_logger()

a_string_variable = "hello"
an_integer_variable = 42
a_floating_point_variable = 3.14

log.debug("some debug")
log.info("some info")
log.info(
    "some variables: %s, %d, %f",
    a_string_variable,
    an_integer_variable,
    a_floating_point_variable,
)
log.warning("some warning")
log.error("some error")
log.critical("some critical error")

try:
    x = 1 / 0
except ZeroDivisionError as error:
    log.exception(error)

quickstart result

Keep in mind that you shouldn't do string formatting yourself. Delegate formatting to simplelogging (i.e. logging in this case), the formatting will be done only if necessary, that is if the message is going to be displayed. See above examples of how to display variables.

Usage with modules

example_module.py

import simplelogging

log = simplelogging.get_logger()


def log_some_messages():
    log.debug("## some debug ##")
    log.info("## some info ##")
    log.warning("## some warning ##")
    log.error("## some error ##")

main.py

import example_module
import simplelogging

# log = simplelogging.get_logger(console_level=simplelogging.DEBUG)
# log = simplelogging.get_logger(file_name="log.txt")
log = simplelogging.get_logger()

a_variable = "a nice variable"
another_variable = 42

log.error("---- normal logging ----")
log.debug("a debug message")
log.info("an info")
log.warning("a warning")
log.error("%s and %d", a_variable, another_variable)

log.error("---- example_module writes to the log ----")
example_module.log_some_messages()

log.error("---- reduced logging (bye debug and info messages) ----")
log.reduced_logging()
log.debug("a debug message")
log.info("an info")
log.warning("a warning")
log.error("an error")

log.error("---- full logging (welcome back debug and info messages) ----")
log.full_logging()
log.debug("a debug message")
log.info("an info")
log.warning("a warning")
log.error("an error")

Result in the console

quickstart with modules result

More examples are provided in the documentation: https://simplelogging.readthedocs.io.

TODO

  • add tests
  • add type annotations
  • add docstring
  • commit hooks
  • describe pros/cons and alternatives
  • release 1.0!

Credits

This package is an extension of the logging package in the Python standard library. Coloring of the console relies on colorlog.

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

History

0.10.0 (2019-09-16)

  • setup.py: require pytest-runner only when necessary
  • remove Python 3.4 support

0.9.0 (2018-12-14)

  • Improve documentation
  • Add tests
  • Change API for easy logging level change

0.8.0 (2018-12-09)

  • Improve documentation
  • Change default format: enlarge level size for critical errors

0.7.0 (2018-12-08)

  • Fix logging to file

0.6.0 (2018-12-07)

  • Colored output on console
  • Improved documentation

0.5.0 (2018-12-02)

  • Fix README rendering in PyPI

0.4.0 (2018-12-02)

  • Fix bump config

0.3.0 (2018-12-02)

  • First release on PyPI.

Project details


Download files

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

Source Distribution

simplelogging-0.10.0.tar.gz (135.5 kB view details)

Uploaded Source

Built Distribution

simplelogging-0.10.0-py2.py3-none-any.whl (5.6 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file simplelogging-0.10.0.tar.gz.

File metadata

  • Download URL: simplelogging-0.10.0.tar.gz
  • Upload date:
  • Size: 135.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.20.1 setuptools/39.0.1 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.6.8

File hashes

Hashes for simplelogging-0.10.0.tar.gz
Algorithm Hash digest
SHA256 c070e0d82b68eff6918076a0edbf0a48b2c9a263854bd35f48a12c6adbbe9d80
MD5 e3f9da393098d8fd8ebd8349a00692f2
BLAKE2b-256 17853d2431f971e703916c7254e4560ed15451faedf2461eb484da9e1ebc5da6

See more details on using hashes here.

File details

Details for the file simplelogging-0.10.0-py2.py3-none-any.whl.

File metadata

  • Download URL: simplelogging-0.10.0-py2.py3-none-any.whl
  • Upload date:
  • Size: 5.6 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.20.1 setuptools/39.0.1 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.6.8

File hashes

Hashes for simplelogging-0.10.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 574e8fdaaecdc43a4cb932710126657b1c3c566594d22f2e04de1a6b56e56d9d
MD5 6ccfa39499f3ceab49cc099fcc3ce6a7
BLAKE2b-256 6300e173efe1df19b62a599b2c543245fc01976ce7cdc2c32f6d16c1288b8fd6

See more details on using hashes here.

Supported by

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