dwldashboard/backend/src/logger.ts

43 lines
1.4 KiB
TypeScript

import chalk from "chalk";
export default class Logger {
space: string;
static getTime(): string {
const date = new Date();
const hours = date.getHours();
const minutes = date.getMinutes();
const seconds = date.getSeconds();
return `${hours < 10 ? '0' : ''}${hours}:${minutes < 10 ? '0' : ''}${minutes}:${seconds < 10 ? '0' : ''}${seconds}`;
}
constructor(name: string) {
this.space = name;
}
public log(message: string): void {
console.log(`[${Logger.getTime()}] [${this.space}] ${chalk.blue('[LOGS]')} ${message}`);
}
public warn(message: string): void {
console.log(`[${Logger.getTime()}] [${this.space}] ${chalk.yellow('[WARN]')} ${message}`);
}
public error(message: string): void {
console.log(`[${Logger.getTime()}] [${this.space}] ${chalk.red('[ERROR]')} ${message}`);
}
public info(message: string): void {
console.log(`[${Logger.getTime()}] [${this.space}] ${chalk.green('[INFO]')} ${message}`);
}
public debug(message: string): void {
if(!process.env.LOG_DEBUG) return;
console.log(`[${Logger.getTime()}] [${this.space}] ${chalk.magenta('[DEBG]')} ${message}`);
}
public fatal(message: string): void {
console.log(chalk.bgRed(`[${Logger.getTime()}] [${this.space}] ${chalk.bold('[FATL]')} ${message}`));
}
}