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

feat: make check for allLogs more explicit #362

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
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) === false) {
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
Loading