@finos/legend-shared
Version:
Legend Studio shared utilities and helpers
90 lines • 3.03 kB
JavaScript
/**
* Copyright (c) 2020-present, Goldman Sachs
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { AbstractPlugin, } from '../application/AbstractPluginManager.js';
// We use numeric enum here for because we want to do comparison
// In order to retrieve the name of the enum we can do reverse mapping, for example: LogLevel[LogLevel.INFO] -> INFO
// https://www.typescriptlang.org/docs/handbook/enums.html#reverse-mappings
export var LOG_LEVEL;
(function (LOG_LEVEL) {
LOG_LEVEL[LOG_LEVEL["DEBUG"] = 1] = "DEBUG";
LOG_LEVEL[LOG_LEVEL["INFO"] = 2] = "INFO";
LOG_LEVEL[LOG_LEVEL["WARN"] = 3] = "WARN";
LOG_LEVEL[LOG_LEVEL["ERROR"] = 4] = "ERROR";
LOG_LEVEL[LOG_LEVEL["SILENT"] = 5] = "SILENT";
})(LOG_LEVEL || (LOG_LEVEL = {}));
export class LogEvent {
channel;
name;
timestamp = Date.now();
/**
* TODO: we should make channel required and potentially name required as well, and for each
* channel, there should be a list of known event names. Unknown event name, or wrong channel
* will result in error.
*/
static create(name) {
const event = new LogEvent();
event.name = name;
return event;
}
}
export class LoggerPlugin extends AbstractPlugin {
level = LOG_LEVEL.DEBUG;
setLevel(level) {
this.level = level;
}
install(pluginManager) {
pluginManager.registerLoggerPlugin(this);
}
debug(event, ...data) {
if (this.level <= LOG_LEVEL.DEBUG) {
this._debug(event, ...data);
}
}
info(event, ...data) {
if (this.level <= LOG_LEVEL.INFO) {
this._info(event, ...data);
}
}
warn(event, ...data) {
if (this.level <= LOG_LEVEL.WARN) {
this._warn(event, ...data);
}
}
error(event, ...data) {
if (this.level <= LOG_LEVEL.ERROR) {
this._error(event, ...data);
}
}
}
export class LogService {
loggers = [];
registerPlugins(loggerPlugins) {
this.loggers = loggerPlugins;
}
debug(event, ...data) {
this.loggers.forEach((logger) => logger.debug(event, ...data));
}
info(event, ...data) {
this.loggers.forEach((logger) => logger.info(event, ...data));
}
warn(event, ...data) {
this.loggers.forEach((logger) => logger.warn(event, ...data));
}
error(event, ...data) {
this.loggers.forEach((logger) => logger.error(event, ...data));
}
}
//# sourceMappingURL=LogService.js.map