Skip to main content

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

Optimization

Configuration Description
logging.logThreads If False, Record will not collect thread and threadName.
logging.logProcesses If False, Record will not collect process.
logging.logMultiprocessing If False, Record will not collect processName.

Context

import loggingx as logging

logging.basicConfig(
    level=logging.INFO,
    format="%(asctime)s\t%(levelname)s\t%(caller)s\t%(message)s\t%(ctxFields)s",
)


def A() -> None:
    logging.info("A")
    with logging.addFields(A="a"):
        B()


def B() -> None:
    logging.info("B")
    with logging.addFields(B="b"):
        C()


def C() -> None:
    logging.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 as logging

handler = logging.StreamHandler()
# handler.setFormatter(logging.JSONFormatter())
handler.setFormatter(logging.JSONFormatter(logging.Information.THREAD_NAME))
logging.basicConfig(level=logging.INFO, handlers=[handler])

if __name__ == "__main__":
    with logging.addFields(ctx="ctx"):
        logging.info("test", extra={"extra": "extra"})
{"time": 1689697694.9980711, "level": "info", "caller": "loggingx.py/main.py:9", "msg": "test", "ctx": "ctx", "thread_name": "MainThread", "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"})

jq

alias log2jq="jq -R -r '. as \$line | try fromjson catch \$line'"
python3 <path> 2>&1 | log2jq

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

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

loggingx_py-0.6.0-py3-none-any.whl (6.6 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for loggingx-py-0.6.0.tar.gz
Algorithm Hash digest
SHA256 9d65f677374d0e238d0693c0ac2e13f0ad27e6da77f6b3e054065326d4b620c6
MD5 97cecff3b6a46a3582427b7825f14add
BLAKE2b-256 ed9e92bd40bf851febba417f9a34b9a1e220e98b8f50493c58e7e664deac3e6a

See more details on using hashes here.

File details

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

File metadata

  • Download URL: loggingx_py-0.6.0-py3-none-any.whl
  • Upload date:
  • Size: 6.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.12.0

File hashes

Hashes for loggingx_py-0.6.0-py3-none-any.whl
Algorithm Hash digest
SHA256 6f538d4b478112f7399279459013e3badc425b6b2f555038c28c337850d2bbe6
MD5 ffa0e1819ccfbdd3b9eb3badd70f9866
BLAKE2b-256 6b5df13233c7ca903681886cd97b98ccc9c3f419827ad5da08f2d36b0cdca36e

See more details on using hashes here.

Supported by

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