Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure Metadata is Properly Passed to LogReturn in Logging Class #48

Closed
Closed
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@ export const COLORS = {
} as const;

export const LOG_LEVEL = {
FATAL: "fatal",
ERROR: "error",
INFO: "info",
VERBOSE: "verbose",
DEBUG: "debug",
FATAL: 'fatal',
ERROR: 'error',
INFO: 'info',
VERBOSE: 'verbose',
DEBUG: 'debug',
TRACE: 'trace', // New log level
} as const;
41 changes: 27 additions & 14 deletions src/logs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,17 @@ export class Logs {
type: "verbose",
});
}

public trace(log: string, metadata?: Metadata): LogReturn {
metadata = this._addDiagnosticInformation(metadata);
return this._log({
level: LOG_LEVEL.TRACE,
consoleLog: Logs.console.trace,
logMessage: log,
metadata,
type: "trace",
});
}

constructor(logLevel: LogLevel) {
this._maxLevel = this._getNumericLevel(logLevel);
Expand Down Expand Up @@ -168,21 +179,23 @@ export class Logs {
}

private _getNumericLevel(level: LogLevel) {
switch (level) {
case LOG_LEVEL.FATAL:
return 0;
case LOG_LEVEL.ERROR:
return 1;
case LOG_LEVEL.INFO:
return 2;
case LOG_LEVEL.VERBOSE:
return 4;
case LOG_LEVEL.DEBUG:
return 5;
default:
return -1;
}
switch (level) {
case LOG_LEVEL.FATAL:
return 0;
case LOG_LEVEL.ERROR:
return 1;
case LOG_LEVEL.INFO:
return 2;
case LOG_LEVEL.VERBOSE:
return 4;
case LOG_LEVEL.DEBUG:
return 5;
case LOG_LEVEL.TRACE: // New log level
return 6; // Assign a higher numeric value
default:
return -1;
}
}
static convertErrorsIntoObjects(obj: unknown): Metadata | unknown {
// this is a utility function to render native errors in the console, the database, and on GitHub.
if (obj instanceof Error) {
Expand Down
9 changes: 8 additions & 1 deletion src/pretty-logs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export class PrettyLogs {
this.fatal = this.fatal.bind(this);
this.debug = this.debug.bind(this);
this.verbose = this.verbose.bind(this);
this.trace = this.trace.bind(this);
}
public fatal(message: string, metadata?: Metadata | string | unknown) {
this._logWithStack(LOG_LEVEL.FATAL, message, metadata);
Expand All @@ -33,6 +34,10 @@ export class PrettyLogs {
public verbose(message: string, metadata?: Metadata | string) {
this._logWithStack(LOG_LEVEL.VERBOSE, message, metadata);
}

public trace(message: string, metadata?: Metadata | string) {
this._logWithStack(LOG_LEVEL.TRACE, message, metadata);
}

private _logWithStack(type: LogLevelWithOk, message: string, metaData?: Metadata | string | unknown) {
this._log(type, message);
Expand Down Expand Up @@ -103,6 +108,7 @@ export class PrettyLogs {
info: "›",
debug: "››",
verbose: "💬",
trace: "🔍",
};

const symbol = defaultSymbols[type];
Expand All @@ -127,7 +133,8 @@ export class PrettyLogs {
error: ["warn", COLORS.fgYellow],
info: ["info", COLORS.dim],
debug: ["debug", COLORS.fgMagenta],
verbose: ["debug", COLORS.dim],
verbose: ["verbose", COLORS.dim],
trace: ["trace", COLORS.fgBlue],
};

const _console = console[colorMap[type][0] as keyof typeof console] as (...args: string[]) => void;
Expand Down
Loading