Skip to content

Commit

Permalink
make check for all logs more explicit
Browse files Browse the repository at this point in the history
  • Loading branch information
todor-a committed Dec 9, 2024
1 parent 24af827 commit eb83df0
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
2 changes: 1 addition & 1 deletion logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ function pinoLogger (opts, stream) {
if (!res.log) {
res.log = responseLogger
}
if (!res.allLogs) {
if (!Array.isArray(res.allLogs)) {
res.allLogs = []
}
res.allLogs.push(responseLogger)
Expand Down
26 changes: 26 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,32 @@ test('internal pino logger not shared between multiple middleware', function (t)
t.not(middleware1.logger, middleware2.logger, 'expected loggers not to be shared between middleware invocations')
})

test('req.allLogs is correctly created if it does not exist', function (t) {
const dest = split(JSON.parse)
const logger = pinoHttp(dest)

function handler (req, res) {
delete req.allLogs

logger(req, res)

t.ok(Array.isArray(req.allLogs), 'req.allLogs should be an array')
t.equal(req.allLogs.length, 1, 'req.allLogs should have one logger entry')
t.equal(typeof req.allLogs[0].info, 'function', 'req.allLogs should contain a valid logger instance')

res.end('hello world')
}

setup(t, logger, function (err, server) {
t.error(err)
doGet(server)
}, handler)

dest.on('data', function () {
t.end()
})
})

test('when multiple pino middleware are present each pino logger retains its own redact config', function (t) {
t.plan(6)

Expand Down

0 comments on commit eb83df0

Please sign in to comment.