@mdf.js/logger
Version:
MMS - API Logger - Enhanced logger library for mms artifacts
84 lines • 3.41 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.FileTransport = exports.FileTransportSchema = 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 { File } = winston_1.transports;
const DEFAULT_ENABLED_STATE = false;
const DEFAULT_LOG_LEVEL = 'info';
const DEFAULT_FILE_NAME = 'logs/mdf-app.log';
const DEFAULT_MAX_FILES = 10;
const DEFAULT_MAX_FILE_SIZE = 10 * 1024 * 1024;
const DEFAULT_ZIP_FILES = false;
const DEFAULT_JSON_FORMAT = false;
/** File transport validation schema */
exports.FileTransportSchema = 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),
filename: joi_1.default.string().default(DEFAULT_FILE_NAME),
maxFiles: joi_1.default.number().default(DEFAULT_MAX_FILES),
maxsize: joi_1.default.number().default(DEFAULT_MAX_FILE_SIZE),
zippedArchive: joi_1.default.boolean().default(DEFAULT_ZIP_FILES),
json: joi_1.default.boolean().default(DEFAULT_JSON_FORMAT),
}).default();
/** File transport management class */
class FileTransport {
/**
* Create a file 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.FileTransportSchema.validate({})
.value;
// Stryker disable all
this.debug = (0, debug_1.default)('mms:logger:file');
this.debug(`${process.pid} - Configuration in the constructor %O`, configuration);
// Stryker enable all
const validation = exports.FileTransportSchema.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);
const fileFormat = this._config.json ? (0, formats_1.jsonFormat)(label) : (0, formats_1.stringFormat)(label);
this.instance = new File({
format: fileFormat,
silent: !this._config.enabled,
level: this._config.level,
filename: this._config.filename,
maxsize: this._config.maxsize,
maxFiles: this._config.maxFiles,
zippedArchive: this._config.zippedArchive,
});
}
/** Transport configuration */
get config() {
return this._config;
}
/** File mode transport instance */
get transport() {
return this.instance;
}
}
exports.FileTransport = FileTransport;
//# sourceMappingURL=file.js.map