From 0faa01d89bb9d3ea27ba553119556dc0e57316f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Doubravsk=C3=BD?= Date: Wed, 3 Jun 2026 09:33:32 +0200 Subject: [PATCH] Add opt-in logging via add_sink, silence library logger by default --- CHANGELOG.md | 11 +++++++++++ pyproject.toml | 2 +- src/sqlmem/__init__.py | 35 ++++++++++++++++++++++++++++++++++- src/sqlmem/config.py | 9 ++------- 4 files changed, 48 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6dc513c..0cec7d2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,17 @@ All notable changes to this project will be documented in this file. --- +## [0.4.0] - 2026-06-03 + +### Added +- `add_sink(sink, *, level, **kwargs)` — public API for routing sqlmem log records to any loguru-compatible sink (stream, file, callable); supports all loguru `logger.add()` kwargs including `rotation`, `retention`, etc. + +### Changed +- `pyproject.toml` — bumped version to `0.4.0` +- `config.py` — replaced destructive `logger.remove()` + forced default sink with `logger.disable("sqlmem")`; sqlmem is now silent by default and does not interfere with the host application's logging setup + +--- + ## [0.3.0] - 2026-06-03 ### Added diff --git a/pyproject.toml b/pyproject.toml index b29c412..6c7ec15 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [project] name = "sqlmem" -version = "0.3.0" +version = "0.4.0" description = "" authors = [ {name = "jan.doubravsky@gmail.com"} diff --git a/src/sqlmem/__init__.py b/src/sqlmem/__init__.py index 3e8a707..4c21084 100644 --- a/src/sqlmem/__init__.py +++ b/src/sqlmem/__init__.py @@ -1,4 +1,37 @@ +from typing import Any + +from loguru import logger + +from .config import DEBUG from .engine import CachingEngine from .exceptions import ReadOnlyError, UnsupportedQueryError -__all__ = ["CachingEngine", "ReadOnlyError", "UnsupportedQueryError"] +_DEFAULT_FORMAT = ( + "{time:YYYY-MM-DD HH:mm:ss} | " + "{level: <8} | " + "{name}:{line} - " + "{message}" +) + + +def add_sink(sink: Any, *, level: str | None = None, **kwargs: Any) -> None: + """Route sqlmem log records to *sink*. + + Accepts any sink supported by loguru (file path, stream, callable, …). + *level* defaults to ``DEBUG`` when ``SQLMEM_DEBUG=true``, otherwise ``INFO``. + Extra keyword arguments are forwarded to :func:`loguru.logger.add`. + + Example:: + + import sys + from sqlmem import add_sink + add_sink(sys.stderr) + add_sink("sqlmem.log", rotation="10 MB") + """ + logger.enable("sqlmem") + kwargs.setdefault("format", _DEFAULT_FORMAT) + kwargs.setdefault("colorize", True) + logger.add(sink, level=level or ("DEBUG" if DEBUG else "INFO"), filter="sqlmem", **kwargs) + + +__all__ = ["CachingEngine", "ReadOnlyError", "UnsupportedQueryError", "add_sink"] diff --git a/src/sqlmem/config.py b/src/sqlmem/config.py index f8b7771..25fc91f 100644 --- a/src/sqlmem/config.py +++ b/src/sqlmem/config.py @@ -10,10 +10,5 @@ DEBUG = os.getenv("SQLMEM_DEBUG", "false").lower() == "true" CACHE_DB_PATH = Path(os.getenv("SQLMEM_CACHE_DB", "cache.db")) BACKUP_INTERVAL_SECONDS = int(os.getenv("SQLMEM_BACKUP_INTERVAL", "3600")) -logger.remove() -logger.add( - sink=lambda msg: print(msg, end=""), - level="DEBUG" if DEBUG else "INFO", - format="{time:YYYY-MM-DD HH:mm:ss} | {level: <8} | {name}:{line} - {message}", - colorize=True, -) +# Silent by default — callers opt in via add_sink(). +logger.disable("sqlmem")