Skip to content

Commit

Permalink
Print msg as an object if it is an object
Browse files Browse the repository at this point in the history
  • Loading branch information
mcollina committed Sep 21, 2018
1 parent f1bd53f commit 6e32d34
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 4 deletions.
10 changes: 7 additions & 3 deletions index.js
Expand Up @@ -153,7 +153,7 @@ module.exports = function prettyFactory (options) {

line += ': '

if (log[messageKey]) {
if (log[messageKey] && typeof log[messageKey] === 'string') {
line += color.message(log[messageKey])
}

Expand Down Expand Up @@ -190,7 +190,7 @@ module.exports = function prettyFactory (options) {
}
}
} else {
line += filterObjects(log, messageKey, errorLikeObjectKeys)
line += filterObjects(log, typeof log[messageKey] === 'string' ? messageKey : undefined, errorLikeObjectKeys)
}

return line
Expand All @@ -207,7 +207,11 @@ module.exports = function prettyFactory (options) {
errorLikeObjectKeys = errorLikeObjectKeys || []

const keys = Object.keys(value)
const filteredKeys = [messageKey]
const filteredKeys = []

if (messageKey) {
filteredKeys.push(messageKey)
}

if (excludeStandardKeys !== false) {
Array.prototype.push.apply(filteredKeys, standardKeys)
Expand Down
36 changes: 35 additions & 1 deletion test/basic.test.js
Expand Up @@ -5,7 +5,16 @@ const os = require('os')
const test = require('tap').test
const pino = require('pino')
const dateformat = require('dateformat')
const prettyFactory = require('../')
const _prettyFactory = require('../')

function prettyFactory (opts) {
if (!opts) {
opts = { colorize: false }
} else if (!opts.hasOwnProperty('colorize')) {
opts.colorize = false
}
return _prettyFactory(opts)
}

// All dates are computed from 'Fri, 30 Mar 2018 17:35:28 GMT'
const epoch = 1522431328992
Expand Down Expand Up @@ -451,5 +460,30 @@ test('basic prettifier tests', (t) => {
log.info('foo')
})

t.test('prettifies msg object', (t) => {
t.plan(6)
const expectedLines = [
' msg: {',
' "b": {',
' "c": "d"',
' }',
' }'
]
const pretty = prettyFactory()
const log = pino({}, new Writable({
write (chunk, enc, cb) {
const formatted = pretty(chunk.toString())
const lines = formatted.split('\n')
t.is(lines.length, expectedLines.length + 2)
lines.shift(); lines.pop()
for (var i = 0; i < lines.length; i += 1) {
t.is(lines[i], expectedLines[i])
}
cb()
}
}))
log.info({ msg: { b: { c: 'd' } } })
})

t.end()
})

0 comments on commit 6e32d34

Please sign in to comment.