UNPKG

syntropylog

Version:

An instance manager with observability for Node.js applications

76 lines 3.02 kB
import { BaseConsolePrettyTransport } from './BaseConsolePrettyTransport'; /** * @class ClassicConsoleTransport * A transport that writes logs to the console in a classic single-line format, * reminiscent of traditional Java logging frameworks. * @extends {BaseConsolePrettyTransport} */ export class ClassicConsoleTransport extends BaseConsolePrettyTransport { levelColorMap; /** * @constructor * @param {TransportOptions} [options] - Options for the transport, such as level or a formatter. */ constructor(options) { super(options); this.levelColorMap = { fatal: this.chalk.bgRed.white.bold, error: this.chalk.red.bold, warn: this.chalk.yellow.bold, info: this.chalk.green, // Using green for info in this style debug: this.chalk.blue, trace: this.chalk.gray, }; } /** * @private * Formats a date object into a 'YYYY-MM-DD HH:mm:ss' string. * @param {string} ts - The ISO timestamp string to format. * @returns {string} The formatted timestamp. */ formatTimestamp(ts) { const date = new Date(ts); const YYYY = date.getFullYear(); const MM = String(date.getMonth() + 1).padStart(2, '0'); const DD = String(date.getDate()).padStart(2, '0'); const HH = String(date.getHours()).padStart(2, '0'); const min = String(date.getMinutes()).padStart(2, '0'); const ss = String(date.getSeconds()).padStart(2, '0'); return `${YYYY}-${MM}-${DD} ${HH}:${min}:${ss}`; } /** * Formats the log object into a classic, single-line string. * @param {LogEntry} logObject - The log object to format. * @returns {string} The formatted string. */ formatLogString(logObject) { const { timestamp, level, service, message, context, ...rest } = logObject; const colorizer = this.levelColorMap[level] || this.chalk.white; // 1. Format the timestamp. const timeStr = this.formatTimestamp(timestamp); // 2. Format the level, padded to a fixed width for alignment. const levelStr = colorizer(level.toUpperCase().padEnd(5)); // 3. Format the service name. const serviceStr = this.chalk.magenta(`[${service}]`); // 4. Combine context, other metadata, and message, then format it. const allMeta = { ...(context || {}), ...rest, message, }; const metaKeys = Object.keys(allMeta); let metaStr = ''; if (metaKeys.length > 0) { metaStr = this.chalk.dim(' [' + metaKeys .map((key) => `${key}=${JSON.stringify(allMeta[key])}`) .join(' ') + ']'); } // 5. Assemble the final string. const logString = `${timeStr} ${levelStr} ${serviceStr}${metaStr}`; return logString; } } //# sourceMappingURL=ClassicConsoleTransport.js.map