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

查看:33
本文介绍了如何覆盖 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 beginning of the log.

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

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

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

I want to have: "Report: error occurred 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天全站免登陆