(value: unknown, depth = 0)
| 34 | const MAX_REDACT_DEPTH = 5; |
| 35 | |
| 36 | function redactSensitive(value: unknown, depth = 0): unknown { |
| 37 | if (value instanceof Error) { |
| 38 | return { |
| 39 | ...value, |
| 40 | message: value.message, |
| 41 | stack: value.stack, |
| 42 | name: value.name, |
| 43 | }; |
| 44 | } |
| 45 | if ( |
| 46 | depth >= MAX_REDACT_DEPTH || |
| 47 | value === null || |
| 48 | typeof value !== 'object' |
| 49 | ) { |
| 50 | return value; |
| 51 | } |
| 52 | if (value instanceof Date) { |
| 53 | return value; |
| 54 | } |
| 55 | if (Array.isArray(value)) { |
| 56 | return value.map((v) => redactSensitive(v, depth + 1)); |
| 57 | } |
| 58 | |
| 59 | const result: Record<string, unknown> = {}; |
| 60 | for (const [key, val] of Object.entries(value as Record<string, unknown>)) { |
| 61 | const lowered = key.toLowerCase(); |
| 62 | if (SENSITIVE_KEY_PATTERNS.some((k) => lowered.includes(k))) { |
| 63 | result[key] = '[REDACTED]'; |
| 64 | } else { |
| 65 | result[key] = redactSensitive(val, depth + 1); |
| 66 | } |
| 67 | } |
| 68 | return result; |
| 69 | } |
| 70 | |
| 71 | export function createLogger({ name }: { name: string }): ILogger { |
| 72 | const service = [process.env.LOG_PREFIX, name, process.env.NODE_ENV ?? 'dev'] |
no test coverage detected