Add opt-in logging via add_sink, silence library logger by default
This commit is contained in:
@@ -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
|
## [0.3.0] - 2026-06-03
|
||||||
|
|
||||||
### Added
|
### Added
|
||||||
|
|||||||
+1
-1
@@ -1,6 +1,6 @@
|
|||||||
[project]
|
[project]
|
||||||
name = "sqlmem"
|
name = "sqlmem"
|
||||||
version = "0.3.0"
|
version = "0.4.0"
|
||||||
description = ""
|
description = ""
|
||||||
authors = [
|
authors = [
|
||||||
{name = "jan.doubravsky@gmail.com"}
|
{name = "jan.doubravsky@gmail.com"}
|
||||||
|
|||||||
+34
-1
@@ -1,4 +1,37 @@
|
|||||||
|
from typing import Any
|
||||||
|
|
||||||
|
from loguru import logger
|
||||||
|
|
||||||
|
from .config import DEBUG
|
||||||
from .engine import CachingEngine
|
from .engine import CachingEngine
|
||||||
from .exceptions import ReadOnlyError, UnsupportedQueryError
|
from .exceptions import ReadOnlyError, UnsupportedQueryError
|
||||||
|
|
||||||
__all__ = ["CachingEngine", "ReadOnlyError", "UnsupportedQueryError"]
|
_DEFAULT_FORMAT = (
|
||||||
|
"<green>{time:YYYY-MM-DD HH:mm:ss}</green> | "
|
||||||
|
"<level>{level: <8}</level> | "
|
||||||
|
"<cyan>{name}</cyan>:<cyan>{line}</cyan> - "
|
||||||
|
"<level>{message}</level>"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
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"]
|
||||||
|
|||||||
@@ -10,10 +10,5 @@ DEBUG = os.getenv("SQLMEM_DEBUG", "false").lower() == "true"
|
|||||||
CACHE_DB_PATH = Path(os.getenv("SQLMEM_CACHE_DB", "cache.db"))
|
CACHE_DB_PATH = Path(os.getenv("SQLMEM_CACHE_DB", "cache.db"))
|
||||||
BACKUP_INTERVAL_SECONDS = int(os.getenv("SQLMEM_BACKUP_INTERVAL", "3600"))
|
BACKUP_INTERVAL_SECONDS = int(os.getenv("SQLMEM_BACKUP_INTERVAL", "3600"))
|
||||||
|
|
||||||
logger.remove()
|
# Silent by default — callers opt in via add_sink().
|
||||||
logger.add(
|
logger.disable("sqlmem")
|
||||||
sink=lambda msg: print(msg, end=""),
|
|
||||||
level="DEBUG" if DEBUG else "INFO",
|
|
||||||
format="<green>{time:YYYY-MM-DD HH:mm:ss}</green> | <level>{level: <8}</level> | <cyan>{name}</cyan>:<cyan>{line}</cyan> - <level>{message}</level>",
|
|
||||||
colorize=True,
|
|
||||||
)
|
|
||||||
|
|||||||
Reference in New Issue
Block a user