Files
renovate/lib/logger/remap.ts
Michael Kriese e438887dfb build: lazy load and initialize logger (#41425)
* build: lazy load initialization modules

* build: lazy load and initialize logger

* chore: lint fix

* Update lib/constants/error-messages.ts

Co-authored-by: Jamie Tanna <jamie.tanna@mend.io>

* Apply suggestion

Co-authored-by: Jamie Tanna <jamie.tanna@mend.io>

---------

Co-authored-by: Jamie Tanna <jamie.tanna@mend.io>
2026-03-10 16:57:29 +00:00

68 lines
1.6 KiB
TypeScript

import type { StringMatchPredicate } from '../util/string-match.ts';
import { getRegexOrGlobPredicate } from '../util/string-match.ts';
import type { BunyanLogLevel, LogLevelRemap } from './types.ts';
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);
// v8 ignore else -- TODO: add test #40625
if (!matchFn) {
matchFn = getRegexOrGlobPredicate(pattern);
matcherCache.set(remap, matchFn);
}
return matchFn(input);
}
export function getRemappedLevel(msg: string): BunyanLogLevel | null {
if (repositoryRemaps) {
for (const remap of repositoryRemaps) {
// v8 ignore else -- TODO: add test #40625
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();
}