UNPKG

traceperf

Version:

High-performance function execution tracking and monitoring for Node.js

112 lines 3.52 kB
import { DEFAULT_CONFIG, LOG_LEVEL_PRIORITY, MODE_MIN_LEVELS } from './constants'; /** * Configuration manager for the logger * Handles merging user config with defaults and provides utility methods */ export class ConfigManager { /** * Create a new ConfigManager instance * * @param userConfig - User-provided configuration options */ constructor(userConfig = {}) { this._config = { ...DEFAULT_CONFIG, ...userConfig }; } /** * Get the current configuration * * @returns The current configuration */ getConfig() { return { ...this._config }; } /** * Update the configuration * * @param newConfig - New configuration options to merge */ updateConfig(newConfig) { this._config = { ...this._config, ...newConfig }; } /** * Get the current mode * * @returns The current mode */ getMode() { var _a; return (_a = this._config.mode) !== null && _a !== void 0 ? _a : DEFAULT_CONFIG.mode; } /** * Set the current mode * * @param mode - The mode to set */ setMode(mode) { this._config.mode = mode; } /** * Check if a log level should be displayed based on the current mode and level * * @param level - The log level to check * @returns Whether the log level should be displayed */ shouldLog(level) { var _a; const currentMode = this.getMode(); const configLevel = (_a = this._config.level) !== null && _a !== void 0 ? _a : DEFAULT_CONFIG.level; // Get the minimum level for the current mode const modeMinLevel = MODE_MIN_LEVELS[currentMode] || 'info'; // Use the higher of the mode's minimum level and the configured level const effectiveMinLevel = this.getHigherPriorityLevel(modeMinLevel, configLevel); // Check if the provided level meets the minimum threshold return LOG_LEVEL_PRIORITY[level] >= LOG_LEVEL_PRIORITY[effectiveMinLevel]; } /** * Get the higher priority level between two log levels * * @param levelA - First log level * @param levelB - Second log level * @returns The higher priority level */ getHigherPriorityLevel(levelA, levelB) { return LOG_LEVEL_PRIORITY[levelA] >= LOG_LEVEL_PRIORITY[levelB] ? levelA : levelB; } /** * Get the performance threshold * * @returns The performance threshold in milliseconds */ getPerformanceThreshold() { var _a; return (_a = this._config.performanceThreshold) !== null && _a !== void 0 ? _a : DEFAULT_CONFIG.performanceThreshold; } /** * Get the indentation size * * @returns The number of spaces for each indentation level */ getIndentSize() { var _a; return (_a = this._config.indentSize) !== null && _a !== void 0 ? _a : DEFAULT_CONFIG.indentSize; } /** * Check if colorization is enabled * * @returns Whether colorization is enabled */ isColorEnabled() { var _a; return (_a = this._config.colorize) !== null && _a !== void 0 ? _a : DEFAULT_CONFIG.colorize; } /** * Check if timestamps are enabled * * @returns Whether timestamps are enabled */ isTimestampEnabled() { var _a; return (_a = this._config.timestamp) !== null && _a !== void 0 ? _a : DEFAULT_CONFIG.timestamp; } } //# sourceMappingURL=config.js.map