feat: fix issue with redaction by implementing another way

Signed-off-by: blam <ben@blam.sh>
This commit is contained in:
blam
2024-05-15 14:49:40 +02:00
parent 2c2a8f8b14
commit 3e8148e39c
2 changed files with 10 additions and 12 deletions
@@ -80,7 +80,7 @@ describe('WinstonLogger', () => {
nested: '[REDACTED] (world) from nested object',
null: null,
nullProto: {
foo: 'hello foo', // read only prop is not redacted
foo: '[REDACTED] foo',
},
},
}),
@@ -86,19 +86,17 @@ export class WinstonLogger implements RootLoggerService {
let redactionPattern: RegExp | undefined = undefined;
const replace = (obj: TransformableInfo) => {
for (const key in obj) {
if (Object.hasOwn(obj, key)) {
if (typeof obj[key] === 'object') {
obj[key] = replace(obj[key] as TransformableInfo);
} else if (typeof obj[key] === 'string') {
try {
obj[key] = obj[key]?.replace(redactionPattern, '[REDACTED]');
} catch {
/* ignore read only properties */
}
}
const stringifiedFields = JSON.stringify(obj, null);
const redacted = JSON.parse(
stringifiedFields.replace(redactionPattern!, '[REDACTED]'),
);
for (const key in redacted) {
if (obj && Object.hasOwn(obj, key)) {
obj[key] = redacted[key];
}
}
return obj;
};
return {