Skip to main content

A collection of useful logging tools

Project description

logging_extras

A collection of useful logging tools for Python 3.

Installation

The majority of the tools in this library require no additional dependencies. To install the library with the base functionality use the following command.

pip install logging-extras

To use the MQTT* classes, install the library with the following command.

pip install logging-extras[mqtt]

Tools

  • RelativeTimeFilter - A logging filter that adds a relative time to the log record.
  • DiffTimeFilter - A logging filter that adds a time difference to the log record.
  • TieredFormatter - A logging formatter that allows for different formatting based on the log level.
  • log_function_call - A decorator that logs the arguments and return value of a function call.

Upcomming tools:

  • MQTThandler - A logging handler that publishes logs to an MQTT broker.
  • MQTTSubscriber - A logging listener that subscribes to an MQTT broker and logs messages.

Examples

RelativeTimeFilter

import logging
from time import sleep

from logger_extras import RelativeTimeFilter

handler = logging.StreamHandler()
relative_time_filter = RelativeTimeFilter()
handler.addFilter(relative_time_filter)
handler.setFormatter(logging.Formatter("%(reltime).3f - %(message)s"))

logger = logging.getLogger(__name__)
logger.addHandler(handler)
logger.setLevel(logging.INFO)

logger.info("Hello World!")
sleep(2)
logger.info("Hello World!")
relative_time_filter.reset_time_reference()
sleep(1)
logger.info("Hello World!")

# This will log the following:
# 0:00:00 - Hello World!
# 0:00:02 - Hello World!
# 0:00:01 - Hello World!

DiffTimeFilter

import logging
from time import sleep

from logger_extras import DiffTimeFilter

handler = logging.StreamHandler()
handler.addFilter(DiffTimeFilter())
handler.setFormatter(logging.Formatter("%(difftime).3f - %(message)s"))

logger = logging.getLogger(__name__)
logger.addHandler(handler)
logger.setLevel(logging.INFO)

logger.info("Hello World!")
sleep(2)
logger.info("Hello World!")
sleep(1)
logger.info("Hello World!")

# This will log the following:
# 0:00:00 - Hello World!
# 0:00:02 - Hello World!
# 0:00:01 - Hello World!

TieredFormatter

import logging

from logger_extras import TieredFormatter

handler = logging.StreamHandler()
handler.setFormatter(
    TieredFormatter(
        fmt="%(levelname)s - %(message)s",
        level_fmts={
            logging.DEBUG: "%(levelname)s - %(funcName)s:%(lineno)d - %(message)s",
            logging.INFO: "%(message)s",
        },
        datefmt=None,
        style="%",
        validate=True,
    )
)

logger = logging.getLogger(__name__)
logger.addHandler(handler)
logger.setLevel(logging.INFO)

logger.debug("Hello World!")
logger.info("Hello World!")
logger.warning("Hello World!")
logger.error("Hello World!")
logger.critical("Hello World!")

# This will log the following:
# DEBUG - __main__:19 - Hello World!
# Hello World!
# WARNING - Hello World!
# ERROR - Hello World!
# CRITICAL - Hello World!

log_function_call

from logger_extras import log_function_call

@log_function_call(level=logging.INFO)
def add(a, b):
    return a + b

_ = add(1, 2)
# This will log the following:
# 2021-08-03 12:00:00,000 - INFO - Calling __main__.add(a=1, b=2)
# 2021-08-03 12:00:00,000 - INFO - '__main__.add' returned 3

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

logger_extras-0.2.0.tar.gz (9.0 kB view details)

Uploaded Source

Built Distribution

logger_extras-0.2.0-py3-none-any.whl (7.2 kB view details)

Uploaded Python 3

File details

Details for the file logger_extras-0.2.0.tar.gz.

File metadata

  • Download URL: logger_extras-0.2.0.tar.gz
  • Upload date:
  • Size: 9.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/4.0.2 CPython/3.11.4

File hashes

Hashes for logger_extras-0.2.0.tar.gz
Algorithm Hash digest
SHA256 781c280c0b8bd701e1038d1c5da2a3bbe27088621eadba57c80e807d58390518
MD5 0196c1f130814d69eb83dafc930aee31
BLAKE2b-256 566ac5710dcb68466a103db8a8e4b8a8f415e785596cebdb74641e76688cdf90

See more details on using hashes here.

Provenance

File details

Details for the file logger_extras-0.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for logger_extras-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 7247c9f1b6c9c6c2b08ec7f881c502e8f6c370659908c014618f17187f689450
MD5 c91fc39b453dad4c9dc8198f1187bfe7
BLAKE2b-256 8b50f28ed11b323ff92c91b7ac2d31f4a440c7408421f78454d961c7c9f3a8e5

See more details on using hashes here.

Provenance

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