Skip to content

A Python util for automatically adding extra log fields to logs

License

Notifications You must be signed in to change notification settings

deansg/py-logging-context

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

py-logging-context

A Python util for automatically adding extra log fields to logs.

Installation

This project is published to PyPI and can be installed using:

pip install py-logging-context==0.1.1

Example Usage

import logging
from py_logging_context import LoggingContext, LoggingContextInjectingFilter

logger = logging.getLogger()
logger.addFilter(LoggingContextInjectingFilter())

with LoggingContext(request_id="some-id", user_name="John Doe"):
    # The output log record will include `request_id` and `user_name` fields, which can be printed by formatters such 
    # as python-json-logger's JsonFormatter (see https://github.com/madzak/python-json-logger).
    logger.info("Hello world!")  

This library is implemented using Python's Context Variables, and therefore works in multithreaded settings as well as with asyncio. This is in contrast to solutions based on thread-local data, which will not work as intended with async functions (such as log-with-context).

This is a lightweight library and has no additional dependencies.

About

A Python util for automatically adding extra log fields to logs

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages