Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 16 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Contextual logging
# Logging with context (contextual logging)

This library provides utilities to easily add context to logging messages and to show them.

Expand All @@ -23,6 +23,14 @@ This library provides utilities and logging abstractions to ease the use of this
Python 3.9 or greater


## Install

Install the package [logging-with-context](pypi.org/project/logging-with-context).

> [!NOTE]
> The package is named logging-with-context because contextual-logging was rejected by Pypi due to being too similar to an existing project.


## How to use it

The expected workflow is:
Expand All @@ -40,7 +48,7 @@ After configuring your application's logging, initialize the global context:
```python
import logging

from contextual_logging.global_context import global_context_initialized
from logging_with_context.global_context import global_context_initialized


def main():
Expand All @@ -56,7 +64,7 @@ It accepts a dictionary with the values you want to include in all the logging m
```python
import logging

from contextual_logging.global_context import add_global_context
from logging_with_context.global_context import add_global_context


# ... somewhere in your app ...
Expand All @@ -77,7 +85,7 @@ In case you can't use the context manager, you can use the manual initialization
```python
import logging

from contextual_logging.global_context import init_global_context, shutdown_global_context
from logging_with_context.global_context import init_global_context, shutdown_global_context


def main():
Expand Down Expand Up @@ -109,7 +117,7 @@ To show the context you need a `Formatter` that somehow uses the context in the

For example, the logging handler in Python applications running at AWS Lambda handles this automatically by adding the context provided in `extra` as labels to the log struct, separated from the message.

If you're logging to a `StreamHandler` you can use `contextual_logging.formatters.ExtraTextFormatter`, which accepts the same options as the standard library `Formatter`.
If you're logging to a `StreamHandler` you can use `logging_with_context.formatters.ExtraTextFormatter`, which accepts the same options as the standard library `Formatter`.

You can use it instead of the default `Formatter` in your logging setup:

Expand All @@ -118,7 +126,7 @@ version: 1

formatters:
contextual:
class: contextual_logging.formatters.ExtraTextFormatter
class: logging_with_context.formatters.ExtraTextFormatter
format: '%(levelname)s %(message)s'

handlers:
Expand All @@ -137,7 +145,7 @@ If you're modifying the logging setup made by other part of an application, you
```python
import logging

from contextual_logging.formatters import ExtraTextFormatter
from logging_with_context.formatters import ExtraTextFormatter

def main():
logging.basicConfig(level=logging.INFO)
Expand All @@ -161,7 +169,7 @@ The API accepts a list of loggers where the `Filter` will be attached in these c
```python
import logging

from contextual_logging.global_context import global_context_initialized
from logging_with_context.global_context import global_context_initialized


def app_entrypoint():
Expand Down
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[project]
name = "contextual-logging"
name = "logging-with-context"
description = "Python library to add context to logging messages"
readme = "README.md"
authors = [
Expand Down Expand Up @@ -44,7 +44,7 @@ select = [
split-on-trailing-comma = false

[tool.pytest.ini_options]
addopts = "--cov-report term --cov-report html --cov contextual_logging"
addopts = "--cov-report term --cov-report html --cov logging_with_context"

[tool.coverage.run]
branch = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ class ContextualAdapter(LoggerAdapter):
Set up everything

>>> import logging
>>> from contextual_logging.adapters import ContextualAdapter
>>> from contextual_logging.formatters import ExtraTextFormatter
>>> from logging_with_context.adapters import ContextualAdapter
>>> from logging_with_context.formatters import ExtraTextFormatter
>>> logging.basicConfig(level=logging.INFO)
>>> root = logging.getLogger()

Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def __init__(
For example:

>>> import logging
>>> from contextual_logging.formatters import ExtraTextFormatter
>>> from logging_with_context.formatters import ExtraTextFormatter
>>> logging.basicConfig(level=logging.INFO)
>>> for handler in logging.getLogger().handlers:
>>> handler.setFormatter(ExtraTextFormatter(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from logging import Logger, getLogger
from typing import Any, Generator, Optional, Sequence

from contextual_logging.filters import FilterWithContextVar
from logging_with_context.filters import FilterWithContextVar

# NOTE: ContextVar should be created at the top module level.
__global_context_var: ContextVar[dict[str, Any]] = ContextVar(
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from pytest import LogCaptureFixture

from contextual_logging.adapters import ContextualAdapter
from logging_with_context.adapters import ContextualAdapter


def test_contextual_adapter_with_extras_ok(caplog: LogCaptureFixture):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import pytest
from pytest import LogCaptureFixture

from contextual_logging.formatters import ExtraTextFormatter
from logging_with_context.formatters import ExtraTextFormatter

CaplogFactory = Callable[[logging.Formatter], LogCaptureFixture]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import pytest

from contextual_logging.global_context import (
from logging_with_context.global_context import (
add_global_context,
global_context_initialized,
)
Expand Down
44 changes: 22 additions & 22 deletions uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading