Skip to content

Commit

Permalink
Merge pull request #49 from JasonEtco/omit-empty-brackets
Browse files Browse the repository at this point in the history
Omit empty brackets if log.time is falsey
  • Loading branch information
mcollina committed Dec 3, 2018
2 parents 8101d3b + 4ce70fa commit f6f3009
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
7 changes: 5 additions & 2 deletions index.js
Expand Up @@ -120,15 +120,18 @@ module.exports = function prettyFactory (options) {
log.time = formatTime(log.time, opts.translateTime)
}

var line = `[${log.time}]`
var line = log.time ? `[${log.time}]` : ''

const coloredLevel = levels.hasOwnProperty(log.level)
? color[log.level](levels[log.level])
: color.default(levels.default)
if (opts.levelFirst) {
line = `${coloredLevel} ${line}`
} else {
line = `${line} ${coloredLevel}`
// If the line is not empty (timestamps are enabled) output it
// with a space after it - otherwise output the empty string
const lineOrEmpty = line && line + ' '
line = `${lineOrEmpty}${coloredLevel}`
}

if (log.name || log.pid || log.hostname) {
Expand Down
2 changes: 1 addition & 1 deletion test/basic.test.js
Expand Up @@ -276,7 +276,7 @@ test('basic prettifier tests', (t) => {
const log = pino({ timestamp: null }, new Writable({
write (chunk, enc, cb) {
const formatted = pretty(chunk.toString())
t.is(formatted, `[undefined] INFO (${pid} on ${hostname}): hello world\n`)
t.is(formatted, `INFO (${pid} on ${hostname}): hello world\n`)
cb()
}
}))
Expand Down

0 comments on commit f6f3009

Please sign in to comment.