UNPKG

@mdf.js/logger

Version:

MMS - API Logger - Enhanced logger library for mms artifacts

55 lines 2.35 kB
"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.jsonFormat = jsonFormat; exports.stringFormat = stringFormat; const winston_1 = require("winston"); const NULL_UUID = '00000000-0000-0000-0000-000000000000'; const PID_PADDING = 8; const LEVEL_PADDING = 7; const LABEL_PADDING = 12; const UUID_PADDING = 36; const CONTEXT_PADDING = 12; const NULL_UUID_STR = NULL_UUID.padEnd(UUID_PADDING); const UNKNOWN_CONTEXT_STR = 'unknown'.padEnd(CONTEXT_PADDING); /** * Check if a value is a string. * @param value - The value to check. * @param defaultValue - The default value to return if the value is not a string. * @returns The value as a string or the default value. */ function asString(value, defaultValue, padEnd) { const result = typeof value === 'string' ? value : `${defaultValue}`; return result.padEnd(padEnd).substring(0, padEnd); } // ************************************************************************************************* // #region Formateo de cadena de caracteres sin color const customFormat = winston_1.format.printf(info => { const pid = asString(info['pid'], process.pid, PID_PADDING); const label = asString(info['label'], 'unknown', LABEL_PADDING); const uuid = asString(info['uuid'], NULL_UUID, UUID_PADDING); const context = asString(info['context'], UNKNOWN_CONTEXT_STR, CONTEXT_PADDING); let str = `${info['timestamp']} | `; str += `${pid} | `; str += `${label} | `; str += `${info.level.padEnd(LEVEL_PADDING)} | `; str += `${uuid} | `; str += `${context} | `; str += info.message; return str; }); // #endregion // ************************************************************************************************* // #region Formatos de registro function jsonFormat(label) { return winston_1.format.combine(winston_1.format.label({ label }), winston_1.format.splat(), winston_1.format.json()); } function stringFormat(label) { return winston_1.format.combine(winston_1.format.label({ label }), winston_1.format.splat(), winston_1.format.simple(), customFormat); } //# sourceMappingURL=formats.js.map