Commit 2b213d05 authored by JC Brand's avatar JC Brand

log: properly log to the set level

parent 30bd4d34
import { get, isElement } from 'lodash'; import { get, isElement } from 'lodash';
const LEVELS = {
'debug': 0,
'info': 1,
'warn': 2,
'error': 3,
'fatal': 4
}
const logger = Object.assign({ const logger = Object.assign({
'debug': get(console, 'log') ? console.log.bind(console) : function noop () {}, 'debug': get(console, 'log') ? console.log.bind(console) : function noop () {},
...@@ -20,7 +27,7 @@ const log = { ...@@ -20,7 +27,7 @@ const log = {
* @method log#setLogLevel * @method log#setLogLevel
* @param { integer } level - The loglevel which allows for filtering of log messages * @param { integer } level - The loglevel which allows for filtering of log messages
*/ */
setLogLevel(level) { setLogLevel (level) {
if (!['debug', 'info', 'warn', 'error', 'fatal'].includes(level)) { if (!['debug', 'info', 'warn', 'error', 'fatal'].includes(level)) {
throw new Error(`Invalid loglevel: ${level}`); throw new Error(`Invalid loglevel: ${level}`);
} }
...@@ -38,6 +45,9 @@ const log = { ...@@ -38,6 +45,9 @@ const log = {
* @param { integer } level - The loglevel which allows for filtering of log messages * @param { integer } level - The loglevel which allows for filtering of log messages
*/ */
log (message, level, style='') { log (message, level, style='') {
if (LEVELS[level] < LEVELS[this.loglevel]) {
return;
}
if (level === 'error' || level === 'fatal') { if (level === 'error' || level === 'fatal') {
style = style || 'color: maroon'; style = style || 'color: maroon';
} else if (level === 'debug') { } else if (level === 'debug') {
...@@ -57,10 +67,8 @@ const log = { ...@@ -57,10 +67,8 @@ const log = {
} else if (level === 'fatal') { } else if (level === 'fatal') {
logger.error(`${prefix} FATAL: ${message}`, style); logger.error(`${prefix} FATAL: ${message}`, style);
} else if (level === 'debug') { } else if (level === 'debug') {
if (this.loglevel === 'debug') {
logger.debug(`${prefix} ${(new Date()).toISOString()} DEBUG: ${message}`, style); logger.debug(`${prefix} ${(new Date()).toISOString()} DEBUG: ${message}`, style);
} } else {
} else if (this.loglevel === 'info') {
logger.info(`${prefix} ${(new Date()).toISOString()} INFO: ${message}`, style); logger.info(`${prefix} ${(new Date()).toISOString()} INFO: ${message}`, style);
} }
}, },
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment