How to get Javascript Function Calls/Trace at Runtime

前端 未结 8 1875
天命终不由人
天命终不由人 2020-12-23 13:56

As I interact with my AJAX based application at RUNTIME I\'d like the console to spit out all the functions it\'s calling. (so no stack trace, or breakpoints, or pr

8条回答
  •  生来不讨喜
    2020-12-23 14:46

    I've used @Briguy37's solution with an improvement. In my case, I did not want to trace functions from some libraries, so I added some code to exclude them. Here is how it is used:

    • First, include the definition of the functions you don't want to trace;
    • excludeLoggingToNamespace to list the functions defined up to now and exclude them;
    • Include the definition of the functions you want to trace;
    • Call addLoggingToNamespace to add the logging capability to the functions defined in the above step.

    Example:

    
    
    
    
    

    Here is the code I added to @Briguy37's solution:

    var excludedFunctions = {};
    
            functionLogger.excludeLoggingToNamespace = function(namespaceObject){
                for(var name in namespaceObject){
                    var potentialFunction = namespaceObject[name];
    
                    if(Object.prototype.toString.call(potentialFunction) === '[object Function]') {
                        excludedFunctions[name] = name;
                    }
                }
            }; 
    

    And I had to modify @Briguy37's addLoggingToNamespace method to take into accound the excludedFunctions hash:

    functionLogger.addLoggingToNamespace = function(namespaceObject){
        for(var name in namespaceObject){
            var potentialFunction = namespaceObject[name];
    
            if(Object.prototype.toString.call(potentialFunction) === '[object Function]' && 
               !excludedFunctions[name]) {
                namespaceObject[name] = functionLogger.getLoggableFunction(potentialFunction, name);
            }
        }
    };    
    

提交回复
热议问题