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