@mdf.js/logger
Version:
MMS - API Logger - Enhanced logger library for mms artifacts
101 lines • 4.29 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.FluentdTransport = exports.FluentdTransportSchema = 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 fluent_logger_1 = require("fluent-logger");
const joi_1 = tslib_1.__importDefault(require("joi"));
const formats_1 = require("../formats");
const types_1 = require("../types");
const { winstonTransport } = fluent_logger_1.support;
const DEFAULT_TAG_PATH = 'netin.';
const DEFAULT_ENABLED_STATE = false;
const DEFAULT_LOG_LEVEL = 'info';
const DEFAULT_HOST = 'localhost';
const DEFAULT_PORT = 28930;
const DEFAULT_TIMEOUT = 5000;
const DEFAULT_ACK_RESPONSE = true;
const DEFAULT_RECONNECT_INTERVAL = 5000;
const DEFAULT_EVENT_MODE = 'Message';
const DEFAULT_TLS = false;
const DEFAULT_FLUSH_INTERVAL = 2000;
const DEFAULT_MESSAGE_SEND_QUEUE_SIZE = 100 * 1024 * 1024;
/** File transport validation schema */
exports.FluentdTransportSchema = 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),
host: joi_1.default.string().default(DEFAULT_HOST),
port: joi_1.default.number().default(DEFAULT_PORT),
timeout: joi_1.default.number().positive().default(DEFAULT_TIMEOUT),
requireAckResponse: joi_1.default.boolean().default(DEFAULT_ACK_RESPONSE),
flushInterval: joi_1.default.number().default(DEFAULT_FLUSH_INTERVAL),
sendQueueSizeLimit: joi_1.default.number().default(DEFAULT_MESSAGE_SEND_QUEUE_SIZE),
eventMode: joi_1.default.allow('Message', 'PackedForward', 'CompressedPackedForward').default(DEFAULT_EVENT_MODE),
reconnectInterval: joi_1.default.number().positive().default(DEFAULT_RECONNECT_INTERVAL),
tls: joi_1.default.boolean().default(DEFAULT_TLS),
tlsOptions: joi_1.default.alternatives().conditional('tls', {
is: true,
then: joi_1.default.object().required(),
otherwise: joi_1.default.any(),
}),
}).default();
/** Fluentd transport management class */
class FluentdTransport {
/**
* Create a fluentd transport instance
* @param label - Logger label
* @param logger - Logger instance
* @param uuid - uuid of the logger instance
* @param configuration - Transport config
*/
constructor(label, logger, uuid, configuration) {
/** Default transport config */
this.defaultConfig = exports.FluentdTransportSchema.validate({})
.value;
// Stryker disable all
this.debug = (0, debug_1.default)('mms:logger:fluentd');
this.debug(`${process.pid} - Configuration in the constructor %O`, configuration);
// Stryker enable all
const validation = exports.FluentdTransportSchema.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 (winstonTransport())(`${DEFAULT_TAG_PATH}${label}`, {
...this._config,
format: (0, formats_1.jsonFormat)(label),
internalLogger: {
info: (message, data, ...extra) => {
logger.silly(message, uuid, 'Fluentd', data, ...extra);
},
error: (message, data, ...extra) => {
logger.error(message, uuid, 'Fluentd', data, ...extra);
},
},
});
}
/** Transport configuration */
get config() {
return this._config;
}
/** Mongodb mode transport instance */
get transport() {
return this.instance;
}
}
exports.FluentdTransport = FluentdTransport;
//# sourceMappingURL=fluentd.js.map