@geogirafe/lib-geoportal
Version:
GeoGirafe is a flexible application to build online geoportals.
79 lines (78 loc) • 2.98 kB
JavaScript
// SPDX-License-Identifier: Apache-2.0
import GirafeSingleton from '../../base/GirafeSingleton.js';
import DefaultConsole from './defaultconsole.js';
export default class LogManager extends GirafeSingleton {
async initializeSingleton() {
console.debug = this.debug.bind(this);
console.log = this.log.bind(this);
console.info = this.info.bind(this);
console.warn = this.warn.bind(this);
console.error = this.error.bind(this);
console.assert = this.assert.bind(this);
}
/**
* Write log to output if the current loglevel is debug, info, warn or error
* @returns true if the log was written, false instead
*/
debug(message, ...optionalParams) {
if (this.context.configManager.Config.general.logLevel === 'debug') {
DefaultConsole.debug(message, ...optionalParams);
return true;
}
return false;
}
/**
* Write log to output if the current loglevel is info, warn or error
* @returns true if the log was written, false instead
*/
log(message, ...optionalParams) {
if (this.context.configManager.Config.general.logLevel === 'debug' ||
this.context.configManager.Config.general.logLevel === 'info') {
DefaultConsole.log(message, ...optionalParams);
return true;
}
return false;
}
/**
* Write log to output if the current loglevel is info, warn or error
* @returns true if the log was written, false instead
*/
info(message, ...optionalParams) {
if (this.context.configManager.Config.general.logLevel === 'debug' ||
this.context.configManager.Config.general.logLevel === 'info') {
DefaultConsole.info(message, ...optionalParams);
return true;
}
return false;
}
/**
* Write log to output if the current loglevel is warn or error
* @returns true if the log was written, false instead
*/
warn(message, ...optionalParams) {
if (this.context.configManager.Config.general.logLevel === 'debug' ||
this.context.configManager.Config.general.logLevel === 'info' ||
this.context.configManager.Config.general.logLevel === 'warn') {
DefaultConsole.warn(message, ...optionalParams);
return true;
}
return false;
}
/**
* Write log to output if the current loglevel is error
* @returns true if the log was written, false instead
*/
error(message, ...optionalParams) {
DefaultConsole.error(message, ...optionalParams);
return true;
}
/**
* Write log to output if the current loglevel is error
* @returns true if the log was written, false instead
*/
assert(condition, ...optionalParams) {
// assert has the same log level as error
DefaultConsole.assert(condition, ...optionalParams);
return !condition;
}
}