Skip to main content

Simple Logging

PyPI PyPI Travis ReadTheDocs Code style: Black Downloads Test Coverage 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.

Changelog

0.12.0 (2024-07-02)

  • Drop Python 3.7 support
  • Switch to Poetry core as a build engine

0.11.0 (2020-08-31)

  • Use poetry

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.

Metadata

Release files for simplelogging 0.12.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for simplelogging 0.12.0
File Size Uploaded
simplelogging-0.12.0.tar.gz 4.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for simplelogging 0.12.0
File Interpreter ABI Platform
simplelogging-0.12.0-py3-none-any.whl Python 3 none any Details

Total release size: 10.2 kB

Release files / simplelogging-0.12.0.tar.gz

Download URL simplelogging-0.12.0.tar.gz
Size 4.9 kB
Tags Source
SHA-256 checksum
How to use checksums
a9695b05090951c029733606f14e24a783c7a100bbe96f8f16a5f2c4c9b08102
BLAKE2b-256 checksum
How to use checksums
3071d6b6513598e6a4300f6142a839d1b0e10a25a4707f1b586d9a6331be91bb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.11.9

Release files / simplelogging-0.12.0-py3-none-any.whl

Download URL simplelogging-0.12.0-py3-none-any.whl
Size 5.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
bca5628e427d32d3e6ef48e1ef1ff85b6af6ea0710037cc5dd5a9c9bf70df4a0
BLAKE2b-256 checksum
How to use checksums
0ae8e1c4a2bc2a3ee645fb82e4420c46f90f5780ddbe12a61df3247cb4e9c063
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.11.9

Release history Release notifications | RSS feed

This release

0.12.0 This release

2 release files

0.11.0

2 release files

0.10.0

2 release files

0.9.0

2 release files

0.8.0

2 release files

0.7.0

2 release files

0.6.0

2 release files

0.5.0

2 release files

0.4.0

2 release files

0.3.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page