Beside the given answers, you can also write a utility that will make wrap the methods of an object with logging:
It would be like this:
function addLogging(object) {
Object.keys(object).forEach(function (key) {
if (typeof object[key] === 'function') {
var originalFn = object[key];
object[key] = function () {
console.log('before calling', key);
var result = originalFn.apply(this, arguments);
console.log('after calling', key);
return result;
}
}
});
}
You can use lodash wrap method to help you.
Long ago I have answered a question regarding something similar like the above code: jQuery logger plugin
It didn't handle constructors perfectly, so it had problems like that. Here's the fixed resulting code.
You can also have a look at sinon.js, it provides spies and it can determine order of call of spied functions. But it might be a bit too much for just this, and it's might slow things down a little.
This method is a common thing done in aspect oriented programming. You can search about it and maybe try to use a AOP library. meld
is a popular one.
Also instead of console.log in chrome you can use console.timeline, which gives you great visibility if used correctly.