|
1 | 1 | import os |
2 | 2 | from datetime import datetime |
3 | 3 | import logging |
| 4 | +from pathlib import Path |
4 | 5 | logger = logging.getLogger(__name__) |
5 | 6 |
|
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 |
9 | 53 |
|
10 | 54 | 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) |
12 | 58 | if not debug_mode: |
13 | 59 | 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 | + |
15 | 67 | timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S") |
16 | 68 | with open(debug_file, "a") as f: |
17 | 69 | f.write(f"[{timestamp}] {message}\n") |
18 | 70 | f.flush() |
19 | 71 |
|
20 | 72 | def info_logger(msg): |
21 | | - if log_mode: |
| 73 | + """Log info message if log level allows""" |
| 74 | + if _should_log('INFO'): |
22 | 75 | return logger.info(msg) |
23 | | - else: |
24 | | - return |
25 | 76 |
|
26 | 77 | def error_logger(msg): |
27 | | - if log_mode: |
| 78 | + """Log error message if log level allows""" |
| 79 | + if _should_log('ERROR'): |
28 | 80 | return logger.error(msg) |
29 | | - else: |
30 | | - return |
31 | 81 |
|
32 | 82 | def warning_logger(msg): |
33 | | - if log_mode: |
| 83 | + """Log warning message if log level allows""" |
| 84 | + if _should_log('WARNING'): |
34 | 85 | return logger.warning(msg) |
35 | | - else: |
36 | | - return |
37 | 86 |
|
38 | 87 | 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