0

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?

A T
  • 13,008
  • 21
  • 97
  • 158

1 Answers1

2

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...");
HeadCode
  • 2,770
  • 1
  • 14
  • 26
  • Thanks, actually `createLogger({log: createLogger({name: 'foo'})})` also works. Following your answer I added this issue: https://github.com/restify/node-restify/issues/1011 – A T Feb 13 '16 at 09:03
  • The docs were wrong, that issue I created catalyzed the doc change. – A T Feb 20 '16 at 04:29