0
배경
예, 많은 다른 Node.js 로깅 라이브러리 winston, bunyan 및 console.log가 있습니다. 요청이있을 때 특정 요청의 정보를 언제, 어떤 시점에서 그리고 어떤 정보가 응답 하는지를 쉽게 로그 다운 할 수 있습니다.요청 정보를 저장하는 고속 로깅
문제가
문제는 하위 함수 호출로 시작됩니다. 하나의 요청에서 동일한 로깅을 사용하는 여러 함수를 호출 할 때 요청 메타 데이터를 이러한 로그 호출에 전달하는 방법 (함수 매개 변수는 가능한 한 방법 인 것처럼 보이지만 실제로는 지저분합니다)?
예 코더에 대한
작은 시각 :
// Middleware to set some request based information
app.use(function (req, res, next) {
req.rid = 'Random generated request id for tracking sub queries';
});
app.get('/', function (req, rest) {
async.series({
'users': async.apply(db.users.find),
'posts': async.apply(db.posts.find),
}, function (err, dbRes) {
console.log('API call made ', req.rid)
res.end(dbRes);
});
});
// Now the database functions are in other file but we also need to track down the request id in there
(db.js)
module.exports = {
users: {
find: function() {
console.log('Calling users listing ', req.rid); // ERROR this is not possible to access, not in this scope
// Make query and return result
}
},
posts: {
find: function() {
console.log('Calling post listing ', req.rid); // ERROR this is not possible to access, not in this scope
// Make query and return result
}
}
};
실제로 몇 가지 검색 결과 몇 가지 가능한 해결책을 찾았습니다. 하나는 데이터를 저장할 수있는 도메인 사용을 포함하지만 이는 해킹 방법입니다. 현재 내가 찾은 최고의 솔루션은이 라이브러리 https://github.com/othiym23/node-continuation-local-storage를 사용하는 것입니다. –
그러나이를 수행하기 위해 추가 작업이 필요합니다. –
실제로이 방법으로는 문제가 해결되지 않습니다.이 문제를 해결할 수있는 유일한 방법은 현재 로깅을 사용하는 모든 함수에 개체 참조를 전달하는 것입니다 (WOW는 최악의 계획을 들립니다.) 이제까지). –