Skip to content

Commit

Permalink
fix: log
Browse files Browse the repository at this point in the history
  • Loading branch information
Keyrxng committed Jun 27, 2024
1 parent 7faf6f3 commit 5a25b29
Showing 1 changed file with 30 additions and 31 deletions.
61 changes: 30 additions & 31 deletions types/logs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,45 +65,44 @@ export class PrettyLogs {
}

private _logWithStack(type: PrettyLogsWithOk, message: string, metaData?: Metadata | string) {
if (typeof metaData === "string") {
if (!metaData) {
this._log(type, message);
return;
} else if (typeof metaData === "string") {
this._log(type, `${message} - ${metaData}`);
return;
} else if (metaData && typeof metaData === "object" && !(metaData.error || metaData?.stack)) {
} else if (typeof metaData === "object" && !(metaData.error || metaData?.stack)) {
this._log(type, `${message} ${!this._isEmpty(metaData) ? JSON.stringify(metaData, null, 2) : ""}`);
return;
}

const metadata = metaData as MetadataInterface | undefined;

if (metadata) {
let stack = metadata?.error?.stack || metadata?.stack;
if (!stack && this.levels[type] >= this.levels.error) {
// generate and remove the top four lines of the stack trace
const stackTrace = this.getStackTrace();
if (stackTrace) {
stackTrace.splice(0, 4);
stack = stackTrace.filter((line) => line.includes(".ts:")).join("\n");
}
}

const newMetadata = { ...metadata };
delete newMetadata.message;
delete newMetadata.name;
delete newMetadata.stack;

if (!this._isEmpty(newMetadata)) {
this._log(type, newMetadata);
}

if (stack && typeof stack == "string") {
const prettyStack = this._formatStackTrace(stack, 1);
const colorizedStack = this._colorizeText(prettyStack, COLORS.dim);
this._log(type, colorizedStack);
} else if (stack && Array.isArray(stack)) {
const prettyStack = this._formatStackTrace((stack as unknown as string[]).join("\n"), 1);
const colorizedStack = this._colorizeText(prettyStack, COLORS.dim);
this._log(type, colorizedStack);
}
let stack = metadata?.error?.stack || metadata?.stack;
// generate and remove the top four lines of the stack trace
const stackTrace = this.getStackTrace();
if (stackTrace) {
stackTrace.splice(0, 4);
stack = stackTrace.filter((line) => line.includes(".ts:")).join("\n");
}

const newMetadata = { ...metadata };
delete newMetadata.message;
delete newMetadata.name;
delete newMetadata.stack;

if (!this._isEmpty(newMetadata)) {
this._log(type, newMetadata);
}

if (stack && typeof stack == "string") {
const prettyStack = this._formatStackTrace(stack, 1);
const colorizedStack = this._colorizeText(prettyStack, COLORS.dim);
this._log(type, colorizedStack);
} else if (stack && Array.isArray(stack)) {
const prettyStack = this._formatStackTrace((stack as unknown as string[]).join("\n"), 1);
const colorizedStack = this._colorizeText(prettyStack, COLORS.dim);
this._log(type, colorizedStack);
}
}

Expand Down

0 comments on commit 5a25b29

Please sign in to comment.