log-vault
Version:
A generator of Winston logger instance with pre-defined configurable transports and formats and extra functionality.
41 lines (40 loc) • 1.89 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.matchPattern = matchPattern;
function matchPattern(log, channels) {
return channels.filter((channel) => {
var _a;
return !((_a = channel.matchPatterns) === null || _a === void 0 ? void 0 : _a.length) ||
channel.matchPatterns.some((pattern) => isPatternMatched(log, pattern));
});
}
function isPatternMatched(log, pattern) {
const { level, match, exclude } = pattern;
let matching = true;
if (exclude) {
if (exclude.meta) {
if (Object.keys(exclude.meta).some((key) => { var _a, _b; return ((_a = log.meta) === null || _a === void 0 ? void 0 : _a[key]) === ((_b = exclude.meta) === null || _b === void 0 ? void 0 : _b[key]); }))
return false;
}
if (exclude.message && isMessageMatching(exclude.message, log.message))
return false;
}
if (level && log.level !== level)
return false;
if (match === null || match === void 0 ? void 0 : match.meta) {
matching && (matching = Object.keys(match.meta).every((key) => { var _a, _b; return ((_a = log.meta) === null || _a === void 0 ? void 0 : _a[key]) === ((_b = match.meta) === null || _b === void 0 ? void 0 : _b[key]); }));
}
if (match === null || match === void 0 ? void 0 : match.message)
matching && (matching = isMessageMatching(match === null || match === void 0 ? void 0 : match.message, log.message));
return matching;
}
function isMessageMatching(pattern, message) {
try {
const messageString = typeof message === "string" ? message : JSON.stringify(message);
return !!(messageString === null || messageString === void 0 ? void 0 : messageString.match(pattern));
}
catch (error) {
console.error("Error while matching", error);
return false;
}
}