2020-12-08 12:54:14 +01:00
|
|
|
import isElement from 'lodash-es/isElement';
|
2019-11-06 11:01:34 +01:00
|
|
|
|
2019-12-19 18:26:00 +01:00
|
|
|
const LEVELS = {
|
|
|
|
'debug': 0,
|
|
|
|
'info': 1,
|
|
|
|
'warn': 2,
|
|
|
|
'error': 3,
|
|
|
|
'fatal': 4
|
|
|
|
}
|
2019-11-06 11:01:34 +01:00
|
|
|
|
|
|
|
const logger = Object.assign({
|
2020-03-24 12:26:34 +01:00
|
|
|
'debug': console?.log ? console.log.bind(console) : function noop () {},
|
|
|
|
'error': console?.log ? console.log.bind(console) : function noop () {},
|
|
|
|
'info': console?.log ? console.log.bind(console) : function noop () {},
|
|
|
|
'warn': console?.log ? console.log.bind(console) : function noop () {}
|
2019-11-06 11:01:34 +01:00
|
|
|
}, console);
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The log namespace
|
|
|
|
* @namespace log
|
|
|
|
*/
|
|
|
|
const log = {
|
|
|
|
|
|
|
|
/**
|
2019-11-22 14:23:19 +01:00
|
|
|
* The the log-level, which determines how verbose the logging is.
|
|
|
|
* @method log#setLogLevel
|
2019-11-06 11:01:34 +01:00
|
|
|
* @param { integer } level - The loglevel which allows for filtering of log messages
|
|
|
|
*/
|
2019-12-19 18:26:00 +01:00
|
|
|
setLogLevel (level) {
|
2019-11-22 14:23:19 +01:00
|
|
|
if (!['debug', 'info', 'warn', 'error', 'fatal'].includes(level)) {
|
|
|
|
throw new Error(`Invalid loglevel: ${level}`);
|
|
|
|
}
|
|
|
|
this.loglevel = level;
|
2019-11-06 11:01:34 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Logs messages to the browser's developer console.
|
|
|
|
* Available loglevels are 0 for 'debug', 1 for 'info', 2 for 'warn',
|
|
|
|
* 3 for 'error' and 4 for 'fatal'.
|
|
|
|
* When using the 'error' or 'warn' loglevels, a full stacktrace will be
|
|
|
|
* logged as well.
|
|
|
|
* @method log#log
|
2022-04-22 00:14:44 +02:00
|
|
|
* @param { string | Error } message - The message to be logged
|
2019-11-06 11:01:34 +01:00
|
|
|
* @param { integer } level - The loglevel which allows for filtering of log messages
|
|
|
|
*/
|
|
|
|
log (message, level, style='') {
|
2019-12-19 18:26:00 +01:00
|
|
|
if (LEVELS[level] < LEVELS[this.loglevel]) {
|
|
|
|
return;
|
|
|
|
}
|
2019-11-22 14:23:19 +01:00
|
|
|
if (level === 'error' || level === 'fatal') {
|
2019-11-06 11:01:34 +01:00
|
|
|
style = style || 'color: maroon';
|
2019-11-22 14:23:19 +01:00
|
|
|
} else if (level === 'debug') {
|
2019-11-06 11:01:34 +01:00
|
|
|
style = style || 'color: green';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (message instanceof Error) {
|
|
|
|
message = message.stack;
|
|
|
|
} else if (isElement(message)) {
|
|
|
|
message = message.outerHTML;
|
|
|
|
}
|
|
|
|
const prefix = style ? '%c' : '';
|
2019-11-22 14:23:19 +01:00
|
|
|
if (level === 'error') {
|
2019-11-06 11:01:34 +01:00
|
|
|
logger.error(`${prefix} ERROR: ${message}`, style);
|
2019-11-22 14:23:19 +01:00
|
|
|
} else if (level === 'warn') {
|
2019-11-06 11:01:34 +01:00
|
|
|
logger.warn(`${prefix} ${(new Date()).toISOString()} WARNING: ${message}`, style);
|
2019-11-22 14:23:19 +01:00
|
|
|
} else if (level === 'fatal') {
|
2019-11-06 11:01:34 +01:00
|
|
|
logger.error(`${prefix} FATAL: ${message}`, style);
|
2019-11-22 14:23:19 +01:00
|
|
|
} else if (level === 'debug') {
|
2019-12-19 18:26:00 +01:00
|
|
|
logger.debug(`${prefix} ${(new Date()).toISOString()} DEBUG: ${message}`, style);
|
|
|
|
} else {
|
2019-11-06 11:01:34 +01:00
|
|
|
logger.info(`${prefix} ${(new Date()).toISOString()} INFO: ${message}`, style);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2019-11-22 14:23:19 +01:00
|
|
|
debug (message, style) {
|
|
|
|
this.log(message, 'debug', style);
|
2019-11-06 11:01:34 +01:00
|
|
|
},
|
|
|
|
|
2019-11-22 14:23:19 +01:00
|
|
|
error (message, style) {
|
|
|
|
this.log(message, 'error', style);
|
2019-11-06 11:01:34 +01:00
|
|
|
},
|
|
|
|
|
2019-11-22 14:23:19 +01:00
|
|
|
info (message, style) {
|
|
|
|
this.log(message, 'info', style);
|
2019-11-06 11:01:34 +01:00
|
|
|
},
|
|
|
|
|
2019-11-22 14:23:19 +01:00
|
|
|
warn (message, style) {
|
|
|
|
this.log(message, 'warn', style);
|
2019-11-06 11:01:34 +01:00
|
|
|
},
|
|
|
|
|
2019-11-22 14:23:19 +01:00
|
|
|
fatal (message, style) {
|
|
|
|
this.log(message, 'fatal', style);
|
2019-11-06 11:01:34 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default log;
|