Skip to main content

Drop-in replacement for Python's built-in `logging` module

Project description

license pypi language

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

handler = logging.StreamHandler()
handler.setFormatter(logging.ConsoleFormatter())
logging.basicConfig(level=logging.INFO, handlers=[handler])


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()
2024-08-22T02:46:38.257+09:00 INFO   main.py:9  A {}
2024-08-22T02:46:38.257+09:00 INFO   main.py:15 B {"A": "a"}
2024-08-22T02:46:38.258+09:00 INFO   main.py:21 C {"A": "a", "B": "b"}

Formatter

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": "main.py:10",
  "msg": "test",
  "ctx": "ctx",
  "thread_name": "MainThread",
  "extra": "extra"
}

ConsoleFormatter

import loggingx as logging

handler = logging.StreamHandler()
handler.setFormatter(logging.ConsoleFormatter())
logging.basicConfig(level=logging.INFO, handlers=[handler])

if __name__ == "__main__":
    with logging.addFields(ctx="ctx"):
        logging.info("test", extra={"extra": "extra"})
2024-08-22T02:48:17.868+09:00 INFO   main.py:9  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"})

orjson

If orjson is installed, this module will use orjson instead of json.

jq

alias log2jq="jq -rRC --unbuffered '. as \$line | try fromjson catch \$line' | sed 's/\\\\n/\\n/g; s/\\\\t/\\t/g'"
python3 <path> 2>&1 | log2jq

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

Uploaded Source

Built Distribution

loggingx_py-0.10.0-py3-none-any.whl (7.5 kB view details)

Uploaded Python 3

File details

Details for the file loggingx_py-0.10.0.tar.gz.

File metadata

  • Download URL: loggingx_py-0.10.0.tar.gz
  • Upload date:
  • Size: 17.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.8.18

File hashes

Hashes for loggingx_py-0.10.0.tar.gz
Algorithm Hash digest
SHA256 9f12ff58daea399d6d6f39951f2edbfd4b3ae88b671e15392745792f48b9937b
MD5 ff0ab99c1388e3b6ab9c50ae06bf8435
BLAKE2b-256 f668ac0070d93cba1309a87f5316b0f875314ab819009bea67d6c2f34a9fe9a8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for loggingx_py-0.10.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d2fae3f9367485113c397f027becdd0b10ea163c494f38c52bb196aab6f3b721
MD5 85f0beb6a48182869e3932b1b7bb2767
BLAKE2b-256 e42ea8d3f415a57b8b468f76af8f66b1086fac190ef674de08d0ce3fd2e293c8

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