2019-09-12 12:58:57 -07:00
|
|
|
import moment from 'moment-mini';
|
2015-06-30 14:23:32 +02:00
|
|
|
|
2017-09-10 16:56:10 +08:00
|
|
|
export const LEVELS = {
|
|
|
|
debug: 1,
|
|
|
|
info: 2,
|
|
|
|
warn: 3,
|
|
|
|
error: 4,
|
|
|
|
fatal: 5
|
2019-09-12 12:58:57 -07:00
|
|
|
};
|
2017-09-10 16:56:10 +08:00
|
|
|
|
2017-09-10 19:41:34 +08:00
|
|
|
export const logger = {
|
2017-09-09 15:16:15 +08:00
|
|
|
debug: () => {},
|
|
|
|
info: () => {},
|
|
|
|
warn: () => {},
|
|
|
|
error: () => {},
|
|
|
|
fatal: () => {}
|
2019-09-12 12:58:57 -07:00
|
|
|
};
|
2015-06-30 14:23:32 +02:00
|
|
|
|
2019-09-12 12:58:57 -07:00
|
|
|
export const setLogLevel = function(level) {
|
2020-03-08 09:49:41 +01:00
|
|
|
logger.trace = () => {};
|
2019-09-12 12:58:57 -07:00
|
|
|
logger.debug = () => {};
|
|
|
|
logger.info = () => {};
|
|
|
|
logger.warn = () => {};
|
|
|
|
logger.error = () => {};
|
|
|
|
logger.fatal = () => {};
|
2017-09-10 16:56:10 +08:00
|
|
|
if (level <= LEVELS.fatal) {
|
2019-12-15 09:50:24 +01:00
|
|
|
logger.fatal = console.error
|
|
|
|
? console.error.bind(console, format('FATAL'), 'color: orange')
|
|
|
|
: console.log.bind(console, '\x1b[35m', format('FATAL'));
|
2017-04-11 23:46:11 +08:00
|
|
|
}
|
2017-09-10 16:56:10 +08:00
|
|
|
if (level <= LEVELS.error) {
|
2019-12-15 09:50:24 +01:00
|
|
|
logger.error = console.error
|
|
|
|
? console.error.bind(console, format('ERROR'), 'color: orange')
|
|
|
|
: console.log.bind(console, '\x1b[31m', format('ERROR'));
|
2017-04-11 23:46:11 +08:00
|
|
|
}
|
2017-09-10 16:56:10 +08:00
|
|
|
if (level <= LEVELS.warn) {
|
2019-12-15 09:50:24 +01:00
|
|
|
logger.warn = console.warn
|
|
|
|
? console.warn.bind(console, format('WARN'), 'color: orange')
|
|
|
|
: console.log.bind(console, `\x1b[33m`, format('WARN'));
|
2017-04-11 23:46:11 +08:00
|
|
|
}
|
2017-09-10 16:56:10 +08:00
|
|
|
if (level <= LEVELS.info) {
|
2019-12-15 09:50:24 +01:00
|
|
|
logger.info = console.info
|
|
|
|
? // ? console.info.bind(console, '\x1b[34m', format('INFO'), 'color: blue')
|
|
|
|
console.info.bind(console, format('INFO'), 'color: lightblue')
|
|
|
|
: console.log.bind(console, '\x1b[34m', format('INFO'));
|
2017-04-11 23:46:11 +08:00
|
|
|
}
|
2017-09-10 16:56:10 +08:00
|
|
|
if (level <= LEVELS.debug) {
|
2019-12-15 09:50:24 +01:00
|
|
|
logger.debug = console.debug
|
|
|
|
? console.debug.bind(console, format('DEBUG'), 'color: lightgreen')
|
|
|
|
: console.log.bind(console, '\x1b[32m', format('DEBUG'));
|
2017-04-11 22:14:25 +08:00
|
|
|
}
|
2019-09-12 12:58:57 -07:00
|
|
|
};
|
2017-09-10 19:41:34 +08:00
|
|
|
|
2019-09-12 12:58:57 -07:00
|
|
|
const format = level => {
|
2019-12-15 09:50:24 +01:00
|
|
|
const time = moment().format('ss.SSS');
|
|
|
|
return `%c${time} : ${level} : `;
|
2019-09-12 12:58:57 -07:00
|
|
|
};
|