-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.ts
53 lines (43 loc) · 1.37 KB
/
logger.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import chalk from "chalk";
import log, { Logger as LogInstance } from "loglevel";
import prefix from 'loglevel-plugin-prefix';
import moment from 'moment';
import { getDateTimeFormat } from "../../windowVars";
const LOG_COLORS: { [key: string]: any } = {
TRACE: chalk.magenta,
DEBUG: chalk.cyan,
INFO: chalk.blue,
WARN: chalk.yellow,
ERROR: chalk.red,
};
class Logger {
logInstance: LogInstance = null;
constructor() {
// init logging
const logger = log.noConflict();
logger.setLevel(log.levels.DEBUG);
prefix.reg(logger);
prefix.apply(logger, {
format(level: string, name: string, timestamp: Date) {
return `${chalk.gray(`[${timestamp}]`)} ${LOG_COLORS[level.toUpperCase()](level)} ${chalk.green(`${window.location.pathname}:`)}`;
},
timestampFormatter(date: Date) {
return moment(date).format(getDateTimeFormat());
}
});
this.logInstance = logger;
}
info(...msg: any[]) {
this.logInstance.info(...msg);
}
error(...msg: any[]) {
this.logInstance.error(...msg);
}
warn(...msg: any[]) {
this.logInstance.warn(...msg);
}
debug(...msg: any[]) {
this.logInstance.debug(...msg);
}
}
export default new Logger();