traceperf
Version:
High-performance function execution tracking and monitoring for Node.js
116 lines • 3.77 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ConfigManager = void 0;
const constants_1 = require("./constants");
/**
* Configuration manager for the logger
* Handles merging user config with defaults and provides utility methods
*/
class ConfigManager {
/**
* Create a new ConfigManager instance
*
* @param userConfig - User-provided configuration options
*/
constructor(userConfig = {}) {
this._config = { ...constants_1.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 : constants_1.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 : constants_1.DEFAULT_CONFIG.level;
// Get the minimum level for the current mode
const modeMinLevel = constants_1.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 constants_1.LOG_LEVEL_PRIORITY[level] >= constants_1.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 constants_1.LOG_LEVEL_PRIORITY[levelA] >= constants_1.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 : constants_1.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 : constants_1.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 : constants_1.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 : constants_1.DEFAULT_CONFIG.timestamp;
}
}
exports.ConfigManager = ConfigManager;
//# sourceMappingURL=config.js.map