How to log all requests made to a hapi server without using a logging library?

后端 未结 3 1012
孤街浪徒
孤街浪徒 2021-02-18 17:57

I\'d like to see a nice log with short info about each request to my server, for use during development. I\'ve seen the documentation on http://hapijs.com/api#request-logs, but

3条回答
  •  无人及你
    2021-02-18 18:24

    So I found a way:

    server.events.on('response', function (request) {
        console.log(request.info.remoteAddress + ': ' + request.method.toUpperCase() + ' ' + request.path + ' --> ' + request.response.statusCode);
    });
    

    The log then looks like this:

    127.0.0.1: GET /myEndpoint/1324134?foo=bar --> 200
    127.0.0.1: GET /sgsdfgsdrh --> 404
    

    Answer edited for Hapi v18, see older versions here

提交回复
热议问题