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
MQTThandler- A logging handler that publishes logs to an MQTT broker.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:
MQTTSubscriber- A logging listener that subscribes to an MQTT broker and logs messages.
Examples
MQTThandler
import logging
from paho.mqtt.client import Client
from logger_extras import MQTTConfig, MQTThandler
handler = MQTThandler(
host="localhost",
topic="logs",
append_logger_name=True,
)
handler.setFormatter(logging.Formatter("%(levelname)s - %(message)s)"))
logger = logging.getLogger(__name__)
logger.addHandler(handler)
logger.setLevel(logging.INFO)
logger.info("Hello World!")
# This will publish the following message to the MQTT broker on the topic "logs/__main__":
# {
# "timestamp": 1690226241.740354,
# "message": "INFO - Hello World!",
# "raw_message": "Hello World!",
# "level": "INFO", "name": "__main__"
# }
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
Metadata
Release files for logger-extras 0.4.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| logger_extras-0.4.2.tar.gz | 12.1 kB | With provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| logger_extras-0.4.2-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 21.3 kB
Release files / logger_extras-0.4.2.tar.gz
| Download URL | logger_extras-0.4.2.tar.gz |
|---|---|
| Size | 12.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
f29687f4f39c1686438d3a357adcc1aa2733ef2b6ced9f09d0642c9816aae4b9
|
|
BLAKE2b-256 checksum How to use checksums |
0bb9aa3e24912a1c3da3ac51852dbc4a110b3edf0be319105d054788274b9a02
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.12.8
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Feb 16, 2025.
Transparency logRelease files / logger_extras-0.4.2-py3-none-any.whl
| Download URL | logger_extras-0.4.2-py3-none-any.whl |
|---|---|
| Size | 9.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
5bc2125b84eab3d3e6f2239eb0abf8a06be5105590a09d7b6afe5fed39dfcbbf
|
|
BLAKE2b-256 checksum How to use checksums |
0aece0c4b318c44e39c2cb894d2024e5d24720ba82445c100fad2056db63ada6
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.12.8
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Feb 16, 2025.
Transparency logSecurity for logger-extras 0.4.2
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
All 2 files in this release have been uploaded with provenance metadata.
Attestation for logger_extras-0.4.2.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Feb 16, 2025.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.