From cdd9bee783c5fbe109d80138d7265f5973eb1e50 Mon Sep 17 00:00:00 2001 From: morizon Date: Mon, 2 Dec 2024 18:19:54 +0800 Subject: [PATCH] fix: mute debug log warnings (#259) --- packages/core/src/loggers.ts | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/packages/core/src/loggers.ts b/packages/core/src/loggers.ts index 9d6be427..8bfaa373 100644 --- a/packages/core/src/loggers.ts +++ b/packages/core/src/loggers.ts @@ -1,9 +1,9 @@ /* eslint-disable @typescript-eslint/ban-ts-comment */ -import { IDebugLogger, ConsoleLike } from '@onekeyfe/cross-inpage-provider-types'; +import { ConsoleLike, IDebugLogger } from '@onekeyfe/cross-inpage-provider-types'; // @ts-ignore -import createDebugAsync from './debug'; import { DEBUG_LOGGER_STORAGE_KEY } from './consts'; import { CrossEventEmitter } from './CrossEventEmitter'; +import createDebugAsync from './debug'; // enable debugLogger: // localStorage.setItem('$$ONEKEY_DEBUG_LOGGER', '*'); @@ -41,6 +41,7 @@ class FakeDebugLogger extends CrossEventEmitter implements IDebugLogger { constructor() { super(); this.initExternalLogInstances(); + this.setMaxListeners(9999); } jsBridge = (...args: any[]) => null; @@ -58,6 +59,8 @@ class FakeDebugLogger extends CrossEventEmitter implements IDebugLogger { }); } + isFaked = true; + _debug = { enable(config: string) { //noop @@ -80,12 +83,14 @@ class FakeDebugLogger extends CrossEventEmitter implements IDebugLogger { (name: string) => (...args: any[]) => { this.once('debugReady', () => { - // @ts-ignore - // eslint-disable-next-line @typescript-eslint/no-unsafe-call - this[name]?.(...args); + if (!this.isFaked) { + // @ts-ignore + // eslint-disable-next-line @typescript-eslint/no-unsafe-call + this[name]?.(...args); + } }); // eslint-disable-next-line @typescript-eslint/no-unsafe-argument - return this._externalLogger?.log?.(`${name } >>> `, ...args); + return this._externalLogger?.log?.(`${name} >>> `, ...args); }; } @@ -95,11 +100,14 @@ class AppDebugLogger extends FakeDebugLogger { // TODO createDebugSync void createDebugAsync().then((debug) => { this._debug = debug; + this.isFaked = false; this.initDebugInstances(); this.emit('debugReady'); }); } + isFaked = false; + initDebugInstances() { if (this.isDebugReady()) { Object.keys(loggerNames).forEach((name) => { @@ -142,4 +150,4 @@ class AppDebugLogger extends FakeDebugLogger { const fakeDebugLogger: IDebugLogger = new FakeDebugLogger(); const appDebugLogger: IDebugLogger = new AppDebugLogger(); -export { fakeDebugLogger, appDebugLogger, fakeLogger, consoleErrorInDev }; +export { appDebugLogger, consoleErrorInDev, fakeDebugLogger, fakeLogger };