68 lines
1.4 KiB
TypeScript
68 lines
1.4 KiB
TypeScript
import dateFmt from 'dateformat';
|
|
import util from 'util';
|
|
|
|
export class Logger {
|
|
public timestamp = 'dd/mm/yy HH:MM:ss';
|
|
|
|
constructor() {}
|
|
|
|
private write(ltype: string, ...data: any[]): void {
|
|
const message = [];
|
|
let cfunc = console.log;
|
|
|
|
if (this.timestamp) {
|
|
message.push(`[${dateFmt(new Date(), this.timestamp)}]`);
|
|
}
|
|
|
|
switch (ltype) {
|
|
case 'info':
|
|
message.push('[ INFO]');
|
|
break;
|
|
case 'error':
|
|
message.push('[ERROR]');
|
|
cfunc = console.error;
|
|
break;
|
|
case 'warn':
|
|
message.push('[ WARN]');
|
|
cfunc = console.warn;
|
|
break;
|
|
case 'debug':
|
|
message.push('[DEBUG]');
|
|
break;
|
|
}
|
|
|
|
// 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.apply(null, message);
|
|
}
|
|
|
|
public log(...data: any[]): void {
|
|
this.write('info', ...data);
|
|
}
|
|
|
|
public warn(...data: any[]): void {
|
|
this.write('warn', ...data);
|
|
}
|
|
|
|
public info(...data: any[]): void {
|
|
this.write('info', ...data);
|
|
}
|
|
|
|
public error(...data: any[]): void {
|
|
this.write('error', ...data);
|
|
}
|
|
|
|
public debug(...data: any[]): void {
|
|
this.write('debug', ...data);
|
|
}
|
|
}
|
|
|
|
const logger = new Logger();
|
|
|
|
export { logger };
|