Browse Source

Adds traps for stringify errors

Fixes sanitize support
main
Eric Amodio 6 years ago
parent
commit
2921aebc82
2 changed files with 24 additions and 5 deletions
  1. +12
    -1
      src/logger.ts
  2. +12
    -4
      src/system/decorators.ts

+ 12
- 1
src/logger.ts View File

@ -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 `<error>`;
}
})
.join(', ');
return loggableParams || '';
}

+ 12
- 4
src/system/decorators.ts View File

@ -61,6 +61,7 @@ export function log(
enter?(this: any, ...args: any[]): string;
exit?(this: any, result: any): string;
prefix?(this: any, context: LogContext<T>, ...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 = `<error>`;
}
}
else {
loggable = String(v);
}
}
return p ? `${p}=${loggable}` : loggable;

Loading…
Cancel
Save