繁体   English   中英

如何在node.js中记录每个方法调用而无需在任何地方添加调试行?

[英]how can I log every method call in node.js without adding debug lines everywhere?

我想记录发出请求的人的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它,并且一次只能看到一个用户的活动。

有没有一种超级聪明的方法可以在不向每个方法添加记录器语句的情况下执行此操作?

答案基本上是正确的,但这里是如何避免无限递归

使用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

这是一种替代方案,但不完全确定它有多可靠,但感觉有点不对劲:

(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);
    Function.prototype.call = newCall;
    this.apply(self, args);
  }
  Function.prototype.call = newCall;
})();

正如您所看到的,它会覆盖call函数 - 当您尝试调用console.log()时会产生一个小问题,因此您需要将函数交换回来。 但它似乎工作!

编辑

由于这是标记的CoffeeScript:

do ->
  oldCall = Function::call
  newCall = (self) ->
    Function::call = oldCall
    console.log "Function called: #{this.name}"
    args = Array.prototype.slice.call arguments, 1
    Function::call = newCall
    this.apply self, args
  Function::call = newCall

我猜这是一个Web应用程序,在这种情况下,如果您使用连接,您可以使用记录用户和URL路径的记录器中间件,这可能就足够了。 否则,您将不得不在包装函数中包装每个函数的行进行一些元编程来进行日志记录。

function logCall(realFunc, instance) {
    return function() {
      log.debug('User: ' + instance.user_id + ' method ' + realFunc.name);
      return realFunc.apply(instance, arguments);
    };
}

为此,您的类方法必须是命名函数,而不是匿名函数。

function sendMessage() {
    //code to send message
    //can use `this` to access instance properties
}
function MyClass(userId) {
    this.userId = userId; //or whatever
    this.sendMessage = logCall(sendMessage, this);
    //repeat above line for each instance method you want instrumented for logging
}

暂无
暂无

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

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