如何在不使用日志记录库的情况下记录对hapi服务器发出的所有请求? [英] How to log all requests made to a hapi server without using a logging library?

查看:65
本文介绍了如何在不使用日志记录库的情况下记录对hapi服务器发出的所有请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望看到一个很好的日志,其中包含有关我的服务器的每个请求的简短信息,以便在开发期间使用。我见过有关 http://hapijs.com/api#request-logs 的文档,但我不能了解它足以让它工作。

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.

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

What should I pass as the config object 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?

我想避免安装日志库。

推荐答案

所以我找到了办法:

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

日志看起来像这样:

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

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

这篇关于如何在不使用日志记录库的情况下记录对hapi服务器发出的所有请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆