feat: logger的catch允许嵌套

This commit is contained in:
unanmed 2024-11-21 21:27:02 +08:00
parent 21a4f62dcb
commit 6b98ba90cf

View File

@ -1,8 +1,6 @@
import { debounce } from 'lodash-es'; import { debounce } from 'lodash-es';
import logInfo from '../../data/logger.json'; import logInfo from '../../data/logger.json';
// todo: 使用格式化输出?
export const enum LogLevel { export const enum LogLevel {
/** 输出所有,包括日志 */ /** 输出所有,包括日志 */
LOG, LOG,
@ -58,6 +56,8 @@ export class Logger {
private catching: boolean = false; private catching: boolean = false;
private catchedInfo: LoggerCatchInfo[] = []; private catchedInfo: LoggerCatchInfo[] = [];
private catchStack: LoggerCatchInfo[][] = [];
constructor(logLevel: LogLevel) { constructor(logLevel: LogLevel) {
this.level = logLevel; this.level = logLevel;
} }
@ -182,13 +182,19 @@ export class Logger {
catch<T>(fn: () => T): LoggerCatchReturns<T> { catch<T>(fn: () => T): LoggerCatchReturns<T> {
const before = this.enabled; const before = this.enabled;
this.catchedInfo = []; this.catchedInfo = [];
this.catchStack.push(this.catchedInfo);
this.disable(); this.disable();
this.catching = true; this.catching = true;
const ret = fn(); const ret = fn();
this.catching = false; this.catching = false;
if (before) this.enable(); 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() { disable() {