Skip to content

Commit

Permalink
feat!: don't override req.log if set during .first
Browse files Browse the repository at this point in the history
  • Loading branch information
mmarchini committed Jan 17, 2023
1 parent 528ecbc commit 852d2c1
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
8 changes: 7 additions & 1 deletion lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -1294,7 +1294,13 @@ Server.prototype._setupRequest = function _setupRequest(req, res) {

// Extend request
req._dtraceId = dtrace.nextId();
req.log = res.log = self.log;
// if log is set on .first, don't override it
if (req.log === undefined) {
req.log = self.log;
}
if (res.log === undefined) {
res.log = req.log;
}
req._date = new Date();
req._timeStart = process.hrtime();
req.serverName = self.name;
Expand Down
41 changes: 41 additions & 0 deletions test/server.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2953,3 +2953,44 @@ test('Server correctly handles multiple clientError listeners', function(t) {
});
}).end();
});

test('req and res should use server logger by default', function(t) {
SERVER.get('/ping', function echoId(req, res, next) {
t.ok(req.log);
t.strictEqual(req.log, SERVER.log);
req.log.info('foo');
t.equal(LOG_BUFFER.records[LOG_BUFFER.length - 1].msg, 'foo');
res.log.info('bar');
t.equal(LOG_BUFFER.records[LOG_BUFFER.length - 1].msg, 'bar');
res.send();
next();
});

CLIENT.get('/ping', function() {
t.end();
});
});

test('req and res should use own logger by if set during .first', function(t) {
const buffer = new StreamRecorder();
SERVER.first(function first(req, res) {
req.log = helper.getLog('server', buffer, 'info');
});

SERVER.get('/ping', function echoId(req, res, next) {
LOG_BUFFER.flushRecords();
t.ok(req.log);
t.notStrictEqual(req.log, SERVER.log);
req.log.info('foo');
t.equal(buffer.records[buffer.length - 1].msg, 'foo');
res.log.info('bar');
t.equal(buffer.records[buffer.length - 1].msg, 'bar');
t.equal(LOG_BUFFER.records.length, 0);
res.send();
next();
});

CLIENT.get('/ping', function() {
t.end();
});
});

0 comments on commit 852d2c1

Please sign in to comment.