Helper for Contextual and Structured Logging in Python with logging
Project description
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"}
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
loggingx_py-0.2.0.tar.gz
(11.1 kB
view hashes)
Built Distribution
Close
Hashes for loggingx_py-0.2.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 02ba6706fb2e138849eb958f9cfaa1eb35bbe114c27b714eae03b2cce2574d4b |
|
MD5 | 41cad9469192bda4609e4ec9beb32b08 |
|
BLAKE2b-256 | ca9f8d9a60fec8ac5fafae771611fa19b87dbe6149251d6f4261cfd765676096 |