13

RequestLogger

現在のリクエスト ID と、定義したその他のパラメーターを入力して、子の bunyan ロガーをセットアップします。

server.use(restify.requestLogger());

プラグインを登録しても、リクエストはログに記録されません。

4

1 に答える 1

24

RequestLoggerプラグインは各リクエストをログに記録しません。のプロパティを拡張.logreqます。

これを行う利点は、各 restify req インスタンスに新しい bunyan インスタンス ログがあり、そこにリクエスト ID が自動的に挿入されるため、高スループット ログを簡単に相互に関連付けることができることです。

最初に独自のロガーを登録する必要があります。

var restify = require('restify'),
    bunyan = require('bunyan'),
    // Create a logger.
    log = bunyan.createLogger({name: 'showtime'}),
    server;

// Register logger.
server = restify.createServer({
    log: log
});

// Extend logger using the plugin.
server.use(restify.requestLogger());

// Use req.log property as a regular instance of bunyan logger.
server.use(function (req, res, next) {
    req.log.info('TEST');
    next();
});

server.get('/ping', function (req, res, next) {
    res.send('pong');
    next();
});

server.listen(8080);

エンドポイントにいくつかのリクエストを/ping行うと、次のログが生成されます。

{"name":"showtime","hostname":"localhost.localdomain","pid":6446,"req_id":"3c734e70-81de-11e4-bc58-e3e7254ff287","level":30,"msg":"TEST","time":"2014-12-12T09:07:02.488Z","v":0}
{"name":"showtime","hostname":"localhost.localdomain","pid":6446,"req_id":"3d6bdf40-81de-11e4-bc58-e3e7254ff287","level":30,"msg":"TEST","time":"2014-12-12T09:07:04.116Z","v":0}
{"name":"showtime","hostname":"localhost.localdomain","pid":6446,"req_id":"3df47030-81de-11e4-bc58-e3e7254ff287","level":30,"msg":"TEST","time":"2014-12-12T09:07:05.011Z","v":0}

「req_id」プロパティに注意してください。

requestLoggerコードなしの同等のリクエスト

server.use(restify.requestLogger({
    log: log,
    serializers: restify.bunyan.serializers
}));

次のログが生成されます。

{"name":"showtime","hostname":"localhost.localdomain","pid":6448,"level":30,"msg":"TEST","time":"2014-12-12T09:07:23.099Z","v":0}
{"name":"showtime","hostname":"localhost.localdomain","pid":6448,"level":30,"msg":"TEST","time":"2014-12-12T09:07:24.527Z","v":0}
{"name":"showtime","hostname":"localhost.localdomain","pid":6448,"level":30,"msg":"TEST","time":"2014-12-12T09:07:24.674Z","v":0}

あなたの意図がすべてのリクエストをログに記録することである場合は、捨ててください

server.use(function (req, res, next) {
    req.log.info('TEST');
    next();
});

賛成:

server.on('after', restify.auditLogger({
    log: log
}));

これにより、ログが生成されます。

{"name":"showtime","hostname":"localhost.localdomain","pid":6451,"audit":true,"level":30,"remoteAddress":"192.168.100.1","remotePort":58986,"req_id":"00093110-81df-11e4-981e-e3d13800c8bf","req":{"method":"GET","url":"/ping","headers":{"user-agent":"curl/7.37.1","host":"192.168.100.100:8080","accept":"*/*"},"httpVersion":"1.1","trailers":{},"version":"*","timers":{"bunyan":378,"handler-1":3044}},"res":{"statusCode":200,"headers":{"content-type":"application/json","content-length":6},"trailer":false},"latency":6,"_audit":true,"msg":"handled: 200","time":"2014-12-12T09:12:30.629Z","v":0}
{"name":"showtime","hostname":"localhost.localdomain","pid":6451,"audit":true,"level":30,"remoteAddress":"192.168.100.1","remotePort":58989,"req_id":"2b2fcac0-81df-11e4-981e-e3d13800c8bf","req":{"method":"GET","url":"/ping","headers":{"user-agent":"curl/7.37.1","host":"192.168.100.100:8080","accept":"*/*"},"httpVersion":"1.1","trailers":{},"version":"*","timers":{"bunyan":105,"handler-1":607}},"res":{"statusCode":200,"headers":{"content-type":"application/json","content-length":6},"trailer":false},"latency":1,"_audit":true,"msg":"handled: 200","time":"2014-12-12T09:13:43.021Z","v":0}
{"name":"showtime","hostname":"localhost.localdomain","pid":6451,"audit":true,"level":30,"remoteAddress":"192.168.100.1","remotePort":58990,"req_id":"2b6532a0-81df-11e4-981e-e3d13800c8bf","req":{"method":"GET","url":"/ping","headers":{"user-agent":"curl/7.37.1","host":"192.168.100.100:8080","accept":"*/*"},"httpVersion":"1.1","trailers":{},"version":"*","timers":{"bunyan":7067,"handler-1":677}},"res":{"statusCode":200,"headers":{"content-type":"application/json","content-length":6},"trailer":false},"latency":8,"_audit":true,"msg":"handled: 200","time":"2014-12-12T09:13:43.371Z","v":0}
于 2014-12-12T09:19:44.137 に答える