Restify req.getLogger is not a function

791 views Asked by At

How do I install bunyan logger so that on the routes they can get children of bunyan for their own loggers?

Attempt:

import * as restify from 'restify';
import {createLogger} from 'bunyan';

let app = restify.createServer();

app.use(restify.queryParser());
app.use(restify.bodyParser());

app.on('after', restify.auditLogger({
    log: createLogger({
        name: 'audit',
        stream: process.stdout
    })
}));

app.get('/foo', (req, res, next) => {
    req.getLogger('foo').info('bar');
    res.json({});
});

app.listen(process.env.PORT || 3000, function() {
    console.info('%s listening at %s', app.name, app.url);
});

req.getLogger should work as per http://restify.com/#getloggercomponent, but maybe there's an app.use step also required, but not mentioned in the docs?

1

There are 1 answers

2
HeadCode On BEST ANSWER

Here is how I did it in a recent application.

var Bunyan = require('bunyan');
var log = new Bunyan();

var server = restify.createServer({
    log: log
});

Then, wherever you have access to the request object you just use it like this.

req.log.error({req_id: req.id()}, "There was an error...");