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

前端 未结 3 695
栀梦
栀梦 2021-02-08 03:00

I would like to log the user_id of the person making a request and the method name of every method called for a javascript class. For example:

35 - log_in
35 -          


        
3条回答
  •  孤街浪徒
    2021-02-08 03:28

    The answer is essentially correct, but here's how to avoid infinite recursion

    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
    

提交回复
热议问题