From 94ab73cedf675de105b79bd76afdb001da75e6ee Mon Sep 17 00:00:00 2001 From: Pieter Venter Date: Sat, 29 Feb 2020 06:09:20 +0800 Subject: [PATCH] chore(devops): added serialization to logger for objects with circular deps --- .gitignore | 3 +++ CHANGELOG.md | 3 +++ package.json | 2 +- transports/console.js | 3 +++ transports/helpers/serialize.js | 11 +++++++++++ transports/timber.js | 3 +++ 6 files changed, 24 insertions(+), 1 deletion(-) create mode 100644 transports/helpers/serialize.js diff --git a/.gitignore b/.gitignore index ac7b785..53d45a0 100644 --- a/.gitignore +++ b/.gitignore @@ -61,3 +61,6 @@ typings/ ignore *.sublime-* + +# vscode history extension +.history/ \ No newline at end of file diff --git a/CHANGELOG.md b/CHANGELOG.md index 2965733..11abf9c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,6 @@ +*v2.3.0* +> Fix: Improve performance by serializing objects that contain circular references + *v2.1.2* > Internal: remove unneeded files diff --git a/package.json b/package.json index 48590e2..fe12291 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@invisible/logger", - "version": "2.2.0", + "version": "2.3.0", "description": "Invisible Logging Wrapper", "main": "index.js", "scripts": { diff --git a/transports/console.js b/transports/console.js index e494458..5143d7d 100644 --- a/transports/console.js +++ b/transports/console.js @@ -3,6 +3,7 @@ const moment = require('moment') const winston = require('winston') const assertLevel = require('./helpers/assertLevel') +const serialize = require('./helpers/serialize') const { LOGGER_LEVEL = 'info', @@ -18,6 +19,8 @@ module.exports = new (winston.transports.Console)({ timestamp: () => moment().format(), formatter: options => { const { message } = options + // serialize objects such as SQL models by checking for toJSON + options.meta = serialize(options.meta) const meta = (options.meta && Object.keys(options.meta).length) ? `\n${JSON.stringify(options.meta, null, 2)}` : '' diff --git a/transports/helpers/serialize.js b/transports/helpers/serialize.js new file mode 100644 index 0000000..f9173ea --- /dev/null +++ b/transports/helpers/serialize.js @@ -0,0 +1,11 @@ +'use strict' + +const serialize = meta => { + if (! meta || typeof meta === 'string') return meta + if (meta.toJSON) return JSON.parse(meta.toJSON()) + Object.keys(meta).forEach(key => { + meta[key] = serialize(meta[key]) + }) + return meta +} +module.exports = serialize diff --git a/transports/timber.js b/transports/timber.js index dfa6be5..71b3535 100644 --- a/transports/timber.js +++ b/transports/timber.js @@ -5,6 +5,7 @@ const serializeError = require('serialize-error') const timber = require('timber') const assertLevel = require('./helpers/assertLevel') +const serialize = require('./helpers/serialize') const { TIMBER_LEVEL = 'info', @@ -22,6 +23,8 @@ module.exports = new (winston.transports.Console)({ name: 'timber', level: TIMBER_LEVEL, formatter: options => { + // serialize objects such as SQL models by checking for toJSON + options.meta = serialize(options.meta) // When we log errors, options.message comes as blank and it causes errors const message = options.message || options.meta.message || 'No message' // Serialize eventual error objecs