javascript 如何在不使用日志库的情况下记录对 hapi 服务器的所有请求?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29746259/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 10:59:47  来源:igfitidea点击:

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

javascriptlogginghapijs

提问by mik01aj

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 I couldn't understand enough of it to get it working.

我想看到一个很好的日志,其中包含对我的服务器的每个请求的简短信息,以便在开发过程中使用。我已经看过http://hapijs.com/api#request-logs上的文档,但我对它的理解不够,无法让它工作。

What should I pass as the configobject when I create the server? Should I then listen to events and log them or does it happen automatically? How do I log all the requests, and not just the errors?

config创建服务器时应该传递什么作为对象?然后我应该监听事件并记录它们还是自动发生?如何记录所有请求,而不仅仅是错误?

I'd like to avoid installing logging libraries.

我想避免安装日志库。

回答by mik01aj

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

为 Hapi v18 编辑的答案,请在此处查看旧版本

回答by Vipin

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

在 v17 以上的 Hapi.js 版本中,请进行以下更改以使其工作:

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

回答by Gergo Erdosi

The easiest would be to use the goodmodule with one of the goodreporters, for example good-file. Here is an example how to use it:

最简单的方法是将good模块与其中一个good记者一起使用,例如good-file。这是一个如何使用它的示例:

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-file'),
            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);
    });
});