From a8992b5ece5d5d5ed3c061c724d50edd2dbd1cea Mon Sep 17 00:00:00 2001 From: Keyrxng <106303466+Keyrxng@users.noreply.github.com> Date: Thu, 26 Sep 2024 10:14:02 +0100 Subject: [PATCH] feat: LogReturn extends Error --- package.json | 2 +- src/logs.ts | 14 +++++++------- src/types/log-types.ts | 3 ++- tests/logs.test.ts | 2 +- 4 files changed, 11 insertions(+), 10 deletions(-) diff --git a/package.json b/package.json index 960f7b0..0fd8f92 100644 --- a/package.json +++ b/package.json @@ -70,4 +70,4 @@ ] }, "packageManager": "yarn@1.22.22" -} \ No newline at end of file +} diff --git a/src/logs.ts b/src/logs.ts index 81b2848..6124418 100644 --- a/src/logs.ts +++ b/src/logs.ts @@ -6,19 +6,19 @@ import { LogParams, LogReturn, Metadata, LogLevel } from "./types/log-types"; type SupabaseConfig = { supabaseClient: SupabaseClient; levelsToLog: LogLevel[]; -} +}; export class Logs { private _maxLevel = -1; static console: PrettyLogs; private _supabase: SupabaseClient | null = null; - private pluginName: string; + private _pluginName: string; private _levelsToLog: LogLevel[] = []; constructor(level: LogLevel, pluginName: string, postingConfig?: SupabaseConfig) { this._maxLevel = this._getNumericLevel(level); Logs.console = new PrettyLogs(); - this.pluginName = pluginName; + this._pluginName = pluginName; if (postingConfig) { this._levelsToLog = postingConfig.levelsToLog; @@ -43,7 +43,7 @@ export class Logs { ); if (this._supabase && this._levelsToLog.includes(level)) { - this._logToSupabase(log); + this._logToSupabase(log).catch(this.error); } return log; @@ -118,14 +118,14 @@ export class Logs { public fatal(log: string, metadata?: Metadata): LogReturn { if (!metadata) { - metadata = Logs.convertErrorsIntoObjects(new Error(log)) + metadata = Logs.convertErrorsIntoObjects(new Error(log)); const stack = metadata.stack as string[]; stack.splice(1, 1); metadata.stack = stack; } if (metadata instanceof Error) { - metadata = Logs.convertErrorsIntoObjects(metadata) + metadata = Logs.convertErrorsIntoObjects(metadata); const stack = metadata.stack as string[]; stack.splice(1, 1); metadata.stack = stack; @@ -231,7 +231,7 @@ export class Logs { { log: log.logMessage.raw, level: log.logMessage.level, - metadata: { ...log.metadata, caller: this.pluginName }, + metadata: { ...log.metadata, caller: this._pluginName }, }, ]); if (error) { diff --git a/src/types/log-types.ts b/src/types/log-types.ts index 6f25618..78210e3 100644 --- a/src/types/log-types.ts +++ b/src/types/log-types.ts @@ -25,11 +25,12 @@ interface MetadataInterface { export type Metadata = Partial; -export class LogReturn { +export class LogReturn extends Error { logMessage: LogMessage; metadata?: Metadata; constructor(logMessage: LogMessage, metadata?: Metadata) { + super(logMessage.raw); this.logMessage = logMessage; this.metadata = metadata; } diff --git a/tests/logs.test.ts b/tests/logs.test.ts index df71546..383250d 100644 --- a/tests/logs.test.ts +++ b/tests/logs.test.ts @@ -6,7 +6,7 @@ describe("Logs", () => { let logs: Logs; beforeEach(() => { - logs = new Logs(LOG_LEVEL.DEBUG, "test") + logs = new Logs(LOG_LEVEL.DEBUG, "test"); }); it("should log an 'ok' message", () => {