Skip to content

Commit

Permalink
Merge pull request invisible-tech#48 from invisible-tech/pv/serialize…
Browse files Browse the repository at this point in the history
…-logs

chore(devops): added serialization to logger for objects with circular deps
  • Loading branch information
cyrus-za authored Mar 2, 2020
2 parents c8281ce + 94ab73c commit b8a1cdd
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,6 @@ typings/
ignore

*.sublime-*

# vscode history extension
.history/
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
*v2.3.0*
> Fix: Improve performance by serializing objects that contain circular references
*v2.1.2*
> Internal: remove unneeded files
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@invisible/logger",
"version": "2.2.0",
"version": "2.3.0",
"description": "Invisible Logging Wrapper",
"main": "index.js",
"scripts": {
Expand Down
3 changes: 3 additions & 0 deletions transports/console.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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)}`
: ''
Expand Down
11 changes: 11 additions & 0 deletions transports/helpers/serialize.js
Original file line number Diff line number Diff line change
@@ -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
3 changes: 3 additions & 0 deletions transports/timber.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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
Expand Down

0 comments on commit b8a1cdd

Please sign in to comment.