UNPKG

@geogirafe/lib-geoportal

Version:

GeoGirafe is a flexible application to build online geoportals.

84 lines (83 loc) 3.09 kB
// SPDX-License-Identifier: Apache-2.0 import GirafeSingleton from '../../base/GirafeSingleton.js'; export default class LogManager extends GirafeSingleton { defaultDebug = console.debug; defaultLog = console.log; defaultInfo = console.info; defaultWarn = console.warn; defaultError = console.error; defaultAssert = console.assert; initLogging() { 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') { this.defaultDebug(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') { this.defaultLog(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') { this.defaultInfo(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') { this.defaultWarn(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) { this.defaultError(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 this.defaultAssert(condition, ...optionalParams); return !condition; } }