Skip to content

Commit

Permalink
suppport message key in pino/browser (#1980)
Browse files Browse the repository at this point in the history
  • Loading branch information
emmyakin committed May 21, 2024
1 parent 0109b15 commit 8db130e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
11 changes: 6 additions & 5 deletions browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,8 @@ function pino (opts) {
asObject: opts.browser.asObject,
formatters: opts.browser.formatters,
levels,
timestamp: getTimeFunction(opts)
timestamp: getTimeFunction(opts),
messageKey: opts.messageKey || 'msg'
}
logger.levels = getLevels(opts)
logger.level = level
Expand Down Expand Up @@ -308,7 +309,7 @@ function createWrap (self, opts, rootLogger, level) {
argsIsSerialized = true
}
if (opts.asObject || opts.formatters) {
write.call(proto, asObject(this, level, args, ts, opts.formatters))
write.call(proto, asObject(this, level, args, ts, opts))
} else write.apply(proto, args)

if (opts.transmit) {
Expand All @@ -330,11 +331,11 @@ function createWrap (self, opts, rootLogger, level) {
})(self[baseLogFunctionSymbol][level])
}

function asObject (logger, level, args, ts, formatters = {}) {
function asObject (logger, level, args, ts, opts) {
const {
level: levelFormatter,
log: logObjectFormatter = (obj) => obj
} = formatters
} = opts.formatters || {}
const argsCloned = args.slice()
let msg = argsCloned[0]
const logObject = {}
Expand All @@ -358,7 +359,7 @@ function asObject (logger, level, args, ts, formatters = {}) {
}
msg = argsCloned.length ? format(argsCloned.shift(), argsCloned) : undefined
} else if (typeof msg === 'string') msg = format(argsCloned.shift(), argsCloned)
if (msg !== undefined) logObject.msg = msg
if (msg !== undefined) logObject[opts.messageKey] = msg

const formattedLogObject = logObjectFormatter(logObject)
return formattedLogObject
Expand Down
17 changes: 17 additions & 0 deletions test/browser.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,23 @@ test('opts.browser.asObject logs pino-like object to console', ({ end, ok, is })
instance.info('test')
end()
})
test('opts.browser.asObject uses opts.messageKey in logs', ({ end, ok, is }) => {
const messageKey = 'message'
const instance = require('../browser')({
messageKey,
browser: {
asObject: true,
write: function (o) {
is(o.level, 30)
is(o[messageKey], 'test')
ok(o.time)
}
}
})

instance.info('test')
end()
})

test('opts.browser.formatters (level) logs pino-like object to console', ({ end, ok, is }) => {
const info = console.info
Expand Down

0 comments on commit 8db130e

Please sign in to comment.