Skip to content

Commit e4e6f9f

Browse files
feat: implement centralized debug logging system - Add Debug utility for conditional logging based on DEBUG_MODE config - Replace console.log/error calls with Debug.log/error throughout frontend - Update app.js, chat.js, lazyload.js, and chat service to use new debug system - Enhance config README with debug utility documentation - Improve production readiness with configurable logging
1 parent de5cea1 commit e4e6f9f

7 files changed

Lines changed: 92 additions & 15 deletions

File tree

frontend/app.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { storage, KEYS } from './utils/storage/storage';
22
import CONFIG from './config/config';
3+
import Debug from './utils/debug/debug';
34
// app.js
45
import chatService from './services/chat/chat';
56
import lazyLoader from './utils/lazyload/lazyload';
@@ -12,7 +13,7 @@ App({
1213
},
1314
onLaunch() {
1415
// Initialize the app
15-
console.log('App launched');
16+
Debug.log('App launched');
1617

1718
// Enable web sockets
1819
chatService.setMode(true); // Enable WebSocket mode globally
@@ -33,7 +34,7 @@ App({
3334

3435
onHide() {
3536
// Clean up lazy loader when app goes to background
36-
console.log('App hidden, cleaning up lazy loader');
37+
Debug.log('App hidden, cleaning up lazy loader');
3738
},
3839

3940
onUnload() {

frontend/config/README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ const wsUrl = CONFIG.getWebSocketUrl();
2222
// Access other settings
2323
const debugMode = CONFIG.APP.DEBUG_MODE;
2424
const maxMessageLength = CONFIG.CHAT.MAX_MESSAGE_LENGTH;
25+
26+
// Use Debug utility for conditional logging
27+
import Debug from '../utils/debug/debug';
28+
Debug.log('This only shows when DEBUG_MODE is true');
29+
Debug.error('This only shows when DEBUG_MODE is true');
30+
Debug.critical('This always shows, even in production');
2531
```
2632

2733
## Environment Configuration
@@ -40,6 +46,9 @@ To switch between development and production:
4046
// Production
4147
BASE_URL: 'https://your-production-domain.com/api',
4248
WS_URL: 'wss://your-production-domain.com/ws/chat',
49+
50+
// Also remember to disable debug mode for production
51+
DEBUG_MODE: false,
4352
```
4453

4554
## Benefits

frontend/config/config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ const CONFIG = {
2020
// App Configuration
2121
APP: {
2222
VERSION: '1.0.0',
23-
DEBUG_MODE: true, // Set to false for production
23+
DEBUG_MODE: true, // Set to false for production - controls console logging
2424
SESSION_TIMEOUT: 30 * 60 * 1000, // 30 minutes in milliseconds
2525
},
2626

frontend/pages/chat/chat.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import chatService from '../../services/chat/chat';
22
import { storage, KEYS } from '../../utils/storage/storage';
33
import { formatTime } from '../../utils/request/request';
44
import lazyLoader from '../../utils/lazyload/lazyload';
5+
import Debug from '../../utils/debug/debug';
56

67
Page({
78
data: {
@@ -179,7 +180,7 @@ Page({
179180
// Chat components loaded successfully
180181
});
181182
} catch (error) {
182-
console.error('[Chat] Lazy loading initialization error:', error);
183+
Debug.error('[Chat] Lazy loading initialization error:', error);
183184
}
184185
},
185186

@@ -214,7 +215,7 @@ Page({
214215
this.scrollToBottom();
215216
}
216217
} catch (error) {
217-
console.error('Failed to load chat history:', error);
218+
Debug.error('Failed to load chat history:', error);
218219
this.setData({ loading: false });
219220
}
220221
},

frontend/services/chat/chat.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import api from '../api/api';
22
import wsChat from './ws_chat';
3+
import Debug from '../../utils/debug/debug';
34

45
class UnifiedChatService {
56
constructor() {
@@ -74,7 +75,7 @@ class UnifiedChatService {
7475
assistantMessage
7576
};
7677
} catch (error) {
77-
console.error('Error sending message:', error);
78+
Debug.error('Error sending message:', error);
7879
throw error;
7980
}
8081
}

frontend/utils/debug/debug.js

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import CONFIG from '../../config/config';
2+
3+
/**
4+
* Debug utility that respects the DEBUG_MODE setting
5+
* Only logs to console when DEBUG_MODE is true
6+
*/
7+
class Debug {
8+
static log(...args) {
9+
if (CONFIG.APP.DEBUG_MODE) {
10+
console.log(...args);
11+
}
12+
}
13+
14+
static error(...args) {
15+
if (CONFIG.APP.DEBUG_MODE) {
16+
console.error(...args);
17+
}
18+
}
19+
20+
static warn(...args) {
21+
if (CONFIG.APP.DEBUG_MODE) {
22+
console.warn(...args);
23+
}
24+
}
25+
26+
static info(...args) {
27+
if (CONFIG.APP.DEBUG_MODE) {
28+
console.info(...args);
29+
}
30+
}
31+
32+
// Always show critical errors regardless of debug mode
33+
static critical(...args) {
34+
console.error('[CRITICAL]', ...args);
35+
}
36+
37+
// Performance timing (only in debug mode)
38+
static time(label) {
39+
if (CONFIG.APP.DEBUG_MODE) {
40+
console.time(label);
41+
}
42+
}
43+
44+
static timeEnd(label) {
45+
if (CONFIG.APP.DEBUG_MODE) {
46+
console.timeEnd(label);
47+
}
48+
}
49+
50+
// Group logging for better organization
51+
static group(label) {
52+
if (CONFIG.APP.DEBUG_MODE) {
53+
console.group(label);
54+
}
55+
}
56+
57+
static groupEnd() {
58+
if (CONFIG.APP.DEBUG_MODE) {
59+
console.groupEnd();
60+
}
61+
}
62+
}
63+
64+
export default Debug;

frontend/utils/lazyload/lazyload.js

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
* Lazy Loading Utility for WeChat Mini Program
33
* Handles component, image, and data lazy loading
44
*/
5+
import Debug from '../debug/debug';
56

67
class LazyLoader {
78
constructor() {
@@ -43,7 +44,7 @@ class LazyLoader {
4344
markComponentLoaded(componentName) {
4445
this.loadedComponents.add(componentName);
4546
if (this.debugMode) {
46-
console.log(`[LazyLoader] Component loaded: ${componentName}`);
47+
Debug.log(`[LazyLoader] Component loaded: ${componentName}`);
4748
}
4849
}
4950

@@ -60,13 +61,13 @@ class LazyLoader {
6061
query.selectAll(selector).boundingClientRect((rects) => {
6162
if (!rects || rects.length === 0) {
6263
if (this.debugMode) {
63-
console.log(`[LazyLoader] No elements found for selector "${selector}". Skipping image lazy loading.`);
64+
Debug.log(`[LazyLoader] No elements found for selector "${selector}". Skipping image lazy loading.`);
6465
}
6566
return;
6667
}
6768

6869
if (this.debugMode) {
69-
console.log(`[LazyLoader] Found ${rects.length} elements for lazy loading with selector "${selector}"`);
70+
Debug.log(`[LazyLoader] Found ${rects.length} elements for lazy loading with selector "${selector}"`);
7071
}
7172

7273
// Create intersection observer for images only if elements exist
@@ -118,7 +119,7 @@ class LazyLoader {
118119
}
119120
},
120121
fail: (err) => {
121-
console.error('[LazyLoader] Image lazy load failed:', err);
122+
Debug.error('[LazyLoader] Image lazy load failed:', err);
122123
// Set fallback or error state
123124
const updateKey = `images.${imageId}`;
124125
context.setData({
@@ -150,7 +151,7 @@ class LazyLoader {
150151
this.setCache(cacheKey, data);
151152
return data;
152153
} catch (error) {
153-
console.error('[LazyLoader] Data lazy load failed:', error);
154+
Debug.error('[LazyLoader] Data lazy load failed:', error);
154155
throw error;
155156
}
156157
}
@@ -166,7 +167,7 @@ class LazyLoader {
166167
}
167168
} catch (e) {
168169
if (this.debugMode) {
169-
console.error('[LazyLoader] Cache read error:', e);
170+
Debug.error('[LazyLoader] Cache read error:', e);
170171
}
171172
}
172173
return null;
@@ -180,7 +181,7 @@ class LazyLoader {
180181
});
181182
} catch (e) {
182183
if (this.debugMode) {
183-
console.error('[LazyLoader] Cache write error:', e);
184+
Debug.error('[LazyLoader] Cache write error:', e);
184185
}
185186
}
186187
}
@@ -211,7 +212,7 @@ class LazyLoader {
211212
this.initImageLazyLoad(selector, context);
212213
} else {
213214
if (this.debugMode) {
214-
console.log(`[LazyLoader] No elements found for "${selector}". Image lazy loading skipped.`);
215+
Debug.log(`[LazyLoader] No elements found for "${selector}". Image lazy loading skipped.`);
215216
}
216217
}
217218
}
@@ -223,7 +224,7 @@ class LazyLoader {
223224
setDebugMode(enabled) {
224225
this.debugMode = enabled;
225226
if (enabled) {
226-
console.log('[LazyLoader] Debug mode enabled');
227+
Debug.log('[LazyLoader] Debug mode enabled');
227228
}
228229
}
229230

0 commit comments

Comments
 (0)