简体   繁体   English

使用Express Node JS记录

[英]Logging with Express Node JS

I want to know what are the option of logging with Express similar to what good is to HAPI. 我想知道使用Express进行登录的选项与对HAPI有何好处相似。 I tried Morgan but not sure how can I log response time and other response information. 我尝试了Morgan,但不确定如何记录响应时间和其他响应信息。

Thanks in advance for help, Devang Desai 在此先感谢您的帮助,Devang Desai

You could use custom token in Morgan. 您可以在Morgan中使用自定义令牌。

For example, if you wanna log the HTTP method, status code, response time, and process ID, you could use this 例如,如果您想记录HTTP方法,状态代码,响应时间和进程ID,则可以使用此方法

// Define a custom token 'pid'
morgan.token('pid', function (request, response) { return process.pid; });

// Do the logging
app.use(morgan(function (tokens, request, response) {
    var log = tokens['method'](request, response) + ' ';
    log += tokens['status'](request, response) + ' ';
    log += tokens['response-time'](request, response) + ' ms - PID: ' + tokens['pid'](request, response);
    return log;
}));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM