Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

suppport messageKey in pino/browser #1980

Merged
merged 1 commit into from
May 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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