@mdf.js/logger
Version:
MMS - API Logger - Enhanced logger library for mms artifacts
71 lines • 2.75 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ConsoleTransport = exports.ConsoleTransportSchema = void 0;
const tslib_1 = require("tslib");
/**
* 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.
*/
const debug_1 = tslib_1.__importDefault(require("debug"));
const joi_1 = tslib_1.__importDefault(require("joi"));
const winston_1 = require("winston");
const formats_1 = require("../formats");
const types_1 = require("../types");
const { Console } = winston_1.transports;
const DEFAULT_ENABLED_STATE = false;
const DEFAULT_LOG_LEVEL = 'info';
/** Console transport validation schema */
exports.ConsoleTransportSchema = joi_1.default.object({
enabled: joi_1.default.boolean().default(DEFAULT_ENABLED_STATE),
level: joi_1.default.string()
.allow(...types_1.LOG_LEVELS)
.default(DEFAULT_LOG_LEVEL),
}).default();
/** Console transport management class */
class ConsoleTransport {
/**
* Create a console transport instance
* @param label - Logger label
* @param uuid - uuid of the logger instance
* @param configuration - Transport config
*/
constructor(label, uuid, configuration) {
/** Default transport config */
this.defaultConfig = exports.ConsoleTransportSchema.validate({})
.value;
// Stryker disable all
this.debug = (0, debug_1.default)('mms:logger:console');
this.debug(`${process.pid} - Configuration in the constructor %O`, configuration);
// Stryker enable all
const validation = exports.ConsoleTransportSchema.validate(configuration);
if (validation.error) {
// Stryker disable next-line all
this.debug(`${process.pid} - Error in the configuration, default will be applied`);
this._config = this.defaultConfig;
}
else {
this._config = validation.value;
}
// Stryker disable next-line all
this.debug(`${process.pid} - Final configuration %O`, this._config);
this.instance = new Console({
format: (0, formats_1.stringFormat)(label),
silent: !this._config.enabled,
level: this._config.level,
//consoleWarnLevels: ['error', 'warn'],
stderrLevels: ['error', 'warn'],
});
}
/** Transport configuration */
get config() {
return this._config;
}
/** Console mode transport instance */
get transport() {
return this.instance;
}
}
exports.ConsoleTransport = ConsoleTransport;
//# sourceMappingURL=console.js.map