Skip to content

1.4 to 2.0 Migration Tips

cachafla edited this page Oct 15, 2012 · 15 revisions

(the GH wiki eats this h1)

restify.bunyan.serializers

Symptom: You might see the following error:

/opt/smartdc/imgapi/node_modules/bunyan/lib/bunyan.js:365
        throw new TypeError(format(
              ^
TypeError: invalid serializer for "res" field: must be a function
    at .../node_modules/bunyan/lib/bunyan.js:365:15
    at Array.forEach (native)
    at addSerializers (.../node_modules/bunyan/lib/bunyan.js:362:30)
    at new Logger (.../node_modules/bunyan/lib/bunyan.js:392:5)
    at Function.createLogger (.../node_modules/bunyan/lib/bunyan.js:1085:10)
    at handleArgv (.../main.js:120:18)
    at main (.../main.js:154:16)
    ...
    at Object.Module._extensions..js (module.js:467:10)

In restify 1.4 restify.bunyan.serializers includes a response field that you'd want to use like this:

var log = bunyan.createLogger({
  name: 'whatever',
  serializers: {
    res: restify.bunyan.serializers.response,
    req: bunyan.stdSerializers.req,
    err: bunyan.stdSerializers.err,
    ...
  },
  ...
});

In restify 2.0 the keys restify.bunyan.serializers actually match well-known log record field names (req for a request, res for a response). Also, restify includes more serializers (client_req and client_res are new) and re-exports the bunyan stdSerializers (at least the current set). Typical usage would now be:

var log = bunyan.createLogger({
  name: 'whatever',
  serializers: restify.bunyan.serializers,
  ...
});

Slightly less convenient if you have your own custom serializers, but generally a win.

req.path

If you used req.path to check which of your endpoints was called, keep in mind that req.path is now a function in restify 2.0. This method is useful when you are sharing a pre (or post) function across different endpoint handlers and need to know what API action is being called.

In restify 2.0 req.path() is now a function:

function pre(req, res, next) {
    console.log(req.path());
    return next();
}
Clone this wiki locally