diff --git a/src/grist_mcp/logging.py b/src/grist_mcp/logging.py index ca92ed5..f5af822 100644 --- a/src/grist_mcp/logging.py +++ b/src/grist_mcp/logging.py @@ -1,8 +1,31 @@ """Logging configuration and utilities.""" +import logging +import os from datetime import datetime +def setup_logging() -> None: + """Configure logging based on LOG_LEVEL environment variable. + + Valid levels: DEBUG, INFO, WARNING, ERROR (default: INFO) + """ + level_name = os.environ.get("LOG_LEVEL", "INFO").upper() + level = getattr(logging, level_name, None) + + if not isinstance(level, int): + level = logging.INFO + + logger = logging.getLogger("grist_mcp") + logger.setLevel(level) + + # Only add handler if not already configured + if not logger.handlers: + handler = logging.StreamHandler() + handler.setFormatter(logging.Formatter("%(message)s")) + logger.addHandler(handler) + + def extract_stats(tool_name: str, arguments: dict, result: dict) -> str: """Extract meaningful stats from tool call based on tool type.""" if tool_name == "list_documents": diff --git a/tests/unit/test_logging.py b/tests/unit/test_logging.py index c21943e..4a3d46d 100644 --- a/tests/unit/test_logging.py +++ b/tests/unit/test_logging.py @@ -1,5 +1,7 @@ """Unit tests for logging module.""" +import logging + from grist_mcp.logging import truncate_token, extract_stats, format_tool_log @@ -119,3 +121,32 @@ class TestFormatToolLog: ) assert "error" in line assert "\n Grist API error: Invalid column 'foo'" in line + + +class TestSetupLogging: + def test_default_level_is_info(self, monkeypatch): + monkeypatch.delenv("LOG_LEVEL", raising=False) + + from grist_mcp.logging import setup_logging + setup_logging() + + logger = logging.getLogger("grist_mcp") + assert logger.level == logging.INFO + + def test_respects_log_level_env(self, monkeypatch): + monkeypatch.setenv("LOG_LEVEL", "DEBUG") + + from grist_mcp.logging import setup_logging + setup_logging() + + logger = logging.getLogger("grist_mcp") + assert logger.level == logging.DEBUG + + def test_invalid_level_defaults_to_info(self, monkeypatch): + monkeypatch.setenv("LOG_LEVEL", "INVALID") + + from grist_mcp.logging import setup_logging + setup_logging() + + logger = logging.getLogger("grist_mcp") + assert logger.level == logging.INFO