Skip to content

Commit

Permalink
feat: LogReturn extends Error
Browse files Browse the repository at this point in the history
  • Loading branch information
Keyrxng committed Sep 26, 2024
1 parent fd34f13 commit a8992b5
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 10 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,4 @@
]
},
"packageManager": "[email protected]"
}
}
14 changes: 7 additions & 7 deletions src/logs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
3 changes: 2 additions & 1 deletion src/types/log-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,12 @@ interface MetadataInterface {

export type Metadata = Partial<MetadataInterface>;

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;
}
Expand Down
2 changes: 1 addition & 1 deletion tests/logs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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", () => {
Expand Down

0 comments on commit a8992b5

Please sign in to comment.