Skip to content

Commit cb47c58

Browse files
authored
feat: add debug mode to control file logging (#11)
1 parent 394c860 commit cb47c58

File tree

4 files changed

+89
-5
lines changed

4 files changed

+89
-5
lines changed

src/mcpcat/__init__.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
from .modules.compatibility import is_compatible_server, is_fastmcp_server
1313
from .modules.internal import set_server_tracking_data
14-
from .modules.logging import write_to_log
14+
from .modules.logging import write_to_log, set_debug_mode
1515
from .types import (
1616
MCPCatData,
1717
MCPCatOptions,
@@ -43,6 +43,9 @@ def track(
4343
if options is None:
4444
options = MCPCatOptions()
4545

46+
# Update global debug_mode value
47+
set_debug_mode(options.debug_mode)
48+
4649
# Validate configuration
4750
if not project_id and not options.exporters:
4851
raise ValueError(

src/mcpcat/modules/logging.py

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,22 @@
33
import os
44
from datetime import datetime, timezone
55

6+
from mcpcat.types import MCPCatOptions
7+
8+
9+
# Initialize debug_mode from environment variable at module load time
10+
_env_debug = os.getenv("MCPCAT_DEBUG_MODE")
11+
if _env_debug is not None:
12+
debug_mode = _env_debug.lower() in ("true", "1", "yes", "on")
13+
else:
14+
debug_mode = False
15+
16+
17+
def set_debug_mode(value: bool) -> None:
18+
"""Set the global debug_mode value."""
19+
global debug_mode
20+
debug_mode = value
21+
622

723
def write_to_log(message: str) -> None:
824
timestamp = datetime.now(timezone.utc).isoformat()
@@ -12,9 +28,10 @@ def write_to_log(message: str) -> None:
1228
log_path = os.path.expanduser("~/mcpcat.log")
1329

1430
try:
15-
# Write to log file (no need to ensure directory exists for home directory)
16-
with open(log_path, "a") as f:
17-
f.write(log_entry)
31+
if debug_mode:
32+
# Write to log file (no need to ensure directory exists for home directory)
33+
with open(log_path, "a") as f:
34+
f.write(log_entry)
1835
except Exception:
1936
# Silently fail - we don't want logging errors to break the server
2037
pass

src/mcpcat/types.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,8 @@ class MCPCatOptions:
122122
identify: IdentifyFunction | None = None
123123
redact_sensitive_information: RedactionFunction | None = None
124124
exporters: dict[str, ExporterConfig] | None = None
125+
debug_mode: bool = False
126+
125127

126128

127129
@dataclass

tests/test_logging.py

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
import pytest
1010

11-
from mcpcat.modules.logging import write_to_log
11+
from mcpcat.modules.logging import write_to_log, set_debug_mode
1212

1313

1414
class TestLogging:
@@ -31,6 +31,38 @@ def cleanup_log_file(self):
3131

3232
def test_write_to_log_creates_file(self, tmp_path):
3333
"""Test that write_to_log creates the log file if it doesn't exist."""
34+
# Enable debug mode
35+
set_debug_mode(True)
36+
37+
# Use a unique file name for this test
38+
unique_id = str(uuid.uuid4())
39+
log_file = tmp_path / f"test_mcpcat_{unique_id}.log"
40+
41+
# Mock os.path.expanduser to use our temp file
42+
with patch(
43+
"mcpcat.modules.logging.os.path.expanduser", return_value=str(log_file)
44+
):
45+
# Write a test message
46+
test_message = f"Test log message {unique_id}"
47+
write_to_log(test_message)
48+
49+
# Check that the file was created
50+
assert log_file.exists(), "Log file was not created"
51+
52+
# Read the file content
53+
content = log_file.read_text()
54+
55+
# Verify the message is in the file
56+
assert test_message in content, "Log message not found in file"
57+
58+
# Verify timestamp format (ISO format)
59+
assert "T" in content, "Timestamp not in ISO format"
60+
61+
def test_write_to_log_checks_debug_mode(self, tmp_path):
62+
"""Test that write_to_log writes to file when debug mode is enabled."""
63+
# Enable debug mode
64+
set_debug_mode(True)
65+
3466
# Use a unique file name for this test
3567
unique_id = str(uuid.uuid4())
3668
log_file = tmp_path / f"test_mcpcat_{unique_id}.log"
@@ -55,8 +87,29 @@ def test_write_to_log_creates_file(self, tmp_path):
5587
# Verify timestamp format (ISO format)
5688
assert "T" in content, "Timestamp not in ISO format"
5789

90+
# Check that log file is not created when debug mode is disabled
91+
set_debug_mode(False)
92+
93+
# Use a unique file name for this test
94+
unique_id = str(uuid.uuid4())
95+
log_file = tmp_path / f"test_mcpcat_{unique_id}.log"
96+
97+
# Mock os.path.expanduser to use our temp file
98+
with patch(
99+
"mcpcat.modules.logging.os.path.expanduser", return_value=str(log_file)
100+
):
101+
# Write a test message
102+
test_message = f"Test log message {unique_id}"
103+
write_to_log(test_message)
104+
105+
# Check that the file was created
106+
assert not log_file.exists(), "Log file was wrongly created"
107+
58108
def test_write_to_log_appends_messages(self, tmp_path):
59109
"""Test that write_to_log appends to existing log file."""
110+
# Enable debug mode
111+
set_debug_mode(True)
112+
60113
# Use a unique file name for this test
61114
unique_id = str(uuid.uuid4())
62115
log_file = tmp_path / f"test_mcpcat_{unique_id}.log"
@@ -105,6 +158,9 @@ def test_write_to_log_appends_messages(self, tmp_path):
105158

106159
def test_write_to_log_handles_directory_creation(self, tmp_path):
107160
"""Test that write_to_log creates parent directories if needed."""
161+
# Enable debug mode
162+
set_debug_mode(True)
163+
108164
# Use a unique file name for this test
109165
unique_id = str(uuid.uuid4())
110166
log_file = tmp_path / f"test_mcpcat_{unique_id}.log"
@@ -123,6 +179,9 @@ def test_write_to_log_handles_directory_creation(self, tmp_path):
123179

124180
def test_write_to_log_silently_handles_errors(self, tmp_path, monkeypatch):
125181
"""Test that write_to_log doesn't raise exceptions on errors."""
182+
# Enable debug mode
183+
set_debug_mode(True)
184+
126185
# Use a unique file name for this test
127186
unique_id = str(uuid.uuid4())
128187
log_file = tmp_path / f"test_mcpcat_{unique_id}.log"
@@ -146,6 +205,9 @@ def test_write_to_log_silently_handles_errors(self, tmp_path, monkeypatch):
146205

147206
def test_log_format(self, tmp_path):
148207
"""Test the format of log entries."""
208+
# Enable debug mode
209+
set_debug_mode(True)
210+
149211
# Use a unique file name for this test
150212
unique_id = str(uuid.uuid4())
151213
log_file = tmp_path / f"test_mcpcat_{unique_id}.log"

0 commit comments

Comments
 (0)