如何覆盖console.log()并在输出的开头附加一个单词? [英] how can I override console.log() and append a word at the beginning of the output?

查看:243
本文介绍了如何覆盖console.log()并在输出的开头附加一个单词?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有函数Log,它打印数据和传递的参数,如何打印内容和&同时始终在日志的开头打印报告:一词。

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 of the log.

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

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

我想:
报告:此日志发生错误......

I want to have: "Report: error ocurred on this log..."

谢谢。

推荐答案

你可以覆盖 console.log 轻松

(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')

演示:小提琴

这篇关于如何覆盖console.log()并在输出的开头附加一个单词?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆