01 · Before and afterThe pattern
const escape = (s: string) => s.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
function maskingLog(next: Handler, secrets: string[]): Handler {
const vals = secrets.filter(Boolean).map(escape);
if (!vals.length) return next;
const re = new RegExp(vals.join('|'), 'g'); // built once
return { event: e => next.event({ ...e,
text: e.text?.replace(re, '********'), detail: e.detail?.replace(re, '********') }) };
}02 · Run it on your appThe check
If one of your secrets contains a $ or a ., is it still masked in every log line and every structured log field?
If the honest answer is no or not sure, this is worth a look before it reaches production.