Skip to main content

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


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.1.tar.gz (39.4 kB view details)

Uploaded Source

Built Distribution

loggingx_py-0.3.1-py3-none-any.whl (5.8 kB view details)

Uploaded Python 3

File details

Details for the file loggingx-py-0.3.1.tar.gz.

File metadata

  • Download URL: loggingx-py-0.3.1.tar.gz
  • Upload date:
  • Size: 39.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.4

File hashes

Hashes for loggingx-py-0.3.1.tar.gz
Algorithm Hash digest
SHA256 31470b8b418ba97e32a097cb8915cc071063817d12fe03018c3bb51e85a1cbea
MD5 4b50a334572f794e1b068bcb42bb101f
BLAKE2b-256 11addf83e4f27a414e9ecb1359d64ac546651a3604ded3a4a20198f3847d525a

See more details on using hashes here.

File details

Details for the file loggingx_py-0.3.1-py3-none-any.whl.

File metadata

  • Download URL: loggingx_py-0.3.1-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.11.4

File hashes

Hashes for loggingx_py-0.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 510e68ff003aedfcef08367af1847f0ba6ba499e088ebfbf5761d0e5a92ccac0
MD5 c32148a878e56157abae601af601b034
BLAKE2b-256 1bce849f6fc2bca6f7038ebc3923e9a119e7c28dfb2e2219564cc6cae3dee3e6

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page