Search code examples
javascriptdebug-backtrace

Custom console log function, a console.log wrapper


function log( msgOrObj ){
    if(dev_mode){
        console.log({
            'message': msgOrObj,
            'caller': arguments.callee.caller.toString()
        });
    }
}

So, I have attempted to write a simple custom console log function (as above). However I am struggling to find which file and line the caller came from. The most I can see is the function that called it.

Has anyone done anything similar? Or is this even possible?

example used in somescript.js from line 70:

log('some very important message!')

Solution

  • So, this is what I went for in the end (where shout is a bespoke function only running in dev mode):

    function log( msgOrObj ){
        if(dev_mode){
            if( typeof(window.console) != 'undefined' ){
                try {  invalidfunctionthrowanerrorplease(); }
                catch(err) {  var logStack = err.stack;  }
                var fullTrace = logStack.split('\n');
                for( var i = 0 ; i < fullTrace.length ; ++i ){
                    fullTrace[i] = fullTrace[i].replace(/\s+/g, ' ');
                }
                var caller = fullTrace[1],
                    callerParts = caller.split('@'),
                    line = '';
    
                //CHROME & SAFARI
                if( callerParts.length == 1 ){
                    callerParts = fullTrace[2].split('('), caller = false;
                    //we have an object caller
                    if( callerParts.length > 1 ){
                        caller = callerParts[0].replace('at Object.','');
                        line = callerParts[1].split(':');
                        line = line[2];
                    }
                    //called from outside of an object
                    else {
                        callerParts[0] = callerParts[0].replace('at ','');
                        callerParts = callerParts[0].split(':');
                        caller = callerParts[0]+callerParts[1];
                        line = callerParts[2];
                    }
                }
                //FIREFOX
                else {
                    var callerParts2 = callerParts[1].split(':');
                    line = callerParts2.pop();
                    callerParts[1] = callerParts2.join(':');
                    caller = (callerParts[0] == '') ? callerParts[1] : callerParts[0];
                }
                console.log( ' ' );
                console.warn( 'Console log: '+ caller + ' ( line '+ line +' )' );
                console.log( msgOrObj );
                console.log({'Full trace:': fullTrace });
                console.log( ' ' );
            } else {
                shout('This browser does not support console.log!')
            }
        }
    }
    

    log() when declared before the rest of the application can be called anywhere from within the app and give the developer all the information required plus will not run out of dev mode.

    (http://webconfiguration.blogspot.co.uk/2013/12/javascript-console-log-wrapper-with.html)