【发布时间】:2017-06-25 07:00:25
【问题描述】:
在我的小型 node.js 应用程序中,我想使用 express 记录所有传入的请求,所以我最终得到了这个:
var bodyParser = require('body-parser');
module.exports = function(app) {
app.set("port", 50001);
app.set("json spaces", 2);
app.use(bodyParser.json());
app.use(function (error, req, res, next) {
app.logger.info("received from "+req.get("X-Forwarded-For")+" : "+req.method+" "+req.originalUrl+" (Authorization: "+req.get("Authorization")+")");
//does not work if json is malformed
//app.logger.info("content :"+JSON.stringify(req.body));
if (error /*instanceof SyntaxError*/) {
res.status(400);
app.logger.error(error);
res.json({ error:{msg: error.message}});
} else {
next();
}
});
app.use(app.auth.initialize());
};
不幸的是,我只在出现错误时通过app.logger.info 行获取日志(在我的情况下,正文中的 JSON 字符串格式不正确)。我在这里错过了什么?
【问题讨论】:
标签: json node.js express logging winston