diff --git a/lib/LoggingEvent.js b/lib/LoggingEvent.js index 60ce832..5732a27 100644 --- a/lib/LoggingEvent.js +++ b/lib/LoggingEvent.js @@ -35,9 +35,20 @@ class LoggingEvent { const logData = this.data.map((e) => { // JSON.stringify(new Error('test')) returns {}, which is not really useful for us. // The following allows us to serialize errors correctly. + // duck-typing for Error object if (e && e.message && e.stack) { e = Object.assign({ message: e.message, stack: e.stack }, e); } + // JSON.stringify({a: parseInt('abc'), b: 1/0, c: -1/0}) returns {a: null, b: null, c: null}. + // The following allows us to serialize to NaN, Infinity and -Infinity correctly. + else if (typeof e === 'number' && (isNaN(e) || !isFinite(e))) { + e = e.toString(); + } + // JSON.stringify([undefined]) returns [null]. + // The following allows us to serialize to undefined correctly. + else if (typeof e === 'undefined') { + e = typeof e; + } return e; }); this.data = logData;