最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

logging - Is there a way to wrap all JavaScript methods with a function? - Stack Overflow

programmeradmin7浏览0评论

I want to wrap every function call with some logging code. Something that would produce output like:

func1(param1, param2)
func2(param1)
func3()
func4(param1, param2)

Ideally, I would like an API of the form:

function globalBefore(func);
function globalAfter(func);

I've googled quite a bit for this, but it seems like there's only aspect-oriented solutions that require you to wrap the specific functions you want to log, or whatever. I want something that applies to every function in the global scope (except itself, obviously).

I want to wrap every function call with some logging code. Something that would produce output like:

func1(param1, param2)
func2(param1)
func3()
func4(param1, param2)

Ideally, I would like an API of the form:

function globalBefore(func);
function globalAfter(func);

I've googled quite a bit for this, but it seems like there's only aspect-oriented solutions that require you to wrap the specific functions you want to log, or whatever. I want something that applies to every function in the global scope (except itself, obviously).

Share Improve this question asked Oct 12, 2010 at 21:22 blake8086blake8086 6998 silver badges18 bronze badges 2
  • Do you want to wrap calls to built in function (like window.alert), or just user-defined functions? – Dagg Nabbit Commented Oct 12, 2010 at 21:32
  • Ideally, everything. I could write stuff to search, sort, and filter later. – blake8086 Commented Oct 13, 2010 at 1:47
Add a ment  | 

3 Answers 3

Reset to default 14

A simple approach would be something like this

var functionPool = {} // create a variable to hold the original versions of the functions

for( var func in window ) // scan all items in window scope
{
  if (typeof(window[func]) === 'function') // if item is a function
  {
    functionPool[func] = window[func]; // store the original to our global pool
    (function(){ // create an closure to maintain function name
         var functionName = func;
         window[functionName] = function(){ // overwrite the function with our own version
         var args = [].splice.call(arguments,0); // convert arguments to array
         // do the logging before callling the method
         console.log('logging: ' + functionName + '('+args.join(',')+')');
         // call the original method but in the window scope, and return the results
         return functionPool[functionName].apply(window, args );
         // additional logging could take place here if we stored the return value ..
        }
      })();
  }
}

To undo you would need to run the

for (func in functionPool)
  window[func] = functionPool[func];

Notes
This handles only global functions, but you can easily extend it to handle specific objects or methods etc..

jquery-aop might do the trick?

Maybe you could have a function to which you pass the function to execute as a parameter:

function runner(func_to_run) {
    alert('about to run ' + func_to_run.name);

    func_to_run();

}

function test() {
    alert ('in test');
}

runner(test)
发布评论

评论列表(0)

  1. 暂无评论