feat(logging): add setup_logging with LOG_LEVEL support
This commit is contained in:
@@ -1,8 +1,31 @@
|
|||||||
"""Logging configuration and utilities."""
|
"""Logging configuration and utilities."""
|
||||||
|
|
||||||
|
import logging
|
||||||
|
import os
|
||||||
from datetime import datetime
|
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:
|
def extract_stats(tool_name: str, arguments: dict, result: dict) -> str:
|
||||||
"""Extract meaningful stats from tool call based on tool type."""
|
"""Extract meaningful stats from tool call based on tool type."""
|
||||||
if tool_name == "list_documents":
|
if tool_name == "list_documents":
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
"""Unit tests for logging module."""
|
"""Unit tests for logging module."""
|
||||||
|
|
||||||
|
import logging
|
||||||
|
|
||||||
from grist_mcp.logging import truncate_token, extract_stats, format_tool_log
|
from grist_mcp.logging import truncate_token, extract_stats, format_tool_log
|
||||||
|
|
||||||
|
|
||||||
@@ -119,3 +121,32 @@ class TestFormatToolLog:
|
|||||||
)
|
)
|
||||||
assert "error" in line
|
assert "error" in line
|
||||||
assert "\n Grist API error: Invalid column 'foo'" 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
|
||||||
|
|||||||
Reference in New Issue
Block a user