@wgtechlabs/log-engine
Version:
A lightweight, security-first logging utility with automatic data redaction for Node.js applications - the first logging library with built-in PII protection.
81 lines • 3.35 kB
JavaScript
;
/**
* Log level and mode filtering logic
* Handles the decision logic for whether messages should be logged
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.LogFilter = void 0;
const types_1 = require("../types/index.cjs");
/**
* Filtering logic for log messages based on levels and modes
* Determines whether a message should be output based on current configuration
*/
class LogFilter {
/**
* Determines if a message should be logged based on current log mode
* Messages are shown only if their level is appropriate for the configured mode
* LOG level is special - it always outputs regardless of configured mode (except when OFF is set)
* OFF mode disables all logging including LOG level messages
* @param level - The log level of the message to check
* @param currentMode - The current logging mode
* @returns true if message should be logged, false otherwise
*/
static shouldLog(level, currentMode) {
// Get the severity rank for the message level using safe lookup
const messageSeverity = LogFilter.getSeverityRank(level);
// Get the minimum severity threshold for the current mode using safe lookup
const modeThreshold = LogFilter.getModeThreshold(currentMode);
// Allow the message if its severity meets or exceeds the mode threshold
return messageSeverity >= modeThreshold;
}
/**
* Get the severity rank for a log level
* @param level - The log level to get rank for
* @returns Numeric severity rank
*/
static getSeverityRank(level) {
switch (level) {
case types_1.LogLevel.DEBUG: return 0;
case types_1.LogLevel.INFO: return 1;
case types_1.LogLevel.WARN: return 2;
case types_1.LogLevel.ERROR: return 3;
case types_1.LogLevel.LOG: return 99;
default: return 0;
}
}
/**
* Get the threshold for a log mode
* @param mode - The log mode to get threshold for
* @returns Numeric threshold value
*/
static getModeThreshold(mode) {
switch (mode) {
case types_1.LogMode.DEBUG: return 0;
case types_1.LogMode.INFO: return 1;
case types_1.LogMode.WARN: return 2;
case types_1.LogMode.ERROR: return 3;
case types_1.LogMode.SILENT: return 99;
case types_1.LogMode.OFF: return 100;
default: return 0;
}
}
}
exports.LogFilter = LogFilter;
// Maps LogLevel values to severity ranks for consistent comparison
LogFilter.SEVERITY_RANKS = {
[types_1.LogLevel.DEBUG]: 0,
[types_1.LogLevel.INFO]: 1,
[types_1.LogLevel.WARN]: 2,
[types_1.LogLevel.ERROR]: 3,
[types_1.LogLevel.LOG]: 99 // Special case - always outputs (except when OFF)
};
// Maps LogMode values to minimum severity rank required for output
LogFilter.MODE_THRESHOLDS = {
[types_1.LogMode.DEBUG]: 0, // Shows DEBUG and above
[types_1.LogMode.INFO]: 1, // Shows INFO and above
[types_1.LogMode.WARN]: 2, // Shows WARN and above
[types_1.LogMode.ERROR]: 3, // Shows ERROR and above
[types_1.LogMode.SILENT]: 99, // Only shows LOG messages
[types_1.LogMode.OFF]: 100 // Shows nothing
};
//# sourceMappingURL=filtering.js.map