File size: 7,188 Bytes
0997c23 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 | # Unified Debug Logging System
**Created:** October 2, 2025
**Status:** ✅ Implemented
## Overview
ComfyUI-SwissArmyKnife now has a **centralized debug logging system** that respects a single `DEBUG` environment variable across both Python backend and JavaScript frontend code.
## How It Works
### Environment Variable
Set the `DEBUG` environment variable to control debug logging:
```bash
# Enable debug logging
DEBUG=true
# Disable debug logging (default)
DEBUG=false
# Also accepts: 1, yes, True for enabled
```
### Backend (Python)
All Python debug logging now uses the centralized `debug_utils.py` module:
```python
from nodes.debug_utils import debug_print, is_debug_enabled
# Use debug_print instead of print for debug messages
debug_print("This will only show when DEBUG=true")
# Check if debug is enabled
if is_debug_enabled():
# Do expensive debug operations only when needed
pass
```
**Files Updated:**
- `nodes/debug_utils.py` - New centralized debug utilities module
- `nodes/config_api.py` - Exposes DEBUG setting to frontend via `/swissarmyknife/config`
- `__init__.py` - Loads DEBUG from environment
### Frontend (JavaScript)
All JavaScript debug logging uses the centralized debug system loaded from server config:
```javascript
// DEBUG mode - will be loaded from server config
let DEBUG_ENABLED = false;
// Conditional logging wrapper
const debugLog = (...args) => {
if (DEBUG_ENABLED) {
console.log('[ComponentName]', ...args);
}
};
// Load DEBUG setting from server
async function loadDebugConfig() {
const response = await fetch('/swissarmyknife/config');
const config = await response.json();
DEBUG_ENABLED = config.debug || false;
}
```
**Files Updated:**
- `web/js/swiss-army-knife.js` - Main extension debug system
- `web/js/video_preview/video_preview.js` - Video preview widget debug logging
## Configuration
### 1. Create `.env` file
```bash
cp .env.example .env
```
### 2. Set DEBUG variable
Edit `.env`:
```bash
DEBUG=true # Enable debug logging
```
### 3. Restart ComfyUI Server
Debug settings are loaded at startup, so you must **restart the ComfyUI server** after changing the DEBUG variable:
```bash
# Stop ComfyUI
# Restart ComfyUI
```
### 4. Refresh Browser
After restarting the server, **hard refresh your browser** to load the new debug configuration:
- **Mac**: `Cmd + Shift + R`
- **Windows/Linux**: `Ctrl + Shift + R`
- **Or**: Open DevTools → Right-click refresh → "Empty Cache and Hard Reload"
## Benefits
### ✅ Single Source of Truth
- One environment variable controls all debug logging
- No need to edit multiple files to enable/disable debug mode
### ✅ Production Ready
- Debug logging disabled by default
- No performance impact in production
- Clean console output without debug noise
### ✅ Developer Friendly
- Easy to enable debug mode for troubleshooting
- Consistent debug output format with prefixes
- Works across both Python and JavaScript
### ✅ Conditional Logging
- Only logs when DEBUG=true
- Avoids cluttering production logs
- Easy to trace issues in development
## Debug Output Examples
### Python Debug Output
```
[DEBUG] LoRAInfoExtractor.extract_lora_info called
- use_civitai_api: True
- civitai_api_key provided: True
- fallback_name: 'my_lora'
[DEBUG] Discovered 3 LoRA entries in stack
```
### JavaScript Debug Output
```
Video Preview Debug Mode: ENABLED
[VideoPreview] reference_vid raw value: /comfyui-nvidia/temp/video.mp4 string
[VideoPreview] Loaded reference_vid:
Extracted: /comfyui-nvidia/temp/video.mp4
Type: temp
Subfolder: (none)
Filename: video.mp4
URL: /api/view?filename=video.mp4&type=temp&subfolder=
```
## Migration Guide
### For Python Code
**Before:**
```python
print("[DEBUG] Some debug message")
print(f"[DEBUG] Value: {value}")
```
**After:**
```python
from nodes.debug_utils import debug_print
debug_print("[DEBUG] Some debug message")
debug_print(f"[DEBUG] Value: {value}")
```
### For JavaScript Code
**Before:**
```javascript
const DEBUG = true;
if (DEBUG) {
console.log('[Component]', 'debug message');
}
```
**After:**
```javascript
let DEBUG_ENABLED = false;
const debugLog = (...args) => {
if (DEBUG_ENABLED) {
console.log('[Component]', ...args);
}
};
async function loadDebugConfig() {
const response = await fetch('/swissarmyknife/config');
const config = await response.json();
DEBUG_ENABLED = config.debug || false;
}
loadDebugConfig();
// Use debugLog instead of console.log
debugLog('debug message');
```
## Files to Update (Future Work)
The following files still contain hardcoded debug logging that should be migrated to use `debug_utils.py`:
### Python Files (Not Yet Migrated)
- `nodes/nodes.py` - LoRA extraction debug logs
- `nodes/civitai_service.py` - CivitAI API debug logs
- `nodes/lora_hash_cache.py` - Cache operation logs
- `nodes/cache.py` - Cache file logs
- `nodes/utils/video_preview.py` - Video preview logs
- `nodes/utils/control_panel.py` - Control panel logs
- `nodes/lora_manager/*.py` - LoRA manager debug logs
### Migration Priority
1. **High Priority**: Core nodes (`nodes.py`, `civitai_service.py`)
2. **Medium Priority**: Utility modules (`cache.py`, `lora_hash_cache.py`)
3. **Low Priority**: Utility nodes (`control_panel.py`, `video_preview.py`)
## Troubleshooting
### Debug logs not appearing after enabling DEBUG
**Solution:**
1. Verify `.env` file exists and contains `DEBUG=true`
2. **Restart ComfyUI server** (required)
3. **Hard refresh browser** (Cmd+Shift+R)
4. Check console for: `Swiss Army Knife Debug Mode: ENABLED`
5. Check console for: `Video Preview Debug Mode: ENABLED`
### Environment variable not loading
**Solution:**
1. Ensure `.env` file is in the extension root directory
2. Check file permissions (should be readable)
3. Verify ComfyUI loads `.env` files (may need python-dotenv)
4. Try setting environment variable directly before starting ComfyUI:
```bash
export DEBUG=true
# Start ComfyUI
```
### Debug mode enabled but no logs
**Solution:**
1. Check that code is using `debug_print()` or `debugLog()`
2. Verify `/swissarmyknife/config` endpoint returns `{"debug": true}`
3. Check browser console and server logs separately
## API Endpoint
### GET `/swissarmyknife/config`
Returns the current debug configuration:
```json
{
"debug": true
}
```
This endpoint is called by JavaScript extensions to synchronize debug settings with the backend.
## Future Enhancements
1. **Dynamic Debug Toggle**: Add UI button to toggle debug mode without restarting
2. **Debug Levels**: Support different debug levels (INFO, DEBUG, TRACE)
3. **Component Filtering**: Enable debug for specific components only
4. **Log File Output**: Option to write debug logs to file
5. **Performance Metrics**: Track and display performance data in debug mode
## Related Documentation
- `.env.example` - Environment variable configuration template
- `nodes/debug_utils.py` - Python debug utilities implementation
- `nodes/config_api.py` - API endpoint for debug configuration
- `web/js/swiss-army-knife.js` - JavaScript debug system implementation
|