Skip to content

Commit e07ac46

Browse files
committed
fix global setting and check env vars once on init
1 parent 58c146f commit e07ac46

File tree

3 files changed

+30
-60
lines changed

3 files changed

+30
-60
lines changed

src/mcpcat/__init__.py

Lines changed: 2 additions & 2 deletions
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, debug_mode
14+
from .modules.logging import write_to_log, set_debug_mode
1515
from .types import (
1616
MCPCatData,
1717
MCPCatOptions,
@@ -44,7 +44,7 @@ def track(
4444
options = MCPCatOptions()
4545

4646
# Update global debug_mode value
47-
debug_mode = options.debug_mode
47+
set_debug_mode(options.debug_mode)
4848

4949
# Validate configuration
5050
if not project_id and not options.exporters:

src/mcpcat/modules/logging.py

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,18 @@
66
from mcpcat.types import MCPCatOptions
77

88

9-
debug_mode = False
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
1021

1122

1223
def write_to_log(message: str) -> None:
@@ -17,13 +28,7 @@ def write_to_log(message: str) -> None:
1728
log_path = os.path.expanduser("~/mcpcat.log")
1829

1930
try:
20-
global debug_mode
21-
debug_mode = (
22-
os.getenv("MCPCAT_DEBUG_MODE").lower()
23-
if os.getenv("MCPCAT_DEBUG_MODE") != None
24-
else str(debug_mode).lower()
25-
)
26-
if debug_mode == "true":
31+
if debug_mode:
2732
# Write to log file (no need to ensure directory exists for home directory)
2833
with open(log_path, "a") as f:
2934
f.write(log_entry)

tests/test_logging.py

Lines changed: 15 additions & 50 deletions
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, debug_mode
11+
from mcpcat.modules.logging import write_to_log, set_debug_mode
1212

1313

1414
class TestLogging:
@@ -31,14 +31,8 @@ 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 using environment variable MCPCAT_DEBUG_MODE
35-
os.environ["MCPCAT_DEBUG_MODE"] = "true"
36-
global debug_mode
37-
debug_mode = (
38-
os.getenv("MCPCAT_DEBUG_MODE").lower()
39-
if os.getenv("MCPCAT_DEBUG_MODE") != None
40-
else str(debug_mode).lower()
41-
)
34+
# Enable debug mode
35+
set_debug_mode(True)
4236

4337
# Use a unique file name for this test
4438
unique_id = str(uuid.uuid4())
@@ -66,17 +60,8 @@ def test_write_to_log_creates_file(self, tmp_path):
6660

6761
def test_write_to_log_checks_debug_mode(self, tmp_path):
6862
"""Test that write_to_log writes to file when debug mode is enabled."""
69-
# Check that MCPCAT_DEBUG_MODE overrides the default setting and writes log to file
70-
os.environ["MCPCAT_DEBUG_MODE"] = "true"
71-
global debug_mode
72-
debug_mode = (
73-
os.getenv("MCPCAT_DEBUG_MODE").lower()
74-
if os.getenv("MCPCAT_DEBUG_MODE") != None
75-
else str(debug_mode).lower()
76-
)
77-
assert debug_mode == "true", (
78-
"Environment variable failed to override default setting"
79-
)
63+
# Enable debug mode
64+
set_debug_mode(True)
8065

8166
# Use a unique file name for this test
8267
unique_id = str(uuid.uuid4())
@@ -103,12 +88,7 @@ def test_write_to_log_checks_debug_mode(self, tmp_path):
10388
assert "T" in content, "Timestamp not in ISO format"
10489

10590
# Check that log file is not created when debug mode is disabled
106-
os.environ["MCPCAT_DEBUG_MODE"] = "false"
107-
debug_mode = (
108-
os.getenv("MCPCAT_DEBUG_MODE").lower()
109-
if os.getenv("MCPCAT_DEBUG_MODE") != None
110-
else str(debug_mode).lower()
111-
)
91+
set_debug_mode(False)
11292

11393
# Use a unique file name for this test
11494
unique_id = str(uuid.uuid4())
@@ -127,14 +107,8 @@ def test_write_to_log_checks_debug_mode(self, tmp_path):
127107

128108
def test_write_to_log_appends_messages(self, tmp_path):
129109
"""Test that write_to_log appends to existing log file."""
130-
# Enable debug mode using environment variable MCPCAT_DEBUG_MODE
131-
os.environ["MCPCAT_DEBUG_MODE"] = "true"
132-
global debug_mode
133-
debug_mode = (
134-
os.getenv("MCPCAT_DEBUG_MODE").lower()
135-
if os.getenv("MCPCAT_DEBUG_MODE") != None
136-
else str(debug_mode).lower()
137-
)
110+
# Enable debug mode
111+
set_debug_mode(True)
138112

139113
# Use a unique file name for this test
140114
unique_id = str(uuid.uuid4())
@@ -184,14 +158,8 @@ def test_write_to_log_appends_messages(self, tmp_path):
184158

185159
def test_write_to_log_handles_directory_creation(self, tmp_path):
186160
"""Test that write_to_log creates parent directories if needed."""
187-
# Enable debug mode using environment variable MCPCAT_DEBUG_MODE
188-
os.environ["MCPCAT_DEBUG_MODE"] = "true"
189-
global debug_mode
190-
debug_mode = (
191-
os.getenv("MCPCAT_DEBUG_MODE").lower()
192-
if os.getenv("MCPCAT_DEBUG_MODE") != None
193-
else str(debug_mode).lower()
194-
)
161+
# Enable debug mode
162+
set_debug_mode(True)
195163

196164
# Use a unique file name for this test
197165
unique_id = str(uuid.uuid4())
@@ -211,14 +179,8 @@ def test_write_to_log_handles_directory_creation(self, tmp_path):
211179

212180
def test_write_to_log_silently_handles_errors(self, tmp_path, monkeypatch):
213181
"""Test that write_to_log doesn't raise exceptions on errors."""
214-
# Enable debug mode using environment variable MCPCAT_DEBUG_MODE
215-
os.environ["MCPCAT_DEBUG_MODE"] = "true"
216-
global debug_mode
217-
debug_mode = (
218-
os.getenv("MCPCAT_DEBUG_MODE").lower()
219-
if os.getenv("MCPCAT_DEBUG_MODE") != None
220-
else str(debug_mode).lower()
221-
)
182+
# Enable debug mode
183+
set_debug_mode(True)
222184

223185
# Use a unique file name for this test
224186
unique_id = str(uuid.uuid4())
@@ -243,6 +205,9 @@ def test_write_to_log_silently_handles_errors(self, tmp_path, monkeypatch):
243205

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

0 commit comments

Comments
 (0)