@mdf.js/logger
Version:
MMS - API Logger - Enhanced logger library for mms artifacts
118 lines • 6.26 kB
JavaScript
"use strict";
/**
* Copyright 2024 Mytra Control S.L. All rights reserved.
*
* Use of this source code is governed by an MIT-style license that can be found in the LICENSE file
* or at https://opensource.org/licenses/MIT.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.WrapperLogger = void 0;
exports.SetContext = SetContext;
const uuid_1 = require("uuid");
class WrapperLogger {
/**
* Create a wrapped version of the logger where the context and uuid are already set
* @param logger - Logger instance to wrap
* @param context - context (class/function) where this logger is logging
* @param componentId - component identification
*/
constructor(logger, context, componentId = (0, uuid_1.v4)()) {
this.logger = logger;
this.context = context;
this.componentId = componentId;
/**
* Log events in the SILLY level: all the information in a very detailed way.
* This level used to be necessary only in the development process, and the meta data used to be
* the results of the operations.
* @param message - human readable information to log
* @param uuid - unique identifier for the actual job/task/request process
* @param context - context (class/function) where this logger is logging
* @param meta - extra information
*/
this.silly = (message, uuid, context, ...meta) => {
this.logger.silly(message, uuid !== null && uuid !== void 0 ? uuid : this.componentId, context !== null && context !== void 0 ? context : this.context, ...meta);
};
/**
* Log events in the DEBUG level: all the information in a detailed way.
* This level used to be necessary only in the debugging process, so not all the data is
* reported, only the related with the main processes and tasks.
* @param message - human readable information to log
* @param uuid - unique identifier for the actual job/task/request process
* @param context - context (class/function) where this logger is logging
* @param meta - extra information
*/
this.debug = (message, uuid, context, ...meta) => {
this.logger.debug(message, uuid !== null && uuid !== void 0 ? uuid : this.componentId, context !== null && context !== void 0 ? context : this.context, ...meta);
};
/**
* Log events in the VERBOSE level: trace information without details.
* This level used to be necessary only in system configuration process, so information about
* the settings and startup process used to be reported.
* @param message - human readable information to log
* @param uuid - unique identifier for the actual job/task/request process
* @param context - context (class/function) where this logger is logging
* @param meta - extra information
*/
this.verbose = (message, uuid, context, ...meta) => {
this.logger.verbose(message, uuid !== null && uuid !== void 0 ? uuid : this.componentId, context !== null && context !== void 0 ? context : this.context, ...meta);
};
/**
* Log events in the INFO level: only relevant events are reported.
* This level is the default level.
* @param message - human readable information to log
* @param uuid - unique identifier for the actual job/task/request process
* @param context - context (class/function) where this logger is logging
* @param meta - extra information
*/
this.info = (message, uuid, context, ...meta) => {
this.logger.info(message, uuid !== null && uuid !== void 0 ? uuid : this.componentId, context !== null && context !== void 0 ? context : this.context, ...meta);
};
/**
* Log events in the WARN level: information about possible problems or dangerous situations.
* @param message - human readable information to log
* @param uuid - unique identifier for the actual job/task/request process
* @param context - context (class/function) where this logger is logging
* @param meta - extra information
*/
this.warn = (message, uuid, context, ...meta) => {
this.logger.warn(message, uuid !== null && uuid !== void 0 ? uuid : this.componentId, context !== null && context !== void 0 ? context : this.context, ...meta);
};
/**
* Log events in the ERROR level: all the errors and problems with detailed information.
* @param message - human readable information to log
* @param uuid - unique identifier for the actual job/task/request process
* @param context - context (class/function) where this logger is logging
* @param meta - extra information
*/
this.error = (message, uuid, context, ...meta) => {
this.logger.error(message, uuid !== null && uuid !== void 0 ? uuid : this.componentId, context !== null && context !== void 0 ? context : this.context, ...meta);
};
/**
* Log events in the ERROR level: all the information in a very detailed way.
* This level used to be necessary only in the development process.
* @param rawError - crash error instance
* @param context - context (class/function) where this logger is logging
*/
this.crash = (rawError, context) => {
this.logger.crash(rawError, context !== null && context !== void 0 ? context : this.context);
};
/** Stream logger */
this.stream = {
write: (message) => {
this.logger.stream.write(message);
},
};
}
}
exports.WrapperLogger = WrapperLogger;
/**
* Create a wrapped version of the logger where the context and uuid are already set
* @param logger - Logger instance to wrap
* @param context - context (class/function) where this logger is logging
* @param componentId - component identification
* @returns
*/
function SetContext(logger, context, componentId) {
return new WrapperLogger(logger, context, componentId);
}
//# sourceMappingURL=Wrapper.js.map