Skip to content

Commit cbd16ef

Browse files
josephharringtonmmarchini
authored andcommitted
feat: allow alternate name for request id in logs
Right now the child loggers add the request id attribute under the field name `req_id`. This change adds an option to override the field name. This allows more customizable logs for easier integration with backend logging frameworks.
1 parent f8beaae commit cbd16ef

File tree

4 files changed

+81
-4
lines changed

4 files changed

+81
-4
lines changed

lib/plugins/audit.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ function getResponseHeaders(res) {
5252
* via the logger.
5353
* @param {Object} [opts.serializers] - Override the default logger serializers
5454
* for err, req and res
55+
* @param {String} [opts.requestIdFieldName] - The name of the request id property attached
56+
* to log lines. Defaults to "req_id".
5557
* @returns {Function} Handler
5658
* @fires audit when an audit log has been generated
5759
* @example
@@ -195,6 +197,7 @@ function auditLogger(opts) {
195197

196198
var server = opts.server;
197199
var printLog = opts.printLog;
200+
const requestIdFieldName = opts.requestIdFieldName || 'req_id';
198201

199202
if (typeof printLog === 'undefined') {
200203
printLog = true;
@@ -270,7 +273,7 @@ function auditLogger(opts) {
270273
var obj = {
271274
remoteAddress: req.connection.remoteAddress,
272275
remotePort: req.connection.remotePort,
273-
req_id: req.getId(),
276+
[requestIdFieldName]: req.getId(),
274277
req: req,
275278
res: res,
276279
err: err,

lib/plugins/requestLogger.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ var shallowCopy = require('./utils/shallowCopy');
2525
* @param {Object} [options] - an options object
2626
* @param {Array} [options.headers] - A list of headers to transfer from
2727
* the request to top level props on the log.
28+
* @param {Object} [options.properties] - A set of key-values to pass to the child logger
29+
* @param {Object} [options.serializers] - Override serializers to use in the child logger
30+
* @param {Object} [options.log] - A logger to use as a fallback if req.log is missing
31+
* @param {String} [options.requestIdFieldName] - The name of the request id property attached
32+
* to log lines. Defaults to "req_id".
2833
* @returns {Function} Handler
2934
* @example
3035
* server.use(restify.plugins.requestLogger({
@@ -51,6 +56,7 @@ function requestLogger(options) {
5156
}
5257

5358
var headersToCopy = opts.headers || [];
59+
const requestIdFieldName = opts.requestIdFieldName || 'req_id';
5460

5561
return function logger(req, res, next) {
5662
if (!req.log && !opts.log) {
@@ -60,7 +66,8 @@ function requestLogger(options) {
6066

6167
var log = req.log || opts.log;
6268

63-
props.req_id = req.getId();
69+
props[requestIdFieldName] = req.getId();
70+
6471
headersToCopy.forEach(function forEach(k) {
6572
if (req.headers[k]) {
6673
props[k] = req.headers[k];
@@ -72,8 +79,8 @@ function requestLogger(options) {
7279
}
7380
req.log = log.child(props, childOptions);
7481

75-
if (props.req_id) {
76-
delete props.req_id;
82+
if (props[requestIdFieldName]) {
83+
delete props[requestIdFieldName];
7784
}
7885

7986
next();

test/plugins/audit.test.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -625,4 +625,38 @@ describe('audit logger', function() {
625625
function(err, req, res) {}
626626
);
627627
});
628+
629+
it('should set request id using supplied field name', function(done) {
630+
SERVER.once(
631+
'after',
632+
restify.plugins.auditLogger({
633+
log: pino({ name: 'audit' }),
634+
server: SERVER,
635+
event: 'after',
636+
requestIdFieldName: 'traceId'
637+
})
638+
);
639+
640+
SERVER.once('audit', function(data) {
641+
assert.ok(data);
642+
assert.ok(data.traceId);
643+
assert.notOk(data.req_id);
644+
done();
645+
});
646+
647+
SERVER.get('/audit', function(req, res, next) {
648+
setTimeout(function() {
649+
res.send();
650+
next();
651+
}, 150);
652+
});
653+
654+
CLIENT.get(
655+
{
656+
path: '/audit',
657+
requestTimeout: 50
658+
},
659+
function(err, req, res) {}
660+
);
661+
});
628662
});

test/plugins/plugins.test.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,39 @@ describe('all other plugins', function() {
9595
done();
9696
});
9797
});
98+
99+
it('adds the request id to logs', function(done) {
100+
SERVER.use(restify.plugins.requestLogger());
101+
SERVER.get('/requestLogger/test', function(req, res, next) {
102+
var childings = req.log[pino.symbols.chindingsSym];
103+
assert.match(childings, /"req_id":"[0-9A-F-]+"/i);
104+
res.send();
105+
next();
106+
});
107+
CLIENT.get('/requestLogger/test', function(err, _, res) {
108+
assert.equal(res.statusCode, 200);
109+
assert.ifError(err);
110+
done();
111+
});
112+
});
113+
114+
it('adds the request id with a custom field name', function(done) {
115+
SERVER.use(
116+
restify.plugins.requestLogger({ requestIdFieldName: 'traceId' })
117+
);
118+
SERVER.get('/requestLogger/test', function(req, res, next) {
119+
var childings = req.log[pino.symbols.chindingsSym];
120+
assert.match(childings, /"traceId":"[0-9A-F-]+"/i);
121+
assert.notMatch(childings, /"req_id"/);
122+
res.send();
123+
next();
124+
});
125+
CLIENT.get('/requestLogger/test', function(err, _, res) {
126+
assert.equal(res.statusCode, 200);
127+
assert.ifError(err);
128+
done();
129+
});
130+
});
98131
});
99132

100133
describe('full response', function() {

0 commit comments

Comments
 (0)