UNPKG

@axinom/mosaic-transactional-inbox-outbox

Version:

This library encapsulates the Mosaic based transactional inbox and outbox pattern

77 lines 2.23 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.TransactionalLogMapper = void 0; // The pg-transactional-outbox library uses a central logger based in Pino. // This logger matches the Mosaic based logger to the Pino one. class TransactionalLogMapper { constructor(logger, logLevel) { this.logger = logger; this.level = logLevel; } fatal(obj, msg) { this.log('fatal', obj, msg); } error(obj, msg) { this.log('error', obj, msg); } warn(obj, msg) { this.log('warn', obj, msg); } info(obj, msg) { this.log('log', obj, msg); // different mapping from Pino to Mosaic Logger } debug(obj, msg) { this.log('debug', obj, msg); } trace(obj, msg) { this.log('trace', obj, msg); } silent() { /** psst... */ } log(level, obj, msg) { // eslint-disable-next-line @typescript-eslint/no-explicit-any const logFunc = this.logger[level].bind(this.logger); if (typeof obj === 'string') { logFunc(obj); } else if (obj instanceof Error) { logFunc(obj, msg); } else { logFunc(getLogMessage(obj, msg)); } } } exports.TransactionalLogMapper = TransactionalLogMapper; const getLogMessage = (obj, message) => { if (typeof obj !== 'object' || !obj) { return { message }; } if ('error' in obj || 'details' in obj) { return Object.assign({ message }, obj); } if (!('aggregateId' in obj) || !('aggregateType' in obj) || !('messageType' in obj) || !('payload' in obj) || typeof obj.payload !== 'object' || !obj.payload || !('content' in obj.payload)) { return { details: obj, message, }; } // For logged WAL messages just log some details and the content return { details: { aggregateId: obj.aggregateId, aggregateType: obj.aggregateType, messageType: obj.messageType, content: obj.payload.content, }, message, }; }; //# sourceMappingURL=transactional-log-mapper.js.map