style: run format:apply
This commit is contained in:
+62
-63
@@ -7,93 +7,92 @@ import 'colorts/lib/string';
|
||||
helper fns
|
||||
*/
|
||||
function getCallerFrame(shift: number = 0): NodeJS.CallSite | undefined {
|
||||
const prepareStackTrace = Error.prepareStackTrace;
|
||||
let loggedStack: NodeJS.CallSite[] | undefined;
|
||||
const prepareStackTrace = Error.prepareStackTrace;
|
||||
let loggedStack: NodeJS.CallSite[] | undefined;
|
||||
|
||||
Error.prepareStackTrace = (_, stackTraces: NodeJS.CallSite[]) => {
|
||||
loggedStack = stackTraces;
|
||||
stackTraces.shift(); // discard curr frame
|
||||
return stackTraces;
|
||||
};
|
||||
Error.prepareStackTrace = (_, stackTraces: NodeJS.CallSite[]) => {
|
||||
loggedStack = stackTraces;
|
||||
stackTraces.shift(); // discard curr frame
|
||||
return stackTraces;
|
||||
};
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-expressions
|
||||
new Error().stack;
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-expressions
|
||||
new Error().stack;
|
||||
|
||||
Error.prepareStackTrace = prepareStackTrace;
|
||||
Error.prepareStackTrace = prepareStackTrace;
|
||||
|
||||
if (!loggedStack) return undefined;
|
||||
if (!loggedStack) return undefined;
|
||||
|
||||
if (shift > 0)
|
||||
for (let i = 0; i < shift; i++)
|
||||
loggedStack.shift();
|
||||
if (shift > 0) for (let i = 0; i < shift; i++) loggedStack.shift();
|
||||
|
||||
return loggedStack[1];
|
||||
return loggedStack[1];
|
||||
}
|
||||
|
||||
/*
|
||||
logger
|
||||
*/
|
||||
const LOG_TYPES = {
|
||||
Info: "[Info]".green,
|
||||
Verbose: "[Verbose]".blue,
|
||||
Warning: "[Warning]".yellow,
|
||||
Error: "[Error]".red,
|
||||
Info: '[Info]'.green,
|
||||
Verbose: '[Verbose]'.blue,
|
||||
Warning: '[Warning]'.yellow,
|
||||
Error: '[Error]'.red
|
||||
} as const;
|
||||
|
||||
export class Logger {
|
||||
private readonly name: string;
|
||||
private readonly name: string;
|
||||
|
||||
constructor(name: string) {
|
||||
this.name = name;
|
||||
}
|
||||
constructor(name: string) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public info(fmt: string, ...args: unknown[]): void {
|
||||
this.log(LOG_TYPES.Info, fmt, args);
|
||||
}
|
||||
public info(fmt: string, ...args: unknown[]): void {
|
||||
this.log(LOG_TYPES.Info, fmt, args);
|
||||
}
|
||||
|
||||
public verbose(fmt: string, ...args: unknown[]): void {
|
||||
this.log(LOG_TYPES.Verbose, fmt, args);
|
||||
}
|
||||
public verbose(fmt: string, ...args: unknown[]): void {
|
||||
this.log(LOG_TYPES.Verbose, fmt, args);
|
||||
}
|
||||
|
||||
public warning(fmt: string, ...args: unknown[]): void {
|
||||
this.log(LOG_TYPES.Warning, fmt, args);
|
||||
}
|
||||
public warning(fmt: string, ...args: unknown[]): void {
|
||||
this.log(LOG_TYPES.Warning, fmt, args);
|
||||
}
|
||||
|
||||
public error(fmt: string, ...args: unknown[]): void {
|
||||
this.log(LOG_TYPES.Error, fmt, args);
|
||||
}
|
||||
public error(fmt: string, ...args: unknown[]): void {
|
||||
this.log(LOG_TYPES.Error, fmt, args);
|
||||
}
|
||||
|
||||
private log(type: string, message: string, args: unknown[]): void {
|
||||
const caller = getCallerFrame(1);
|
||||
if (!caller) {
|
||||
console.error('Failed to determine caller information');
|
||||
return;
|
||||
}
|
||||
private log(type: string, message: string, args: unknown[]): void {
|
||||
const caller = getCallerFrame(1);
|
||||
if (!caller) {
|
||||
console.error('Failed to determine caller information');
|
||||
return;
|
||||
}
|
||||
|
||||
const timestamp = this.getFormattedTime();
|
||||
const callerInfo = this.getCallerInfo(caller);
|
||||
const formattedMessage = util.format(message, ...args);
|
||||
const timestamp = this.getFormattedTime();
|
||||
const callerInfo = this.getCallerInfo(caller);
|
||||
const formattedMessage = util.format(message, ...args);
|
||||
|
||||
console.log(
|
||||
`${timestamp} - ${callerInfo} [${this.name.magenta}] ${type} ${formattedMessage}`
|
||||
);
|
||||
}
|
||||
console.log(
|
||||
`${timestamp} - ${callerInfo} [${this.name.magenta}] ${type} ${formattedMessage}`
|
||||
);
|
||||
}
|
||||
|
||||
private getFormattedTime(): string {
|
||||
const now = new Date();
|
||||
return now.toLocaleTimeString('en-US', {
|
||||
hour: '2-digit',
|
||||
minute: '2-digit',
|
||||
second: '2-digit',
|
||||
hour12: false
|
||||
});
|
||||
}
|
||||
private getFormattedTime(): string {
|
||||
const now = new Date();
|
||||
return now.toLocaleTimeString('en-US', {
|
||||
hour: '2-digit',
|
||||
minute: '2-digit',
|
||||
second: '2-digit',
|
||||
hour12: false
|
||||
});
|
||||
}
|
||||
|
||||
private getCallerInfo(caller: NodeJS.CallSite): string {
|
||||
const functionName = caller.getFunctionName()?.replace(/\[.*\]/, '') || '<anonymous>';
|
||||
const fileName = caller.getFileName() || 'unknown';
|
||||
const relativePath = path.relative(process.cwd(), fileName);
|
||||
private getCallerInfo(caller: NodeJS.CallSite): string {
|
||||
const functionName =
|
||||
caller.getFunctionName()?.replace(/\[.*\]/, '') || '<anonymous>';
|
||||
const fileName = caller.getFileName() || 'unknown';
|
||||
const relativePath = path.relative(process.cwd(), fileName);
|
||||
|
||||
return `${functionName.cyan} @ ${relativePath.dim}`;
|
||||
}
|
||||
return `${functionName.cyan} @ ${relativePath.dim}`;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user