Python Logging Context
pylogctx is a library for enriching each logs records from a context. Typical usage is for adding some request_id to all logs in order to make troubleshooting more comfortable. This context is shared by all piece of code using logging, transparently.
import logging.config
from pylogctx import context as log_context
logging.config.dictConfig({
'formatters': {'json': {
'()': 'pythonjsonlogger.jsonlogger.JsonFormatter',
'format': "%(asctime)s %(name)s %(levelname)s %(message)s",
}},
'filters': {'context': {
'()': 'pylogctx.AddContextFilter',
}},
'handlers': {'console': {
'class': 'logging.StreamHandler',
'filters': ['context'],
'formatter': 'json',
}},
'root': {
'level': 'INFO',
'handlers': ['console'],
},
})
logger = logging.getLogger(__name__)
def mycode(request, ticket_id):
# push new fields
log_context.update(requestId=uuid.uuid4())
myticket = get_object_or_404(models.Ticket, pk=ticket_id)
# push objects, they will be adapted to log fields
log_context.update(myticket):
# Log as usual
logger.info("Working on %r", myticket)
for comment in myticket.comments:
# A context manager allow to push and pop fields
with log_context(comment):
logger.info("Working on comment %r", comment)
# code, use external libs, etc.
# Don't forget to clear the context for the next request. Use the
# middleware to have it clean.
log_context.clear()
The output looks like:
{'loggerName': 'package.module', 'levelname': 'INFO', 'message': 'Working on <Ticket #1>', 'ticketId': 1, 'requestId': 'c5521138-031a-4da6-b9db-c9eda3e090f1'}
{'loggerName': 'package.module', 'levelname': 'INFO', 'message': 'Working on comment <Comment #4>', 'ticketId': 1, 'ticketCommentId': 4, 'requestId': 'c5521138-031a-4da6-b9db-c9eda3e090f1'}
{'loggerName': 'package.module', 'levelname': 'INFO', 'message': 'Working on comment <Comment #5>', 'ticketId': 1, 'ticketCommentId': 5, 'requestId': 'c5521138-031a-4da6-b9db-c9eda3e090f1'}
{'loggerName': 'package.module', 'levelname': 'INFO', 'message': 'Working on comment <Comment #78>', 'ticketId': 1, 'ticketCommentId': 78, 'requestId': 'c5521138-031a-4da6-b9db-c9eda3e090f1'}
{'loggerName': 'package.module', 'levelname': 'INFO', 'message': 'Working on comment <Comment #9>', 'ticketId': 1, 'ticketCommentId': 9, 'requestId': 'c5521138-031a-4da6-b9db-c9eda3e090f1'}
{'loggerName': 'package.module', 'levelname': 'INFO', 'message': 'Working on <Ticket #890>', 'ticketId': 890, 'requestId': 'c64aaae7-049b-4a02-929b-2d0ac9141f5c'}
{'loggerName': 'package.module', 'levelname': 'INFO', 'message': 'Working on comment <Comment #80>', 'ticketId': 890, 'ticketCommentId': 80, 'requestId': 'c64aaae7-049b-4a02-929b-2d0ac9141f5c'}
Install it with your favorite python package installer:
$ pip install pylogctx
There is a few helpers for Celery and Django projects. See USAGE for details!
Contributors
Join us to make log rocking better! Read HACKING and ask maintainers:
David STEINBERGER @dsteinberger
Étienne BERSAC @bersace
Lev Orekhov @lorehov
Malte Baden Hansen @Moulde
Vincent RIALLAND @vrialland
Hugo SMETT @hsmett
Metadata
Release files for pylogctx 1.12
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 | Actions |
|---|---|---|---|---|
| pylogctx-1.12.tar.gz | 7.6 kB | Without provenance | Details for pylogctx-1.12.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| pylogctx-1.12-py2.py3-none-any.whl | Python 3, Python 2 | none | any | Details for pylogctx-1.12-py2.py3-none-any.whl |
Total release size: 17.2 kB
Release files / pylogctx-1.12.tar.gz
| Download URL | pylogctx-1.12.tar.gz |
|---|---|
| Size | 7.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
b5e1995c3867114646f965afa9240201f67f0f10018222ca0ae5ed9d83618197
|
|
BLAKE2b-256 checksum How to use checksums |
2080c3053530878d662fd12089174fbb562919ed4e48227367d6822553fd39cb
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
Release files / pylogctx-1.12-py2.py3-none-any.whl
| Download URL | pylogctx-1.12-py2.py3-none-any.whl |
|---|---|
| Size | 9.6 kB |
| Tags | Python 2 Python 3 |
|
SHA-256 checksum How to use checksums |
23c7ddfee2e97d12c7770e1acca4ecc79a212b0deeb36979de38e0ee62878782
|
|
BLAKE2b-256 checksum How to use checksums |
dbeebab27c83669ec55276c3f1f9626eabae504b83e94a1be4c3ca11e20382cf
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
Security for pylogctx 1.12
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.