From 6b98ba90cf3a1e6e473f45f24c2ac0cb78aa7d38 Mon Sep 17 00:00:00 2001 From: unanmed <1319491857@qq.com> Date: Thu, 21 Nov 2024 21:27:02 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20logger=E7=9A=84catch=E5=85=81=E8=AE=B8?= =?UTF-8?q?=E5=B5=8C=E5=A5=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/core/common/logger.ts | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/core/common/logger.ts b/src/core/common/logger.ts index c61ec82..ac28a07 100644 --- a/src/core/common/logger.ts +++ b/src/core/common/logger.ts @@ -1,8 +1,6 @@ import { debounce } from 'lodash-es'; import logInfo from '../../data/logger.json'; -// todo: 使用格式化输出? - export const enum LogLevel { /** 输出所有,包括日志 */ LOG, @@ -58,6 +56,8 @@ export class Logger { private catching: boolean = false; private catchedInfo: LoggerCatchInfo[] = []; + private catchStack: LoggerCatchInfo[][] = []; + constructor(logLevel: LogLevel) { this.level = logLevel; } @@ -182,13 +182,19 @@ export class Logger { catch(fn: () => T): LoggerCatchReturns { const before = this.enabled; this.catchedInfo = []; + this.catchStack.push(this.catchedInfo); this.disable(); this.catching = true; const ret = fn(); this.catching = false; if (before) this.enable(); - return { ret, info: this.catchedInfo }; + this.catchStack.pop(); + const last = this.catchStack?.at(-1); + const info = this.catchedInfo; + this.catchedInfo = last ?? []; + + return { ret, info }; } disable() {