我想记录发出请求的人的 user_id 以及为 javascript 类调用的每个方法的方法名称。例如:
35 - log_in
35 - list_of_other_users
78 - log_in
35 - send_message_to_user
35 - connect_to_redis
78 - list_of_other_users
由于一切都是异步的,用户 35 和 78 可能同时在做一些事情。所以我想确保每个日志行都以他们的 user_id 开头,这样我就可以 grep 并且一次只能看到一个用户的事件。
有没有一种 super 聪明的方法可以在不向每个方法添加记录器语句的情况下做到这一点?
最佳答案
答案基本正确,但这里是如何避免无限递归
Javascript
(function () {
var oldCall = Function.prototype.call;
var newCall = function(self) {
Function.prototype.call = oldCall;
console.log('Function called:', this.name);
var args = Array.prototype.slice.call(arguments, 1);
var res = this.apply(self, args);
Function.prototype.call = newCall;
return res
}
Function.prototype.call = newCall;
})();
CoffeeScript
do ->
oldCall = Function::call
newCall = (self) ->
Function::call = oldCall
console.log "Function called: #{this.name}"
args = Array.prototype.slice.call arguments, 1
res = this.apply self, args
Function::call = newCall
res
Function::call = newCall
关于javascript - 如何在 node.js 中记录每个方法调用而不在任何地方添加调试行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15889766/