Helper for Contextual and Structured Logging in Python with logging
Project description
loggingx.py
loggingx is a drop-in replacement for Python's built-in logging module. Even better, once you've imported loggingx, you don't need to modify your existing logging module.
python3 -m pip install loggingx-py
Additional Format
| Attribute name | Format | Description |
|---|---|---|
| caller | %(caller)s | Caller(<pathname>:<lineno>) |
| ctxFields | %(ctxFields)s | Context fields |
Context
import loggingx
loggingx.basicConfig(
level=loggingx.INFO,
format="%(asctime)s\t%(levelname)s\t%(caller)s\t%(message)s\t%(ctxFields)s",
)
def A() -> None:
loggingx.info("A")
with loggingx.addFields(A="a"):
B()
def B() -> None:
loggingx.info("B")
with loggingx.addFields(B="b"):
C()
def C() -> None:
loggingx.info("C")
if __name__ == "__main__":
A()
2023-07-19 01:15:33,981 INFO loggingx.py/main.py:10 A {}
2023-07-19 01:15:33,981 INFO loggingx.py/main.py:16 B {'A': 'a'}
2023-07-19 01:15:33,982 INFO loggingx.py/main.py:22 C {'A': 'a', 'B': 'b'}
JSONFormatter
import loggingx
handler = loggingx.StreamHandler()
handler.setFormatter(loggingx.JSONFormatter())
loggingx.basicConfig(level=loggingx.INFO, handlers=[handler])
if __name__ == "__main__":
with loggingx.addFields(ctx="ctx"):
loggingx.info("test", extra={"extra": "extra"})
{"time": 1689697694.9980711, "level": "info", "caller": "loggingx.py/main.py:9", "msg": "test", "ctx": "ctx", "extra": "extra"}
With logging
import logging
import loggingx
# handler = loggingx.StreamHandler()
handler = logging.StreamHandler()
handler.setFormatter(loggingx.JSONFormatter())
# loggingx.basicConfig(level=loggingx.INFO, handlers=[handler])
logging.basicConfig(level=logging.INFO, handlers=[handler])
if __name__ == "__main__":
with loggingx.addFields(ctx="ctx"):
# loggingx.info("test", extra={"extra": "extra"})
logging.info("test", extra={"extra": "extra"})
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
loggingx-py-0.3.0.tar.gz
(25.1 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file loggingx-py-0.3.0.tar.gz.
File metadata
- Download URL: loggingx-py-0.3.0.tar.gz
- Upload date:
- Size: 25.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5ed929d51fb10742607b69c41fff621d7616a059e91b5480227b30d811ef8623
|
|
| MD5 |
e6986519364c07496dc7721fb2d95974
|
|
| BLAKE2b-256 |
db1e766bacbfb6038c33c203e31931c8aed894ea0857ba657c572d6cde7917c2
|
File details
Details for the file loggingx_py-0.3.0-py3-none-any.whl.
File metadata
- Download URL: loggingx_py-0.3.0-py3-none-any.whl
- Upload date:
- Size: 5.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2e8a5df8ab18c27f6b7c428dd254af37ccd05c789370f04a9e82bc4d23fc5b5c
|
|
| MD5 |
742257d536ca50d1a6b478a1ae85f687
|
|
| BLAKE2b-256 |
b0f97d49713d2b2605e3570b322b43d437b53798326fd4584b8ed0470b163166
|