2

I am trying to write a logger object which logs messages to screen. here is my code. http://github.com/huseyinyilmaz/javascript-logger in every function that needs to log something, I am writing loggerstart and loggerEnd functions at start and end of my functions. But I want to run thos codes automaticalls for every function. is there a way to modify Function prototype so every function call can run automatically. (I am not using any javascript framework.)

2
  • You could obtain the caller method name from the stack (if performance isn't too big a deal)...eriwen.com/javascript/js-stack-trace Commented Aug 4, 2010 at 11:37
  • I put a enableLog switch in my object so it will run only if we need it. Thats why performance won't be a problem. It would be very helpful only if I could add my logging code to Function.prototype (which would be the perfect solution for this). Since I have to go trough every method in my objects and modify them I will already know my function names. Commented Aug 4, 2010 at 12:02

2 Answers 2

4

EDIT: Rewritten the function to make it more modular

Well, this is a creepy way to do it, but I use this way sometimes when I need overriding some functions. It works well, allows any kind of customization and easy to understand (still creepy).

However, you will need to have all your functions stored in some kind of global object. See the example for details.

function dynamic_call_params(func, fp) {
    return func(fp[0],fp[1],fp[2],fp[3],fp[4],fp[5],fp[6],fp[7],fp[8],fp[9],fp[10],fp[11],fp[12],fp[13],fp[14],fp[15],fp[16],fp[17],fp[18],fp[19]);
}

function attachWrapperToFunc(object, funcName, wrapperFunction) {
    object["_original_function_"+funcName] = object[funcName];
    object[funcName] = function() {
        return wrapperFunction(object, object["_original_function_"+funcName], funcName, arguments);
    }
}

function attachWrapperToObject(object, wrapperFunction) {
    for (varname in object) {
        if (typeof(object[varname]) == "function") {
            attachWrapperToFunc(object, varname, wrapperFunction);
        }
    }
}

And some usage example:

var myProgram = new Object();
myProgram.function_one = function(a,b,c,d) {
    alert(a+b+c+d);
}
myProgram.function_two = function(a,b) {
    alert(a*b);
}

myProgram.function_three = function(a) {
    alert(a);
}

function loggerWrapperFunction(functionObject, origFunction, origFunctionName, origParams) {
    alert("start: "+origFunctionName);
    var result = dynamic_call_params(origFunction, origParams);
    alert("end: "+origFunctionName);
    return result;
}

attachWrapperToObject(myProgram,loggerWrapperFunction);
myProgram.function_one(1,2,3,4);
myProgram.function_two(2,3);
myProgram.function_three(5);

Output will be: start,10,end,start,6,end,start,5,end

So generally it allows you to wrap each function in some object automatically with a custom written wrapper function.

Sign up to request clarification or add additional context in comments.

3 Comments

methods to my logger that way. May be I can go trough window object and append my global methods too. But functions in closure of another functions will be left(which can be added to logger manually). Looks like this is the best aproach to go. Thank you for your answer.
Looping through window object will fail. Problem is that there are some window properties that are loopable but not readable. For example try this: alert(typeof(window["sessionStorage"]));. Still, maybe you would be able to do it with some nasty exception handling to skip those unreadable properties. By the way, you can also add additional parameters (for example called function name) to the wrapper function parameters. Not hard to do but will provide your logger some info on what function is called and with what parameters.
Changed the code to reflect that 'additional parameters' changes.
0

You could call every function with a wrapper function.

function wrapper(callback) {
    loggerstart();
    callback();
    loggerend();
}

And call it with wrapper(yourfunction);

7 Comments

So now he will need to augment each of his function in entire project with 10 millions of different functions with wrappers?
Yes, he does. Otherwise there is no solution I know of. If you have one, post it, but don't only criticize my approach. Anyway...you can easly search and replace those 10 millions of different functions (I on't think that he has that much functions).
I need this logger to have as minimal footprint in my code as possible, so It can be integrated to my project with minimum efford. Your suggestion would definitely work, but its footprint will be huge.
Voting here is to express my attitude regarding your solution. And I assume once @yilmazhusein asked here such question he has already eliminated your suggestion as not quite feasible. And I'll promise to post my solution here right after I've find one.
it's not the vote that annoys me. It's your unhelpful comment.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.