Make logger output configurable
init_logging can now optionally be called with an object containing custom handlers for log messages. This allows clients to use their own logging frameworks instead of log messages always just being written to the console.
This commit is contained in:
parent
ceb8ef4ec1
commit
fe034b04f7
|
|
@ -17,18 +17,28 @@ let Info = () => {};
|
||||||
let Warn = () => {};
|
let Warn = () => {};
|
||||||
let Error = () => {};
|
let Error = () => {};
|
||||||
|
|
||||||
export function init_logging(level) {
|
export function init_logging(param) {
|
||||||
if (typeof level === 'undefined') {
|
Debug = Info = Warn = Error = () => {};
|
||||||
level = _log_level;
|
let has_custom_logger = false;
|
||||||
} else {
|
switch (typeof param) {
|
||||||
_log_level = level;
|
case 'string':
|
||||||
|
_log_level = param;
|
||||||
|
break;
|
||||||
|
case 'object':
|
||||||
|
let has_custom_logger = true;
|
||||||
|
if ('level' in param) {
|
||||||
|
_log_level = param.level;
|
||||||
|
}
|
||||||
|
if ('debug' in param) { Debug = param.debug; }
|
||||||
|
if ('info' in param) { Info = param.info; }
|
||||||
|
if ('warn' in param) { Warn = param.warn; }
|
||||||
|
if ('error' in param) { Error = param.error; }
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
Debug = Info = Warn = Error = () => {};
|
if (!has_custom_logger && typeof window.console !== "undefined") {
|
||||||
|
|
||||||
if (typeof window.console !== "undefined") {
|
|
||||||
/* eslint-disable no-console, no-fallthrough */
|
/* eslint-disable no-console, no-fallthrough */
|
||||||
switch (level) {
|
switch (_log_level) {
|
||||||
case 'debug':
|
case 'debug':
|
||||||
Debug = console.debug.bind(window.console);
|
Debug = console.debug.bind(window.console);
|
||||||
case 'info':
|
case 'info':
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue