Skip to content

Commit 5f2cff2

Browse files
logs handled
1 parent b8d7715 commit 5f2cff2

2 files changed

Lines changed: 74 additions & 18 deletions

File tree

src/codegraphcontext/cli/config_manager.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
"INDEX_VARIABLES": "true",
2727
"ALLOW_DB_DELETION": "false",
2828
"DEBUG_LOGS": "false",
29+
"DEBUG_LOG_PATH": str(Path.home() / "mcp_debug.log"),
30+
"ENABLE_APP_LOGS": "INFO",
2931
"LOG_FILE_PATH": str(CONFIG_DIR / "logs" / "cgc.log"),
3032
"MAX_FILE_SIZE_MB": "10",
3133
"IGNORE_TEST_FILES": "false",
@@ -45,8 +47,10 @@
4547
"FALKORDB_SOCKET_PATH": "Path to FalkorDB Unix socket",
4648
"INDEX_VARIABLES": "Index variable nodes in the graph (lighter graph if false)",
4749
"ALLOW_DB_DELETION": "Allow full database deletion commands",
48-
"DEBUG_LOGS": "Enable debug logging",
49-
"LOG_FILE_PATH": "Path to save log files",
50+
"DEBUG_LOGS": "Enable debug logging (for development/troubleshooting)",
51+
"DEBUG_LOG_PATH": "Path to debug log file",
52+
"ENABLE_APP_LOGS": "Application log level (DEBUG|INFO|WARNING|ERROR|CRITICAL|DISABLED)",
53+
"LOG_FILE_PATH": "Path to application log file",
5054
"MAX_FILE_SIZE_MB": "Maximum file size to index (in MB)",
5155
"IGNORE_TEST_FILES": "Skip test files during indexing",
5256
"IGNORE_HIDDEN_FILES": "Skip hidden files/directories",
@@ -64,6 +68,7 @@
6468
"INDEX_VARIABLES": ["true", "false"],
6569
"ALLOW_DB_DELETION": ["true", "false"],
6670
"DEBUG_LOGS": ["true", "false"],
71+
"ENABLE_APP_LOGS": ["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL", "DISABLED"],
6772
"IGNORE_TEST_FILES": ["true", "false"],
6873
"IGNORE_HIDDEN_FILES": ["true", "false"],
6974
"ENABLE_AUTO_WATCH": ["true", "false"],
@@ -257,7 +262,7 @@ def validate_config_value(key: str, value: str) -> tuple[bool, Optional[str]]:
257262
except ValueError:
258263
return False, "MAX_DEPTH must be 'unlimited' or a number"
259264

260-
if key == "LOG_FILE_PATH":
265+
if key in ("LOG_FILE_PATH", "DEBUG_LOG_PATH"):
261266
# Validate path is writable
262267
log_path = Path(value)
263268
try:
Lines changed: 66 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,90 @@
11
import os
22
from datetime import datetime
33
import logging
4+
from pathlib import Path
45
logger = logging.getLogger(__name__)
56

6-
# Toggle this to True to enable debug logging
7-
debug_mode = True # Set to True for dev/test, False for production
8-
log_mode = True # Set to True to enable user login logging
7+
# Log level mapping
8+
LOG_LEVELS = {
9+
'DEBUG': logging.DEBUG,
10+
'INFO': logging.INFO,
11+
'WARNING': logging.WARNING,
12+
'ERROR': logging.ERROR,
13+
'CRITICAL': logging.CRITICAL,
14+
'DISABLED': logging.CRITICAL + 10 # Higher than CRITICAL to disable all
15+
}
16+
17+
def _get_config_value(key, default):
18+
"""Helper to get config value with fallback"""
19+
try:
20+
from codegraphcontext.cli.config_manager import get_config_value
21+
value = get_config_value(key)
22+
if value is None:
23+
return default
24+
# Convert string boolean to actual boolean
25+
if isinstance(value, str):
26+
if value.lower() in ('true', 'false'):
27+
return value.lower() == 'true'
28+
return value
29+
except Exception:
30+
return default
31+
32+
def _should_log(level_name):
33+
"""Check if a message at the given level should be logged"""
34+
configured_level = _get_config_value('ENABLE_APP_LOGS', 'INFO')
35+
36+
# Handle legacy boolean values
37+
if isinstance(configured_level, bool):
38+
return configured_level
39+
40+
# Convert to uppercase for comparison
41+
configured_level = str(configured_level).upper()
42+
43+
# If disabled, don't log anything
44+
if configured_level == 'DISABLED':
45+
return False
46+
47+
# Get numeric levels
48+
configured_numeric = LOG_LEVELS.get(configured_level, logging.INFO)
49+
message_numeric = LOG_LEVELS.get(level_name.upper(), logging.INFO)
50+
51+
# Log if message level >= configured level
52+
return message_numeric >= configured_numeric
953

1054
def debug_log(message):
11-
"""Write debug message to a file if debug_mode is enabled"""
55+
"""Write debug message to a file if DEBUG_LOGS is enabled"""
56+
# Check if debug logging is enabled via config
57+
debug_mode = _get_config_value('DEBUG_LOGS', False)
1258
if not debug_mode:
1359
return
14-
debug_file = os.path.expanduser("~/mcp_debug.log")
60+
61+
# Get debug log path from config
62+
debug_file = _get_config_value('DEBUG_LOG_PATH', os.path.expanduser("~/mcp_debug.log"))
63+
64+
# Ensure parent directory exists
65+
Path(debug_file).parent.mkdir(parents=True, exist_ok=True)
66+
1567
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
1668
with open(debug_file, "a") as f:
1769
f.write(f"[{timestamp}] {message}\n")
1870
f.flush()
1971

2072
def info_logger(msg):
21-
if log_mode:
73+
"""Log info message if log level allows"""
74+
if _should_log('INFO'):
2275
return logger.info(msg)
23-
else:
24-
return
2576

2677
def error_logger(msg):
27-
if log_mode:
78+
"""Log error message if log level allows"""
79+
if _should_log('ERROR'):
2880
return logger.error(msg)
29-
else:
30-
return
3181

3282
def warning_logger(msg):
33-
if log_mode:
83+
"""Log warning message if log level allows"""
84+
if _should_log('WARNING'):
3485
return logger.warning(msg)
35-
else:
36-
return
3786

3887
def debug_logger(msg):
39-
return logger.debug(msg)
88+
"""Log debug message if log level allows"""
89+
if _should_log('DEBUG'):
90+
return logger.debug(msg)

0 commit comments

Comments
 (0)