如何覆盖 console.log() 并在输出的开头添加一个单词?

2024-01-15

我有一个 Log 函数,它可以打印数据以及传递的参数,如何打印内容并同时始终在日志开头打印单词“Report:”。

function Log(){
    if (app.debug_logs){
        if(console && console.log){
            console.log.apply(console, "Report: " + arguments);
        }
    }
}

Log(" error occurred ", " on this log... ");

我希望有: “报告:此日志发生错误...”

Thanks.


您可以覆盖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 http://jsfiddle.net/arunpjohny/7QsJ4/1/

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何覆盖 console.log() 并在输出的开头添加一个单词? 的相关文章

随机推荐