Adding context to log records
Project description
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
Do you want py3 support or other nice improvements ? Join us to make log rocking better!
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
pylogctx-1.8.tar.gz
(7.2 kB
view details)
Built Distribution
File details
Details for the file pylogctx-1.8.tar.gz
.
File metadata
- Download URL: pylogctx-1.8.tar.gz
- Upload date:
- Size: 7.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 10c56e548d4faceace81b4cf00787e9629dbcf586646f1f94781d0fee2c70e93 |
|
MD5 | c26216253b84f18219c3a7d46604b362 |
|
BLAKE2b-256 | b3b0f1060d9a55d854e8b31aeaa1662db183846f724a965d0f6f9dd41b3b822b |
File details
Details for the file pylogctx-1.8-py2.py3-none-any.whl
.
File metadata
- Download URL: pylogctx-1.8-py2.py3-none-any.whl
- Upload date:
- Size: 8.9 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 533415b23f0e4b14bdf6ca48e9ddc8f6eefa93344bfa648f594c50ad94d6cd6a |
|
MD5 | 53a2e7bd31b09079de1148183c52621a |
|
BLAKE2b-256 | 0f013ae4cc173bdec6426ad5fdc5e00c6955722774f900ac7a0a56fd6a4939bd |