Skip to main content

Python Logging Context

Imports: isort Code style: black

1. Description

import logging
from logging_context import get_logging_context, setup_logging_context
from logging_context.formatter import LoggingContextFormatter

logger = logging.getLogger("your_logger")
logger.setLevel(logging.INFO) 

handler = logging.StreamHandler()
handler.setFormatter(LoggingContextFormatter("%(asctime)s - %(name)s - %(levelname)s - context=%(context)s - %(message)s"))
handler.setLevel(logging.INFO)

logger.addHandler(handler)

logger.info("before setup")
# 2021-11-30 16:49:52,060 - your_logger - INFO - context=None - before setup
context = get_logging_context()
setup_logging_context(context)
logger.info("after setup")
# 2021-11-30 16:50:12,916 - your_logger - INFO - context={} - after setup
context.set_value("var", 100)
logger.info("after set value")
# 2021-11-30 16:50:36,562 - your_logger - INFO - context={"var": 100} - after set value
context.set_value("var", 200)
logger.info("after change value")
# 2021-11-30 16:50:53,912 - your_logger - INFO - context={"var": 200} - after change value
context.delete_value("var")
logger.info("after delete value")
# 2021-11-30 16:51:18,369 - your_logger - INFO - context={} - after delete value

2. Installation

pip install logging-context

3. Usage

3.1 Setup logging record factory

Function setup_logging_context will add a record.context attribute into your log record.

If you don't run the function, logging can't found any attribute context and raise KeyError exception.

from logging_context import get_logging_context, setup_logging_context
context = get_logging_context()
setup_logging_context(context)

3.2 Setup logging format

Function setup_logging_context will add a record.context attribute into your log record.
You can add %(context)s into your log format to show entire context values in your log
You should use LoggingContextFormatter instead of default logging.Formatter. LoggingContextFormatter added record.context to your log record by default.

context = get_logging_context()
context.set_value("var1", 200)
context.set_value("var2", "var2 value")

handler = logging.StreamHandler()
handler.setFormatter(LoggingContextFormatter("%(asctime)s - %(name)s - %(levelname)s - context=%(context)s - %(message)s"))
logger.addHandler(handler)

logger.info("log message")
# 2021-11-30 17:08:14,263 - your_logger - INFO - context={"var1": 200, "var2": "var2 value"} - log message

3.3 Context object

Wherever you want to use context, you should call function get_logging_context to get current context

from logging_context import get_logging_context
context = get_logging_context()

You can set/update/delete any context value and you can clear any values of your context

logger.info("before set value")
# 2021-11-30 17:12:40,413 - your_logger - INFO - context={} - before set value
context.set_value("var1", 200)
context.set_value("var2", "var2 value")
logger.info("after set value")
# 2021-11-30 17:13:09,508 - your_logger - INFO - context={"var1": 200, "var2": "var2 value"} - after set value
context.set_value("var1", 100)
logger.info("after change value")
# 2021-11-30 17:14:20,317 - your_logger - INFO - context={"var1": 100, "var2": "var2 value"} - after change value
context.delete_value("var2")
logger.info("after delete value")
# 2021-11-30 17:14:52,383 - your_logger - INFO - context={"var1": 100} - after delete value

context.set_value("var1", 150)
context.set_value("var2", "var2 value")
# 2021-11-30 17:16:05,623 - your_logger - INFO - context={"var1": 150, "var2": "var2 value"} - before clean
context.clean()
logger.info("after clean")
# 2021-11-30 17:16:45,567 - your_logger - INFO - context={} - after clean

Development

Clone this project and run following commands to setup environment

cd logging_context
make virtualenv
source .venv/bin/activate
make install

Metadata

Release files for logging-context 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for logging-context 0.1.0
File Size Uploaded
logging-context-0.1.0.tar.gz 4.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for logging-context 0.1.0
File Interpreter ABI Platform
logging_context-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 10.7 kB

Release files / logging-context-0.1.0.tar.gz

Download URL logging-context-0.1.0.tar.gz
Size 4.7 kB
Tags Source
SHA-256 checksum
How to use checksums
dd8e0c9fbe7cea5f1e4e258ab9a95fb51da0a3f6b680eb57c3a46dd131f51ffb
BLAKE2b-256 checksum
How to use checksums
46ee98e203b24aceb831c823db99e628f0c9eca0a33f6d2daad448d24949e56b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.6.0 importlib_metadata/4.8.2 pkginfo/1.8.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.6.15

Release files / logging_context-0.1.0-py3-none-any.whl

Download URL logging_context-0.1.0-py3-none-any.whl
Size 6.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b933cfee984505b07ce66ea0398b54debde7f8d8f4ce593d4124e477d337427d
BLAKE2b-256 checksum
How to use checksums
c980af43c93a050941c9a8349ba6af56b73561c61a9cecd8e91563c887016fac
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.6.0 importlib_metadata/4.8.2 pkginfo/1.8.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.6.15

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page