diff --git a/README.md b/README.md index e205f11..11e64d8 100644 --- a/README.md +++ b/README.md @@ -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. @@ -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: @@ -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(): @@ -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 ... @@ -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(): @@ -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: @@ -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: @@ -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) @@ -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(): diff --git a/pyproject.toml b/pyproject.toml index b80f595..e4b4cef 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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 = [ @@ -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 diff --git a/src/contextual_logging/__init__.py b/src/logging_with_context/__init__.py similarity index 100% rename from src/contextual_logging/__init__.py rename to src/logging_with_context/__init__.py diff --git a/src/contextual_logging/adapters.py b/src/logging_with_context/adapters.py similarity index 94% rename from src/contextual_logging/adapters.py rename to src/logging_with_context/adapters.py index 3358e2b..9d2ccbc 100644 --- a/src/contextual_logging/adapters.py +++ b/src/logging_with_context/adapters.py @@ -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() diff --git a/src/contextual_logging/filters.py b/src/logging_with_context/filters.py similarity index 100% rename from src/contextual_logging/filters.py rename to src/logging_with_context/filters.py diff --git a/src/contextual_logging/formatters.py b/src/logging_with_context/formatters.py similarity index 97% rename from src/contextual_logging/formatters.py rename to src/logging_with_context/formatters.py index 950c586..5e526b5 100644 --- a/src/contextual_logging/formatters.py +++ b/src/logging_with_context/formatters.py @@ -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( diff --git a/src/contextual_logging/global_context.py b/src/logging_with_context/global_context.py similarity index 97% rename from src/contextual_logging/global_context.py rename to src/logging_with_context/global_context.py index 71d4ef4..a28b931 100644 --- a/src/contextual_logging/global_context.py +++ b/src/logging_with_context/global_context.py @@ -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( diff --git a/src/contextual_logging/py.typed b/src/logging_with_context/py.typed similarity index 100% rename from src/contextual_logging/py.typed rename to src/logging_with_context/py.typed diff --git a/tests/contextual_logging/__init__.py b/tests/logging_with_context/__init__.py similarity index 100% rename from tests/contextual_logging/__init__.py rename to tests/logging_with_context/__init__.py diff --git a/tests/contextual_logging/test_adapters.py b/tests/logging_with_context/test_adapters.py similarity index 97% rename from tests/contextual_logging/test_adapters.py rename to tests/logging_with_context/test_adapters.py index e2c1967..bc6c90a 100644 --- a/tests/contextual_logging/test_adapters.py +++ b/tests/logging_with_context/test_adapters.py @@ -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): diff --git a/tests/contextual_logging/test_formatters.py b/tests/logging_with_context/test_formatters.py similarity index 98% rename from tests/contextual_logging/test_formatters.py rename to tests/logging_with_context/test_formatters.py index 4af5ae8..16132a2 100644 --- a/tests/contextual_logging/test_formatters.py +++ b/tests/logging_with_context/test_formatters.py @@ -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] diff --git a/tests/contextual_logging/test_global_context.py b/tests/logging_with_context/test_global_context.py similarity index 96% rename from tests/contextual_logging/test_global_context.py rename to tests/logging_with_context/test_global_context.py index a031ff7..9e4c9b7 100644 --- a/tests/contextual_logging/test_global_context.py +++ b/tests/logging_with_context/test_global_context.py @@ -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, ) diff --git a/uv.lock b/uv.lock index 2eabcdb..d92e364 100644 --- a/uv.lock +++ b/uv.lock @@ -10,28 +10,6 @@ wheels = [ { url = "https://files.pythonhosted.org/packages/d1/d6/3965ed04c63042e047cb6a3e6ed1a63a35087b6a609aa3a15ed8ac56c221/colorama-0.4.6-py2.py3-none-any.whl", hash = "sha256:4f1d9991f5acc0ca119f9d443620b77f9d6b33703e51011c16baf57afb285fc6", size = 25335 }, ] -[[package]] -name = "contextual-logging" -version = "0.1.dev20+g0029eff.d20241215" -source = { editable = "." } - -[package.dev-dependencies] -dev = [ - { name = "pytest" }, - { name = "pytest-cov" }, - { name = "ruff" }, -] - -[package.metadata] - -[package.metadata.requires-dev] -dev = [ - { name = "pytest", specifier = ">=8.3.4" }, - { name = "pytest-cov", specifier = ">=6.0.0" }, - { name = "ruff", specifier = ">=0.8.2" }, -] -lint = [] - [[package]] name = "coverage" version = "7.6.8" @@ -124,6 +102,28 @@ wheels = [ { url = "https://files.pythonhosted.org/packages/ef/a6/62565a6e1cf69e10f5727360368e451d4b7f58beeac6173dc9db836a5b46/iniconfig-2.0.0-py3-none-any.whl", hash = "sha256:b6a85871a79d2e3b22d2d1b94ac2824226a63c6b741c88f7ae975f18b6778374", size = 5892 }, ] +[[package]] +name = "logging-with-context" +version = "0.1.dev25+g97fc909.d20241215" +source = { editable = "." } + +[package.dev-dependencies] +dev = [ + { name = "pytest" }, + { name = "pytest-cov" }, + { name = "ruff" }, +] + +[package.metadata] + +[package.metadata.requires-dev] +dev = [ + { name = "pytest", specifier = ">=8.3.4" }, + { name = "pytest-cov", specifier = ">=6.0.0" }, + { name = "ruff", specifier = ">=0.8.2" }, +] +lint = [] + [[package]] name = "packaging" version = "24.2"