@autorest/common
Version:
Autorest common utilities
73 lines • 2.85 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.shouldLogLevel = exports.FilterLogger = void 0;
const datastore_1 = require("@azure-tools/datastore");
const utils_1 = require("../utils");
/**
* Logger adding filtering functionality based on:
* - level: only show log with level higher than the configuration.
* - suppression: List of code that should not be logged.
*/
class FilterLogger {
constructor(options) {
var _a, _b;
this.level = options.level;
this.suppressions = (_b = (_a = options.suppressions) === null || _a === void 0 ? void 0 : _a.map((x) => ({ ...x, code: x.code.toLowerCase() }))) !== null && _b !== void 0 ? _b : [];
}
process(log) {
if (!shouldLogLevel(log, this.level)) {
return;
}
return this.filterSuppressions(log);
}
filterSuppressions(log) {
var _a, _b, _c;
const hadSource = log.source && log.source.length > 0;
let currentLog = log;
const key = (_a = log.code) === null || _a === void 0 ? void 0 : _a.toLowerCase();
// filter
for (const sup of this.suppressions) {
// matches key
if (key && (key === sup.code || key.startsWith(`${sup.code}/`))) {
// filter applicable sources
if (log.source && hadSource) {
currentLog = {
...currentLog,
source: (_b = currentLog.source) === null || _b === void 0 ? void 0 : _b.filter((s) => !this.matchesSourceFilter(s.document, s.position.path, sup)),
};
}
else {
return undefined;
}
}
}
// drop message if all source locations have been stripped
if (hadSource && ((_c = currentLog.source) === null || _c === void 0 ? void 0 : _c.length) === 0) {
return undefined;
}
return log;
}
matchesSourceFilter(document, path, supression) {
// from
const from = (0, utils_1.arrayify)(supression.from);
const matchesFrom = from.length === 0 || from.find((d) => document.toLowerCase().endsWith(d.toLowerCase()));
// where
const where = (0, utils_1.arrayify)(supression.where);
const matchesWhere = where.length === 0 || (path && where.find((w) => (0, datastore_1.matches)(w, path))) || false;
return Boolean(matchesFrom && matchesWhere);
}
}
exports.FilterLogger = FilterLogger;
const LOG_LEVEL = {
debug: 10,
verbose: 20,
information: 30,
warning: 40,
error: 50,
fatal: 60,
};
function shouldLogLevel(log, level) {
return LOG_LEVEL[log.level] >= LOG_LEVEL[level];
}
exports.shouldLogLevel = shouldLogLevel;
//# sourceMappingURL=filter-logger-processor.js.map