From 869ba46522ca26d8e209e3070a3a11c2c7e8f527 Mon Sep 17 00:00:00 2001 From: unadlib Date: Wed, 18 Dec 2024 10:31:33 +0800 Subject: [PATCH] fix(logger): add logger ignoreRule --- packages/logger/src/transports/console.ts | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/packages/logger/src/transports/console.ts b/packages/logger/src/transports/console.ts index fff54ab..96e544f 100644 --- a/packages/logger/src/transports/console.ts +++ b/packages/logger/src/transports/console.ts @@ -14,16 +14,34 @@ export class ConsoleTransport implements ITransport { protected _consoleWrite: LogWriter; + protected _ignoreRules: RegExp[] = []; + constructor( protected _options?: { + /** + * By default the console transport is disabled. + */ enabled?: boolean; + /** + * By default all logs are displayed. + */ filter?: string; + /** + * The storage to save the log. + */ storage?: Storage; + /** + * Ignore logs that match the rules. + */ + ignoreRule?: string[]; } ) { this._consoleWrite = createLogWriter({ storage: this._options?.storage, }); + this._ignoreRules = (this._options?.ignoreRule ?? []).map( + (rule) => new RegExp(rule) + ); } protected get _storage() { @@ -48,6 +66,7 @@ export class ConsoleTransport implements ITransport { } write({ message }: SerializedMessage) { + if (this._ignoreRules.some((regex) => regex.test(message))) return; if (this._options?.enabled) { this._consoleWrite(message); }