Skip to content

Commit bc175bb

Browse files
committed
feat(extension): implement development-only logger with debug toggle
- Create DevLogger utility to eliminate console spam in production - Add 10-click debug activation (click floating sphere logo 10 times) - Replace console.log with logger.debug() throughout extension - Support multiple debug activation methods (localStorage, URL params, etc.) - Maintain clean console for users while preserving debug capabilities - Reorganize logger code for better maintainability Files changed: - NEW: apps/extension/src/utils/logger.ts - Core logger utility - Modified: floatingSphere App.tsx - Added debug click handler - Modified: Various background events - Replaced console logging - Modified: Content selector hooks - Replaced debug logging Fixes console spam issue and improves user experience.
1 parent 83e3fc6 commit bc175bb

File tree

12 files changed

+319
-27
lines changed

12 files changed

+319
-27
lines changed

apps/extension/src/components/content-selector/hooks/use-content-selector.tsx

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import { getElementType } from '../utils';
2222
import { genContentSelectorID } from '@refly/utils/id';
2323
import { getMarkdown } from '@refly/utils/html2md';
2424
import { BLOCK_SELECTED_MARK_ID, INLINE_SELECTED_MARK_ID } from '../utils/index';
25+
import { logger } from '../../../utils/logger';
2526

2627
// utils
2728
import { getSelectionNodesMarkdown } from '@refly/utils/html2md';
@@ -126,7 +127,7 @@ export const useContentSelector = (
126127
containerElem?.removeChild(menuContainer);
127128
document.head.removeChild(styleContainer);
128129
} catch (err) {
129-
console.log('remove err', err);
130+
logger.log('remove err', err);
130131
}
131132
}, 300);
132133
};
@@ -392,7 +393,7 @@ export const useContentSelector = (
392393
} as Mark,
393394
},
394395
};
395-
console.log('contentSelectorClickHandler', safeStringifyJSON(msg));
396+
logger.debug('contentSelectorClickHandler', safeStringifyJSON(msg));
396397
sendMessage(msg);
397398
};
398399

@@ -437,13 +438,13 @@ export const useContentSelector = (
437438
const onMouseMove = (ev: MouseEvent) => {
438439
ev.stopImmediatePropagation();
439440

440-
console.log('isMouseOutsideContainer', isMouseOutsideContainer(ev), selector);
441+
logger.debug('isMouseOutsideContainer', isMouseOutsideContainer(ev), selector);
441442

442443
if (isMouseOutsideContainer(ev)) {
443444
return;
444445
}
445446

446-
console.log('contentActionHandler', ev, statusRef, markRef, showContentSelectorRef);
447+
logger.debug('contentActionHandler', ev, statusRef, markRef, showContentSelectorRef);
447448
if (
448449
statusRef.current &&
449450
markRef.current &&
@@ -523,7 +524,7 @@ export const useContentSelector = (
523524
const selection = window.getSelection();
524525
const text = selection?.toString();
525526

526-
console.log('onMouseDownUpEvent');
527+
logger.debug('onMouseDownUpEvent');
527528

528529
if (statusRef.current && markRef.current && showContentSelectorRef.current) {
529530
if (text && text?.trim()?.length > 0) {
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import { sendHeartBeatMessage } from './utils';
22
import { Tabs } from 'wxt/browser';
3+
import { logger } from '../../../utils/logger';
34

45
export const onActivated = (activeInfo: Tabs.OnActivatedActiveInfoType) => {
56
// 在此处处理标签切换
6-
console.log(`Tab with ID ${activeInfo.tabId} was activated in window ${activeInfo.windowId}`);
7+
logger.debug(`Tab with ID ${activeInfo.tabId} was activated in window ${activeInfo.windowId}`);
78

89
sendHeartBeatMessage(activeInfo);
910
};
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Tabs } from 'wxt/browser';
2+
import { logger } from '../../../utils/logger';
23

34
export const onDetached = (tabId: number, detachInfo: Tabs.OnDetachedDetachInfoType) => {
45
// 在此处处理标签切换
5-
console.log(`Tab with ID ${tabId} was detached in window ${detachInfo.oldWindowId}`);
6+
logger.debug(`Tab with ID ${tabId} was detached in window ${detachInfo.oldWindowId}`);
67
};

apps/extension/src/entrypoints/background/events/ports.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@ import { TASK_STATUS, SkillEvent } from '@refly/common-types';
33
import { getCookie } from '@/utils/cookie';
44
import { ssePost } from '@refly-packages/ai-workspace-common/utils/sse-post';
55
import { getAbortController, getLastUniqueId, setAbortController, setLastUniqueId } from '../index';
6+
import { logger } from '../../../utils/logger';
67

78
export const handleStreamingChat = async (
89
req: { body: any; uniqueId: string },
910
res: { send: (response?: any) => void },
1011
) => {
1112
const { type, payload } = req?.body || {};
1213
const { uniqueId } = req;
13-
console.log('receive request', req.body);
14+
logger.debug('receive request', req.body);
1415
setLastUniqueId(uniqueId);
1516

1617
try {
@@ -76,7 +77,7 @@ export const handleStreamingChat = async (
7677
},
7778
});
7879

79-
console.log('after abortController', abortController);
80+
logger.debug('after abortController', abortController);
8081
} else if (type === TASK_STATUS.SHUTDOWN) {
8182
const abortController = getAbortController(uniqueId);
8283

@@ -85,21 +86,21 @@ export const handleStreamingChat = async (
8586
}
8687
}
8788
} catch (err) {
88-
console.log('err', err);
89+
logger.error('streaming chat error', err);
8990
try {
9091
const abortController = getAbortController(uniqueId);
9192
if (!abortController?.signal?.aborted) {
9293
abortController?.abort?.();
9394
}
9495
} catch (err) {
95-
console.log('err', err);
96+
logger.error('abort controller cleanup error', err);
9697
}
9798
}
9899
};
99100

100101
export const onPort = async (port: Runtime.Port) => {
101102
port.onMessage.addListener(async (message: any, comingPort: Runtime.Port) => {
102-
console.log('onPort', message, comingPort);
103+
logger.debug('onPort', message, comingPort);
103104
if (comingPort.name === 'streaming-chat') {
104105
return handleStreamingChat(message, {
105106
send: async (msg: any) => {
@@ -115,7 +116,7 @@ export const onPort = async (port: Runtime.Port) => {
115116
try {
116117
abortController?.abort?.();
117118
} catch (err) {
118-
console.log('err', err);
119+
logger.error('port disconnect cleanup error', err);
119120
}
120121
});
121122
};

apps/extension/src/entrypoints/contentSelector-csui.content/index.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { defineContentScript } from 'wxt/sandbox';
33
import { createShadowRootUi } from 'wxt/client';
44
import { App } from './App';
55
import { setRuntime } from '@refly/utils/env';
6+
import { logger } from '@/utils/logger';
67

78
export default defineContentScript({
89
matches: ['<all_urls>'],
@@ -16,7 +17,7 @@ export default defineContentScript({
1617

1718
async main(ctx) {
1819
setRuntime('extension-csui');
19-
console.log('ctx', ctx);
20+
logger.debug('contentSelector ctx', ctx);
2021

2122
const injectSelectorCSS = () => {
2223
const style = document.createElement('style');

apps/extension/src/entrypoints/floatingSphere-csui.content/App.tsx

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import { useSaveSelectedContent } from '@/hooks/use-save-selected-content';
2222
import { BackgroundMessage, SyncMarkEvent, type MessageName } from '@refly/common-types';
2323
import { useGetUserSettings } from '@/hooks/use-get-user-settings';
2424
import { useUserStore } from '@refly-packages/ai-workspace-common/stores/user';
25+
import { logger } from '@/utils/logger';
2526

2627
const getPopupContainer = () => {
2728
const elem = document
@@ -54,7 +55,7 @@ export const App = () => {
5455
getLoginStatus();
5556
}, []);
5657

57-
console.log('i18n', i18n?.languages);
58+
logger.debug('i18n', i18n?.languages);
5859

5960
// 加载快捷键
6061
const [_shortcut] = useState<string>(reflyEnv.getOsType() === 'OSX' ? '⌘ J' : 'Ctrl J');
@@ -229,7 +230,7 @@ export const App = () => {
229230
icon={<IconSave />}
230231
size="small"
231232
onClick={() => {
232-
console.log('saveResource', saveResource);
233+
logger.debug('saveResource', saveResource);
233234
handleSaveResourceAndNotify(saveResource);
234235
}}
235236
className="refly-floating-sphere-dropdown-item assist-action-item"
@@ -274,7 +275,7 @@ export const App = () => {
274275
};
275276

276277
const handleMouseUp = () => {
277-
console.log('handleMouseUp');
278+
logger.debug('handleMouseUp');
278279
setIsDragging(false);
279280
isDraggingRef.current = false;
280281
};
@@ -294,6 +295,18 @@ export const App = () => {
294295

295296
const [isVisible, setIsVisible] = useState(true);
296297

298+
const handleDebugClick = useCallback(() => {
299+
const showMessage = (content: string, type: 'info' | 'success' = 'info') => {
300+
if (type === 'success') {
301+
Message.success({ content, duration: 4000 });
302+
} else {
303+
Message.info({ content, duration: 2000 });
304+
}
305+
};
306+
307+
logger.handleDebugClick(showMessage);
308+
}, []);
309+
297310
const handleClose = (e: React.MouseEvent) => {
298311
Message.info({
299312
content: t('extension.floatingSphere.toggleCopilotClose'),
@@ -357,7 +370,8 @@ export const App = () => {
357370
<img
358371
src={Logo}
359372
alt={t('extension.floatingSphere.toggleCopilot')}
360-
style={{ width: 25, height: 25 }}
373+
style={{ width: 25, height: 25, cursor: 'pointer' }}
374+
onClick={handleDebugClick}
361375
/>
362376
<span className="refly-floating-sphere-entry-shortcut">Refly</span>
363377
</div>

apps/extension/src/entrypoints/floatingSphere-csui.content/index.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { setRuntime } from '@refly/utils/env';
77
import { ConfigProvider } from 'antd';
88
import { MemoryRouter, Route } from '@refly-packages/ai-workspace-common/utils/router';
99
import { AppRouter } from '@/routes';
10+
import { logger } from '@/utils/logger';
1011

1112
export default defineContentScript({
1213
matches: ['<all_urls>'],
@@ -22,7 +23,7 @@ export default defineContentScript({
2223
async main(ctx) {
2324
setRuntime('extension-csui');
2425

25-
console.log('ctx', ctx);
26+
logger.debug('floatingSphere ctx', ctx);
2627
let _removeInjectCSS: () => void;
2728
// 3. Define your UI`
2829
const ui = await createShadowRootUi(ctx, {

apps/extension/src/entrypoints/utils-csui.content/index.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import ReactDOM from 'react-dom/client';
44
import { setRuntime } from '@refly/utils/env';
55
import { createShadowRootUi } from 'wxt/client';
66
import App from './App';
7+
import { logger } from '@/utils/logger';
78

89
export default defineContentScript({
910
matches: ['<all_urls>'],
@@ -19,7 +20,7 @@ export default defineContentScript({
1920
async main(ctx) {
2021
setRuntime('extension-csui');
2122

22-
console.log('ctx', ctx);
23+
logger.debug('utils-csui ctx', ctx);
2324
// 3. Define your UI`
2425
const ui = await createShadowRootUi(ctx, {
2526
name: 'refly-utils-app',

apps/extension/src/hooks/use-get-user-settings.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { getRuntime } from '@refly/utils/env';
1515
import { UserSettings } from '@refly/openapi-schema';
1616
import { browser } from 'wxt/browser';
1717
import debounce from 'lodash.debounce';
18+
import { logger } from '@/utils/logger';
1819

1920
export const useGetUserSettings = () => {
2021
const userStore = useUserStore((state) => ({
@@ -40,7 +41,7 @@ export const useGetUserSettings = () => {
4041
userStore.setIsCheckingLoginStatus(true);
4142
const res = await getClient().getSettings();
4243

43-
console.log('loginStatus', res);
44+
logger.debug('loginStatus', res);
4445

4546
if (!res?.error || !res) {
4647
userStore.resetState();
@@ -92,7 +93,7 @@ export const useGetUserSettings = () => {
9293
}
9394
}
9495
} catch (err) {
95-
console.log('getLoginStatus err', err);
96+
logger.debug('getLoginStatus err', err);
9697
userStore.setIsCheckingLoginStatus(false);
9798
userStore.resetState();
9899

apps/extension/src/hooks/use-storage.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { useEffect, useState } from 'react';
22
import { storage } from '@refly-packages/ai-workspace-common/utils/storage';
33
import { safeParseJSON } from '@refly-packages/ai-workspace-common/utils/parse';
4+
import { logger } from '@/utils/logger';
45

56
export type StorageLocation = 'local' | 'sync' | 'session' | 'managed';
67

@@ -29,7 +30,7 @@ export const useStorage = <T>(
2930

3031
useEffect(() => {
3132
storageItem.watch((newValue) => {
32-
console.log('new Syn storage value', newValue);
33+
logger.debug('new Syn storage value', newValue);
3334
setStorageValue(newValue as T);
3435
});
3536
}, []);

apps/extension/src/modules/toggle-copilot/hooks/use-toggle-copilot.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import pTimeout from 'p-timeout';
55
import { checkPageUnsupported } from '@refly-packages/ai-workspace-common/utils/extension/check';
66
import { checkBrowserArc } from '@/utils/browser';
77
import { useCopilotTypeStore } from '@/modules/toggle-copilot/stores/use-copilot-type';
8+
import { logger } from '@/utils/logger';
89

910
export const useToggleCopilot = () => {
1011
const { copilotType } = useCopilotTypeStore((state) => ({
@@ -22,9 +23,9 @@ export const useToggleCopilot = () => {
2223

2324
await handleCheckArcBrowser();
2425

25-
console.log('isArcBrowserRef.current', isArcBrowserRef.current, isCopilotOpen);
26+
logger.debug('isArcBrowserRef.current', isArcBrowserRef.current, isCopilotOpen);
2627
if (isArcBrowserRef.current) {
27-
console.log('sendMessage', 'toggleCopilotSidePanel');
28+
logger.debug('sendMessage', 'toggleCopilotCSUI');
2829
sendMessage({
2930
type: 'others',
3031
name: 'toggleCopilotCSUI',
@@ -35,7 +36,7 @@ export const useToggleCopilot = () => {
3536
source: getRuntime(),
3637
});
3738
} else {
38-
console.log('sendMessage', 'toggleCopilotSidePanel');
39+
logger.debug('sendMessage', 'toggleCopilotSidePanel');
3940
sendMessage({
4041
type: 'toggleCopilot',
4142
name: 'toggleCopilotSidePanel',
@@ -93,7 +94,7 @@ export const useToggleCopilot = () => {
9394
});
9495

9596
useEffect(() => {
96-
console.log('useToggleCopilot copilotType', copilotType);
97+
logger.debug('useToggleCopilot copilotType', copilotType);
9798
//
9899

99100
// Initial check if the page is already visible

0 commit comments

Comments
 (0)