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
Metadata
Release files for loggingx-py 0.12.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| loggingx_py-0.12.0.tar.gz | 19.5 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| loggingx_py-0.12.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 27.5 kB
Release files / loggingx_py-0.12.0.tar.gz
| Download URL | loggingx_py-0.12.0.tar.gz |
|---|---|
| Size | 19.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
cf7378d9bb305a018cdb1433027ba6f951b26492d107c2a91eb77459a95e30ac
|
|
BLAKE2b-256 checksum How to use checksums |
cf84e575e1b976b82fd2a8792b296440d6b6f7e8d1b6f6c3438b9207b475c52b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.1.1 CPython/3.8.18
|
Release files / loggingx_py-0.12.0-py3-none-any.whl
| Download URL | loggingx_py-0.12.0-py3-none-any.whl |
|---|---|
| Size | 8.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
fb9b8b8b4e9fbf8c202a6881e808ef1a57613178ab62da0fec7192e6b21ece47
|
|
BLAKE2b-256 checksum How to use checksums |
3bd30b99204a3b6fcac86382f648402699e86401a2b56e7beb7ee7b08daa0724
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.1.1 CPython/3.8.18
|
Security for loggingx-py 0.12.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.