Search code examples
javascriptloggingconsolefirebugline-numbers

How to access line numbers when wrapping Firebug (or similar) Console api


I have wrapped the console API to provide granular logging levels as well as few other sugar features.

This works fine, the only problem is that firebug (or whatever other console) will always report the line number the log came from as the line the console API itself is invoked.

How would you suggest I make the console log the line number at which I call my wrapper function?

I would prefer a cross browser solution but failing that a firebug plugin could be a good start.

fyi I call my loging function like so:

db.log(db.LogLevel.WARN, "Blah Blah Blah");

Solution

  • So this recently came up again so I decided to revisit it.

    Now I'm older and wiser it's clear to me a much better solution then what I was trying to do is to call the console functions as they are but selectively replace them with dummy functions when the level is turned down. This gives me fine grained logging and accurate line number reporting. A few features have been lost from my previous solution but I think this is an acceptable compromise.

    Here's a partial snip of my new logging lib which shows the main solution

    ...
    levels : ["debug","info","warn","error"],
    
        init : function(minLevel) {
            var params = abm.getUrlParams();
            minLevel = params["debug"] || minLevel;
    
            //TODO: firebug lite
            window.console = window.console || {};
    
            var clear = false;
            for (var i=0; i<self.levels.length; i++) {
                var level = self.levels[i];
                originalFunctions[i] = originalFunctions[i] 
                || console[level] || fallback;
    
                if (level && (clear || level===minLevel)) {
                    console[level] = originalFunctions[i];
                    clear=true;
                } else {
                    console[level] = suppressed(level);
                }
            }
    
        }
    ...
    

    You can see the full thing here: https://github.com/antiBaconMachine/abm-log