我一直在寻找bunyan
登录我的 nodejs 应用程序。我试过了,一切似乎都很好。我跳过了关于的部分log.child
,但现在我正在尝试了解如何使用它。我认为它的目的是允许我为日志条目指定一些特殊的标识符,以便我可以唯一地识别该日志如何与其他日志条目相关联。
如果是这样的话,我会设想自己log.child
在每个请求中使用:
var bunyan = require('bunyan');
var log = bunyan.createLogger({name: 'myapp'});
router.post('/submit', function(req, res) {
var logChild = log.child({reqId: uuid.v4()});
logChild.info({ req:req }, req.user.name + ' has called /submit');
saveData(req)
.then(function(data) {
logChild.info({data: data}, req.user.name + ' has saved to DB successfully in /submit');
res.json({'success': 'Saved!'});
})
.error(function(err) {
logChild.error({ err: err }, req.user.name + ' has caused an error in /submit ');
res.status(500).json("error": err});
});
});
这样,如果用户在 30 秒内两次Bob
POST /submit
,则会有一些上下文来区分日志文件中的两个不同调用。
也就是说,我会看到这样的东西(有上下文):
Bob has called /submit uuid: 109156be-c4fb-41ea-b1b4-efe1671c5836
Bob has called /submit uuid: 49dlsd7i-dapd-fdio-fei0-sd59fd0ph34d
Bob has saved to DB successfully in /submit uuid: 109156be-c4fb-41ea-b1b4-efe1671c5836
Bob has caused an error in /submit uuid: 49dlsd7i-dapd-fdio-fei0-sd59fd0ph34d
而不是这个(没有上下文):
Bob has called /submit
Bob has called /submit
Bob has saved to DB successfully in /submit
Bob has caused an error in /submit
因此,对于我的 Nodejs 应用程序中的所有路由,我将创建logChild
对象,然后使用logChild
记录该路由中的条目。
我是否正确理解并实施了用例log.child
?