Skip to main content

log-with-context is a Python logger that saves variables in a thread-local context to be passed as extra to Python logging methods.

Installation

This library is available on PyPI and can be installed with

python3 -m pip install log-with-context

Usage

This library provides a wrapped Python logging.Logger that adds a shared context to each logging message, passed as the extra parameter.

You will need an additional library (like JSON-log-formatter) to actually output the logging messages. We avoided putting this functionality in this library to keep it lightweight and flexible. We assumed that you already have a preferred way to format your logging messages.

import logging
import logging.config

from log_with_context import add_logging_context, Logger

logging.config.dictConfig({
    "version": 1,
    "disable_existing_loggers": True,
    "formatters": {
        "json": {"()": "json_log_formatter.JSONFormatter"},
    },
    "handlers": {
        "console": {
            "formatter": "json",
            "class": "logging.StreamHandler",
        }
    },
    "loggers": {
        "": {"handlers": ["console"], "level": "INFO"},
    },
})

LOGGER = Logger(__name__)

LOGGER.info("First message. No context")

with add_logging_context(current_request="hi"):
    LOGGER.info("Level 1")

    with add_logging_context(more_info="this"):
        LOGGER.warning("Level 2")

    LOGGER.info("Back to level 1")

LOGGER.error("No context at all...")

The above program logs the following messages to standard error:

{"message": "First message. No context", "time": "2021-04-08T16:37:23.126099"}
{"current_request": "hi", "message": "Level 1", "time": "2021-04-08T16:37:23.126336"}
{"current_request": "hi", "more_info": "this", "message": "Level 2", "time": "2021-04-08T16:37:23.126389"}
{"current_request": "hi", "message": "Back to level 1", "time": "2021-04-08T16:37:23.126457"}
{"message": "No context at all...", "time": "2021-04-08T16:37:23.126514"}

This example may look trivial, but it is very handy to maintain a logging context up and down a Python call stack without having to pass additional variables to the functions and methods that you call.

Implementation details

Logging contexts are stored as thread-local variables. If you want to share information between threads, you must create a Logging context in each thread with the same information.

Similarly, logging contexts are deliberately not copied when creating subprocesses. This is done to minimize bugs and make sure that log-with-context behaves in the exact same manner across operating systems.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

log-with-context-0.2.0.tar.gz (3.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

log_with_context-0.2.0-py3-none-any.whl (3.7 kB view details)

Uploaded Python 3

File details

Details for the file log-with-context-0.2.0.tar.gz.

File metadata

  • Download URL: log-with-context-0.2.0.tar.gz
  • Upload date:
  • Size: 3.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.10 CPython/3.9.7 Linux/5.8.0-1041-azure

File hashes

Hashes for log-with-context-0.2.0.tar.gz
Algorithm Hash digest
SHA256 07ee147860274c1614a914c0fd6b580ab0bfcb100167c88f8aad7898f37749ee
MD5 24fd4755beec65d03cf7c75b32715f37
BLAKE2b-256 e42bbc28c9b96482e481569a78e95db779ea2f91eaeef5c370b31836380be139

See more details on using hashes here.

File details

Details for the file log_with_context-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: log_with_context-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 3.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.10 CPython/3.9.7 Linux/5.8.0-1041-azure

File hashes

Hashes for log_with_context-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 65ebaeb2dded2b2a3fbc54159608ab65e58193d5834dbab29919cbf27387b5bb
MD5 5857d4c1cb661495d716d4787923ae90
BLAKE2b-256 bfee3441bbd142573be63f1035e753619564dcd47df3e6c33e0b68c53df6e39b

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page