diff --git a/src/logger.ts b/src/logger.ts index 70f8a89..d312522 100644 --- a/src/logger.ts +++ b/src/logger.ts @@ -130,7 +130,18 @@ export class Logger { return ''; } - const loggableParams = params.map(p => (typeof p === 'object' ? JSON.stringify(p) : String(p))).join(', '); + const loggableParams = params + .map(p => { + if (typeof p !== 'object') return String(p); + + try { + return JSON.stringify(p); + } + catch { + return ``; + } + }) + .join(', '); return loggableParams || ''; } diff --git a/src/system/decorators.ts b/src/system/decorators.ts index eb9d927..c9945c7 100644 --- a/src/system/decorators.ts +++ b/src/system/decorators.ts @@ -61,6 +61,7 @@ export function log( enter?(this: any, ...args: any[]): string; exit?(this: any, result: any): string; prefix?(this: any, context: LogContext, ...args: any[]): string; + sanitize?(this: any, key: string, value: any): any; timed?: boolean; } = { args: true, timed: true } ) { @@ -139,10 +140,17 @@ export function log( loggable = options.args[p](v); } else { - loggable = - typeof v === 'object' - ? JSON.stringify(v, this.sanitizeSerializableParam) - : String(v); + if (typeof v === 'object') { + try { + loggable = JSON.stringify(v, options.sanitize); + } + catch { + loggable = ``; + } + } + else { + loggable = String(v); + } } return p ? `${p}=${loggable}` : loggable;