From 2921aebc828b76d130afe6f2525f4374d6c624f9 Mon Sep 17 00:00:00 2001 From: Eric Amodio Date: Sun, 21 Oct 2018 00:39:10 -0400 Subject: [PATCH] Adds traps for stringify errors Fixes sanitize support --- src/logger.ts | 13 ++++++++++++- src/system/decorators.ts | 16 ++++++++++++---- 2 files changed, 24 insertions(+), 5 deletions(-) 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;