core/src/core/logger.ts

120 lines
2.5 KiB
TypeScript
Raw Normal View History

2020-11-21 15:41:08 +00:00
import dateFmt from 'dateformat';
import util from 'util';
2021-10-02 08:07:01 +00:00
type LogType = 'info' | 'debug' | 'warn' | 'error';
/**
* Logger for all of Squeebot. Use this instead of console.log/warn/error!
*/
2020-11-21 15:41:08 +00:00
export class Logger {
2020-11-28 13:34:34 +00:00
private console = [console.log, console.warn, console.error];
2020-11-21 15:41:08 +00:00
2021-10-02 08:07:01 +00:00
constructor(
public timestamp = 'dd/mm/yy HH:MM:ss'
) {}
2020-11-21 15:41:08 +00:00
2021-10-02 08:07:01 +00:00
/**
* Set node.js readline consideration
* @param rl Readline instance
*/
2020-11-28 13:34:34 +00:00
public setReadline(rl: any): void {
for (const index in this.console) {
const old = this.console[index];
this.console[index] = (...data: any[]): void => {
rl.output.write('\x1b[2K\r');
old(...data);
2021-10-02 08:07:01 +00:00
rl.prompt(true);
2020-11-28 13:34:34 +00:00
};
}
}
2021-10-02 08:07:01 +00:00
/**
* Write out to log
* @param ltype Logger level
* @param data Data to log
*/
private write(ltype: LogType, ...data: any[]): void {
2020-11-21 15:41:08 +00:00
const message = [];
2020-11-28 13:34:34 +00:00
let cfunc = this.console[0];
2020-11-21 15:41:08 +00:00
if (this.timestamp) {
message.push(`[${dateFmt(new Date(), this.timestamp)}]`);
}
switch (ltype) {
case 'info':
message.push('[ INFO]');
break;
case 'debug':
message.push('[DEBUG]');
break;
case 'warn':
message.push('[ WARN]');
cfunc = this.console[1];
break;
case 'error':
message.push('[ERROR]');
cfunc = this.console[2];
break;
2020-11-21 15:41:08 +00:00
}
// Short dance to apply formatting
let final = data[0];
if (data.length > 1) {
const fargs = data.slice(1);
final = util.format(data[0], ...fargs);
}
message.push(final);
cfunc(...message);
2020-11-21 15:41:08 +00:00
}
2021-10-02 08:07:01 +00:00
/**
* Logger level: `INFO`
*
* See `console.log` for more information.
*/
2020-11-21 15:41:08 +00:00
public log(...data: any[]): void {
this.write('info', ...data);
}
2021-10-02 08:07:01 +00:00
/**
* Logger level: `WARN`
*
* See `console.warn` for more information.
*/
2020-11-21 15:41:08 +00:00
public warn(...data: any[]): void {
this.write('warn', ...data);
}
2021-10-02 08:07:01 +00:00
/**
* Logger level: `INFO`
*
* See `console.log` for more information.
*/
2020-11-21 15:41:08 +00:00
public info(...data: any[]): void {
this.write('info', ...data);
}
2021-10-02 08:07:01 +00:00
/**
* Logger level: `ERROR`
*
* See `console.error` for more information.
*/
2020-11-21 15:41:08 +00:00
public error(...data: any[]): void {
this.write('error', ...data);
}
2021-10-02 08:07:01 +00:00
/**
* Logger level: `DEBUG`
*
* See `console.log` for more information.
*/
2020-11-21 15:41:08 +00:00
public debug(...data: any[]): void {
this.write('debug', ...data);
}
}
2021-10-02 08:07:01 +00:00
// Create singleton for Logger to be used anywhere
2020-11-21 15:41:08 +00:00
const logger = new Logger();
export { logger };