开发者

javascript abstract console logging

I want to make a function, like this.

For example:

function Logger() {
    this.log = function(msg) {
        console.log(msg);
    }
}

And I want to use it in functions/modules etc, and that all works fine. But the default console in my browser normally give the fileName + lineNumber.

Now when I abstract this functionality, the fileName and lineNumber is not where I put my instance.log(). Because it will say from where the console.log is being called, not the function itself.

So my question:

How can I get the correct information from where I want to use my logger? Or give me, please, any tips to imp开发者_运维问答rove this functionality.


function Logger() {
    this.log = console.log.bind(console);
}

I asked about this some time ago: Create shortcut to console.log() in Chrome.


Try using backtrace function like this one :

function printStackTrace() {
    var callstack = [];
    var isCallstackPopulated = false;
    try {
        i.dont.exist += 0; //doesn't exist- that's the point
    } catch (e) {
        if (e.stack) { //Firefox
            var lines = e.stack.split('\n');
            for (var i = 0, len = lines.length; i & lt; len; i++) {
                if (lines[i].match(/^\s*[A-Za-z0-9\-_\$]+\(/)) {
                    callstack.push(lines[i]);
                }
            }
            //Remove call to printStackTrace()
            callstack.shift();
            isCallstackPopulated = true;
        }
        else if (window.opera & amp; & amp; e.message) { //Opera
            var lines = e.message.split('\n');
            for (var i = 0, len = lines.length; i & lt; len; i++) {
                if (lines[i].match(/^\s*[A-Za-z0-9\-_\$]+\(/)) {
                    var entry = lines[i];
                    //Append next line also since it has the file info
                    if (lines[i + 1]) {
                        entry += ' at ' + lines[i + 1];
                        i++;
                    }
                    callstack.push(entry);
                }
            }
            //Remove call to printStackTrace()
            callstack.shift();
            isCallstackPopulated = true;
        }
    }
    if (!isCallstackPopulated) { //IE and Safari
        var currentFunction = arguments.callee.caller;
        while (currentFunction) {
            var fn = currentFunction.toString();
            var fname = fn.substring(fn.indexOf( & amp; quot;

            function & amp; quot;) + 8, fn.indexOf('')) || 'anonymous';
            callstack.push(fname);
            currentFunction = currentFunction.caller;
        }
    }
    output(callstack);
}

function output(arr) {
    //Optput however you want
    alert(arr.join('\n\n'));
}


Try assigning the function:

(function () {
    window.log = (console && console.log 
        ? console.log 
        : function () { 
              // Alternative log
          });
})();

Later just call log('Message') in your code.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜