MCPcopy Index your code
hub / github.com/restify/node-restify / requestLogger

Function requestLogger

lib/plugins/requestLogger.js:42–88  ·  view source on GitHub ↗

* Sets up a child [logger](https://github.com/pinojs/pino) logger with * the current request id filled in, along with any other parameters you define. * * You can pass in no options to this, in which case only the request id will be * appended, and no serializers appended (this is also the most

(options)

Source from the content-addressed store, hash-verified

40 * }));
41 */
42function requestLogger(options) {
43 assert.optionalObject(options);
44 var opts = options || {};
45
46 var props;
47
48 if (opts.properties) {
49 props = shallowCopy(opts.properties);
50 } else {
51 props = {};
52 }
53
54 if (opts.serializers) {
55 props.serializers = opts.serializers;
56 }
57
58 var headersToCopy = opts.headers || [];
59 const requestIdFieldName = opts.requestIdFieldName || 'req_id';
60
61 return function logger(req, res, next) {
62 if (!req.log && !opts.log) {
63 next();
64 return;
65 }
66
67 var log = req.log || opts.log;
68
69 props[requestIdFieldName] = req.getId();
70
71 headersToCopy.forEach(function forEach(k) {
72 if (req.headers[k]) {
73 props[k] = req.headers[k];
74 }
75 });
76 const childOptions = {};
77 if (props.serializers) {
78 childOptions.serializers = props.serializers;
79 }
80 req.log = log.child(props, childOptions);
81
82 if (props[requestIdFieldName]) {
83 delete props[requestIdFieldName];
84 }
85
86 next();
87 };
88}
89
90///--- Exports
91

Callers

nothing calls this directly

Calls 2

shallowCopyFunction · 0.50
nextFunction · 0.50

Tested by

no test coverage detected