how can I override console.log() and append a word at the beginning of the output?

前端 未结 1 1208
灰色年华
灰色年华 2020-11-29 06:46

I have the function Log which prints data along with passed arguments, how can I print the content & at the same time always print the word \"Report: \" at the beggining

相关标签:
1条回答
  • 2020-11-29 07:29

    You can override the console.log easily

    (function(){
        if(window.console && console.log){
            var old = console.log;
            console.log = function(){
                Array.prototype.unshift.call(arguments, 'Report: ');
                old.apply(this, arguments)
            }
        }  
    })();
    
    console.log('test')
    

    Demo: Fiddle

    0 讨论(0)
提交回复
热议问题