misskey/src/misc/logger.ts

55 lines
1.2 KiB
TypeScript
Raw Normal View History

2017-11-06 02:59:14 -08:00
import chalk, { Chalk } from 'chalk';
export type LogLevel = 'Error' | 'Warn' | 'Info';
2017-11-06 02:59:14 -08:00
function toLevelColor(level: LogLevel): Chalk {
switch (level) {
case 'Error': return chalk.red;
case 'Warn': return chalk.yellow;
case 'Info': return chalk.blue;
}
}
2016-12-29 06:09:21 -08:00
export default class Logger {
2017-05-24 04:50:17 -07:00
private domain: string;
2016-12-29 06:09:21 -08:00
2017-05-24 04:50:17 -07:00
constructor(domain: string) {
this.domain = domain;
}
public static log(level: LogLevel, message: string): void {
const color = toLevelColor(level);
const time = (new Date()).toLocaleTimeString('ja-JP');
2018-07-13 07:51:16 -07:00
const coloredMessage = level === 'Info' ? message : color.bold(message);
console.log(`[${time} ${color.bold(level.toUpperCase())}]: ${coloredMessage}`);
2016-12-29 03:03:34 -08:00
}
2016-12-29 06:09:21 -08:00
2017-05-24 04:50:17 -07:00
public static error(message: string): void {
2016-12-29 06:09:21 -08:00
Logger.log('Error', message);
}
2017-05-24 04:50:17 -07:00
public static warn(message: string): void {
2016-12-29 06:09:21 -08:00
Logger.log('Warn', message);
}
2017-05-24 04:50:17 -07:00
public static info(message: string): void {
2016-12-29 06:09:21 -08:00
Logger.log('Info', message);
}
2017-05-24 04:50:17 -07:00
public log(level: LogLevel, message: string): void {
2016-12-29 06:09:21 -08:00
Logger.log(level, `[${this.domain}] ${message}`);
}
2017-05-24 04:50:17 -07:00
public error(message: string): void {
2016-12-29 06:09:21 -08:00
this.log('Error', message);
}
2017-05-24 04:50:17 -07:00
public warn(message: string): void {
2016-12-29 06:09:21 -08:00
this.log('Warn', message);
}
2017-05-24 04:50:17 -07:00
public info(message: string): void {
2016-12-29 06:09:21 -08:00
this.log('Info', message);
}
}