2017-09-09 15:16:15 +08:00
|
|
|
import moment from 'moment'
|
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
|
|
|
|
}
|
|
|
|
|
2017-09-09 15:16:15 +08:00
|
|
|
const format = (level) => {
|
|
|
|
const time = moment().format('HH:mm:ss (SSS)')
|
2017-09-10 16:56:10 +08:00
|
|
|
return `${time} : ${level} : `
|
2015-10-19 21:36:55 +02:00
|
|
|
}
|
2015-06-30 14:23:32 +02:00
|
|
|
|
2017-09-09 15:16:15 +08:00
|
|
|
export const Log = {
|
|
|
|
debug: () => {},
|
|
|
|
info: () => {},
|
|
|
|
warn: () => {},
|
|
|
|
error: () => {},
|
|
|
|
fatal: () => {}
|
2016-12-14 12:41:12 +01:00
|
|
|
}
|
2015-06-30 14:23:32 +02:00
|
|
|
|
2017-09-09 15:16:15 +08:00
|
|
|
export const setLogLevel = function (level) {
|
2017-09-10 16:56:10 +08:00
|
|
|
if (level <= LEVELS.fatal) {
|
|
|
|
Log.fatal = 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) {
|
|
|
|
Log.error = 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) {
|
|
|
|
Log.warn = 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) {
|
|
|
|
Log.info = 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) {
|
|
|
|
Log.debug = console.log.bind(console, '\x1b[32m', format('DEBUG'))
|
2017-04-11 22:14:25 +08:00
|
|
|
}
|
2015-10-04 19:30:53 +02:00
|
|
|
}
|