0
0
mirror of https://github.com/renovatebot/renovate.git synced 2024-12-22 05:28:35 +00:00
renovatebot_renovate/lib/logger/remap.ts
2024-08-19 13:15:27 +00:00

67 lines
1.6 KiB
TypeScript

import type { LogLevelString } from 'bunyan';
import type { StringMatchPredicate } from '../util/string-match';
import { getRegexOrGlobPredicate } from '../util/string-match';
import type { LogLevelRemap } from './types';
let globalRemaps: LogLevelRemap[] | undefined;
let repositoryRemaps: LogLevelRemap[] | undefined;
let matcherCache = new WeakMap<LogLevelRemap, StringMatchPredicate>();
function match(remap: LogLevelRemap, input: string): boolean {
const { matchMessage: pattern } = remap;
let matchFn = matcherCache.get(remap);
if (!matchFn) {
matchFn = getRegexOrGlobPredicate(pattern);
matcherCache.set(remap, matchFn);
}
return matchFn(input);
}
export function getRemappedLevel(msg: string): LogLevelString | null {
if (repositoryRemaps) {
for (const remap of repositoryRemaps) {
if (match(remap, msg)) {
return remap.newLogLevel;
}
}
}
if (globalRemaps) {
for (const remap of globalRemaps) {
if (match(remap, msg)) {
return remap.newLogLevel;
}
}
}
return null;
}
function resetMatcherCache(): void {
matcherCache = new WeakMap();
}
export function setGlobalLogLevelRemaps(
remaps: LogLevelRemap[] | undefined,
): void {
globalRemaps = remaps;
}
export function resetGlobalLogLevelRemaps(): void {
globalRemaps = undefined;
resetMatcherCache();
}
export function setRepositoryLogLevelRemaps(
remaps: LogLevelRemap[] | undefined,
): void {
repositoryRemaps = remaps;
}
export function resetRepositoryLogLevelRemaps(): void {
repositoryRemaps = undefined;
resetMatcherCache();
}