UNPKG

syntropylog

Version:

An instance manager with observability for Node.js applications

56 lines 2.06 kB
/** * @file src/logger/transports/BaseConsolePrettyTransport.ts * @description An abstract base class for console transports that provide colored, human-readable output. */ import chalk from 'chalk'; import { Transport } from './Transport'; /** * @class BaseConsolePrettyTransport * @description Provides common functionality for "pretty" console transports, * including color handling and console method selection. Subclasses must * implement the `formatLogString` method to define the final output format. * @extends {Transport} */ export class BaseConsolePrettyTransport extends Transport { chalk; constructor(options) { super(options); // Chalk v4 is used directly, not instantiated. this.chalk = chalk; } /** * The core log method. It handles common logic and delegates specific * formatting to the subclass. * @param {LogEntry} entry - The log entry to process. * @returns {Promise<void>} */ async log(entry) { if (!this.isLevelEnabled(entry.level)) { return; } // Apply the formatter first if it exists. const finalObject = this.formatter ? this.formatter.format(entry) : entry; // Let the subclass format the final string. const logString = this.formatLogString(finalObject); // Select the appropriate console method based on the log level. const consoleMethod = this.getConsoleMethod(finalObject.level); consoleMethod(logString); } /** * Determines which console method to use based on the log level. * @param {LogLevel} level - The log level. * @returns {Function} The corresponding console method (e.g., console.log). */ getConsoleMethod(level) { switch (level) { case 'fatal': case 'error': return console.error; case 'warn': return console.warn; default: return console.log; } } } //# sourceMappingURL=BaseConsolePrettyTransport.js.map