如何在hapi.js服务器中编写类似访问日志的Apache

时间:2015-08-03 19:32:38

标签: node.js hapijs

我刚接触到hapijs,并制作了一个简单的服务器。现在我想要写一个像access.log这样的apache。最好的方法是什么?

我是否可以仅使用hapijs服务器执行此操作,还是应该更好地尝试集成winston来执行此操作?如果第二个更好,你知道如何整合它。

2 个答案:

答案 0 :(得分:1)

最简单的方法是将good模块与good-apache-log记者一起使用。以下是如何使用它的示例:

var Hapi = require('hapi');
var Good = require('good');

var server = new Hapi.Server();
server.connection({ port: 8080 });

server.route({
    method: 'GET',
    path: '/',
    handler: function (request, reply) {

        reply('Hello, world!');
    }
});

server.route({
    method: 'GET',
    path: '/{name}',
    handler: function (request, reply) {

        reply('Hello, ' + encodeURIComponent(request.params.name) + '!');
    }
});

server.register({
    register: Good,
    options: {
        reporters: [{
            reporter: require('good-apache-log'),
            events: {
                response: '*',
                log: '*'
            },
            config: {
                path: '/var/log/hapi',
                rotate: 'daily'
            }
        }]
    }
}, function (err) {

    if (err) {
        throw err; // something bad happened loading the plugin
    }

    server.start(function () {

        server.log('info', 'Server running at: ' + server.info.uri);
    });
});

答案 1 :(得分:0)

要获得类似于Apache访问日志的内容,您可以使用GoodGood-File记者并记录所有response个事件:

var Hapi = require('hapi');

var server = new Hapi.Server();
server.connection({ port: 4000 });

var options = {
    reporters: [
        {
            reporter: require('good-file'),
            events: { response: '*' },
            config: 'access.log'                 // The file to log to
        }
    ]
};

server.register([
    {
        register: require('good'),
        options: options
    }
], 
function (err) {

    if (err) {
        throw err;
    }

    server.start(function (err) {

        if (err) {
            throw err;
        }

        console.log('Server started!');
    });
});

它会将每个请求的大量数据记录到文件中,但可以使用配置选项对其进行过滤:

404响应的示例日志项:

{
   "event":"response",
   "timestamp":1438690799059,
   "id":"1438690799059:Matts-iMac.local:12134:icxbcqzv:10000",
   "instance":"http://Matts-iMac.local:4000",
   "labels":[

   ],
   "method":"get",
   "path":"/;jkdghkjsdgf",
   "query":{

   },
   "responseTime":19,
   "statusCode":404,
   "pid":12134,
   "source":{
      "remoteAddress":"127.0.0.1",
      "userAgent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.125 Safari/537.36"
   },
   "log":[
      {
         "request":"1438690799059:Matts-iMac.local:12134:icxbcqzv:10000",
         "timestamp":1438690799063,
         "tags":[
            "received"
         ],
         "data":{
            "id":"1438690799059:Matts-iMac.local:12134:icxbcqzv:10000",
            "method":"get",
            "url":"/;jkdghkjsdgf",
            "agent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.125 Safari/537.36"
         },
         "internal":true
      },
      {
         "request":"1438690799059:Matts-iMac.local:12134:icxbcqzv:10000",
         "timestamp":1438690799067,
         "tags":[
            "handler",
            "error"
         ],
         "data":{
            "msec":0.7662460058927536,
            "error":"Not Found",
            "data":{
               "data":null,
               "isBoom":true,
               "isServer":false,
               "output":{
                  "statusCode":404,
                  "payload":{
                     "statusCode":404,
                     "error":"Not Found"
                  },
                  "headers":{
                     "content-type":"application/json; charset=utf-8",
                     "cache-control":"no-cache",
                     "content-encoding":"gzip",
                     "vary":"accept-encoding"
                  }
               },
               "message":"Not Found"
            }
         },
         "internal":true
      },
      {
         "request":"1438690799059:Matts-iMac.local:12134:icxbcqzv:10000",
         "timestamp":1438690799078,
         "tags":[
            "response"
         ],
         "internal":true
      }
   ]
}