From 604bf99bcf690dd2f525fedc94316a2f516b1009 Mon Sep 17 00:00:00 2001 From: PakitoSec Date: Sun, 5 Apr 2026 18:10:03 +0200 Subject: [PATCH] feat: update logger initialization to use get_logger; bump version to 0.9.1 --- README.md | 18 ++++++++---------- pyproject.toml | 2 +- src/logurich/__init__.py | 4 +++- src/logurich/core.py | 13 ++++++++++++- uv.lock | 2 +- 5 files changed, 25 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 4ee7b97..f6a443a 100644 --- a/README.md +++ b/README.md @@ -16,15 +16,13 @@ pip install logurich[click] ## Usage ```python -import logging - from rich.panel import Panel -from logurich import init_logger +from logurich import get_logger, init_logger init_logger("INFO", enqueue=False) -logger = logging.getLogger(__name__) +logger = get_logger(__name__) logger.info("This is a log message") logger.info("Hello %s", "world") @@ -52,7 +50,9 @@ logger.info( ``` -Named loggers returned by `logging.getLogger(...)` expose `ctx(...)`, `rich(...)`, `bind(...)`, and `contextualize(...)`. `logger.ctx(...)` is shorthand for the existing module-level `ctx(...)` helper, and `logger.contextualize(...)` is a convenience alias for `global_context_configure(...)`. The module-level helpers remain supported if you prefer `global_context_configure(...)` or `extra={"context": {"key": ctx(...)}}`. +For full IDE autocompletion of `ctx(...)`, `rich(...)`, `bind(...)`, and `contextualize(...)`, use `get_logger(...)` from logurich instead of `logging.getLogger(...)`. It returns the same logger instance but typed as `LogurichLogger`. `logger.ctx(...)` is shorthand for the existing module-level `ctx(...)` helper, and `logger.contextualize(...)` is a convenience alias for `global_context_configure(...)`. The module-level helpers remain supported if you prefer `global_context_configure(...)` or `extra={"context": {"key": ctx(...)}}`. + +`logging.getLogger(...)` still works at runtime — only the typing differs. For short-lived scripts and CLIs, `init_logger()` automatically registers an `atexit` hook, so you do not need to call `shutdown_logger()` just to flush logs at process exit. @@ -61,17 +61,15 @@ For short-lived scripts and CLIs, `init_logger()` automatically registers an `at Use the standard library to create named loggers: ```python -import logging - -from logurich import init_logger +from logurich import get_logger, init_logger init_logger("INFO", enqueue=False) -logger = logging.getLogger(__name__) +logger = get_logger(__name__) logger.info("Hello from %s", __name__) ``` -Logurich does not export a module-level logger or logger factory. Clients should create named loggers with `logging.getLogger(...)`. +Use `get_logger(...)` from logurich for typed access. `logging.getLogger(...)` still works at runtime. ## Using Logurich in Reusable Libraries diff --git a/pyproject.toml b/pyproject.toml index fc67ad9..5324ea5 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [project] name = "logurich" -version = "0.9.0" +version = "0.9.1" description = "A Python library combining standard logging and Rich for beautiful logging." authors = [ { name = "PakitoSec", email = "jeromep83@gmail.com" } diff --git a/src/logurich/__init__.py b/src/logurich/__init__.py index 23f6217..cc76b5f 100644 --- a/src/logurich/__init__.py +++ b/src/logurich/__init__.py @@ -1,6 +1,6 @@ """Public package exports for logurich.""" -__version__ = "0.9.0" +__version__ = "0.9.1" from .console import ( console, @@ -18,6 +18,7 @@ configure_child_logging, ctx, get_log_queue, + get_logger, global_context_configure, global_context_set, init_logger, @@ -42,4 +43,5 @@ "rich_to_str", "LOG_LEVEL_CHOICES", "LogLevel", + "get_logger", ] diff --git a/src/logurich/core.py b/src/logurich/core.py index de21092..dc9a29a 100644 --- a/src/logurich/core.py +++ b/src/logurich/core.py @@ -342,7 +342,18 @@ def _install_logger_class() -> None: _install_logger_class() -_internal_logger: LogurichLogger = logging.getLogger("logurich") + +def get_logger(name: Optional[str] = None) -> LogurichLogger: + """Typed wrapper around :func:`logging.getLogger`. + + Returns the same logger instance but typed as :class:`LogurichLogger` + so that IDEs auto-complete ``ctx``, ``rich``, ``bind``, and + ``contextualize``. + """ + return logging.getLogger(name) # type: ignore[return-value] + + +_internal_logger: LogurichLogger = get_logger("logurich") _internal_logger.setLevel(logging.NOTSET) _internal_logger.propagate = True diff --git a/uv.lock b/uv.lock index a435239..51a88fb 100644 --- a/uv.lock +++ b/uv.lock @@ -149,7 +149,7 @@ wheels = [ [[package]] name = "logurich" -version = "0.9.0" +version = "0.9.1" source = { editable = "." } dependencies = [ { name = "rich" },