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

后端 未结 3 1011
孤街浪徒
孤街浪徒 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:27

    In Hapi.js version above v17, please make the below changes to make it work:

    server.events.on('response', function (request) {
        // you can use request.log or server.log it's depends
        server.log(request.info.remoteAddress + ': ' + request.method.toUpperCase() + ' ' + request.url.path + ' --> ' + request.response.statusCode);
    });
    

    The log will be:

    127.0.0.1: GET /todo --> 200
    

提交回复
热议问题