UNPKG

@axinom/mosaic-transactional-inbox-outbox

Version:

This library encapsulates the Mosaic based transactional inbox and outbox pattern

81 lines (73 loc) 2.19 kB
import { InboxOutboxLogger, LogMessage } from './common'; // The pg-transactional-outbox library uses a central logger based in Pino. // This logger matches the Mosaic based logger to the Pino one. export class TransactionalLogMapper { constructor(private logger: InboxOutboxLogger, logLevel: string) { this.level = logLevel; } public fatal(obj: unknown, msg?: string): void { this.log('fatal', obj, msg); } public error(obj: unknown, msg?: string): void { this.log('error', obj, msg); } public warn(obj: unknown, msg?: string): void { this.log('warn', obj, msg); } public info(obj: unknown, msg?: string): void { this.log('log', obj, msg); // different mapping from Pino to Mosaic Logger } public debug(obj: unknown, msg?: string): void { this.log('debug', obj, msg); } public trace(obj: unknown, msg?: string): void { this.log('trace', obj, msg); } public silent(): void { /** psst... */ } level: string; private log(level: string, obj: unknown, msg?: string): void { // eslint-disable-next-line @typescript-eslint/no-explicit-any const logFunc = (this.logger as any)[level].bind(this.logger); if (typeof obj === 'string') { logFunc(obj); } else if (obj instanceof Error) { logFunc(obj, msg); } else { logFunc(getLogMessage(obj, msg)); } } } const getLogMessage = (obj: unknown, message?: string): LogMessage => { if (typeof obj !== 'object' || !obj) { return { message }; } if ('error' in obj || 'details' in obj) { return { message, ...obj } as LogMessage; } 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 as Record<string, unknown>, 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, } as LogMessage; };