Skip to content

Commit

Permalink
Respect messageKey option when mergeHapiLogData is set (#138)
Browse files Browse the repository at this point in the history
  • Loading branch information
lauraseidler committed Jun 1, 2021
1 parent 73d8a2c commit b154215
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ async function register (server, options) {
}

const mergeHapiLogData = options.mergeHapiLogData
const messageKey = options.messageKey || 'msg'
const getChildBindings = options.getChildBindings ? options.getChildBindings : (request) => ({ req: request })
const shouldLogRequestStart = typeof options.logRequestStart === 'function'
? (request) => options.logRequestStart(request)
Expand Down Expand Up @@ -244,7 +245,7 @@ async function register (server, options) {
var logObject
if (mergeHapiLogData) {
if (typeof data === 'string') {
data = { msg: data }
data = { [messageKey]: data }
}

logObject = Object.assign({ tags }, data)
Expand Down
25 changes: 25 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1275,6 +1275,31 @@ experiment('logging with mergeHapiLogData option enabled', () => {
server.log(['info'], 'hello world')
await finish
})

test('respects `messageKey` option', async () => {
const server = getServer()
let done
const finish = new Promise(function (resolve, reject) {
done = resolve
})
const stream = sink(data => {
expect(data).to.include({ message: 'hello world' })
done()
})
const plugin = {
plugin: Pino,
options: {
stream: stream,
level: 'info',
mergeHapiLogData: true,
messageKey: 'message'
}
}

await server.register(plugin)
server.log(['info'], 'hello world')
await finish
})
})

experiment('custom serializers', () => {
Expand Down

0 comments on commit b154215

Please sign in to comment.