我可以在js中创建自定义控制台日志功能吗?

时间:2013-09-13 13:18:46

标签: javascript jconsole console.log

有没有办法让一个函数运行console.log来获取一串变量。为变量名的名称运行一次,并显示其val

function multilog(text){
    var text=text.split(",")
    for(i=0;i<text.length;i++){
        console.log(text[i]+': ')
        console.log(JSON.stringify(text[i]));
    }
}
multilog('number_words,number_paragraphs,relatedwords');

所需输出的示例

number_words: 1 number_paragraphs: 2 相关词汇: [示例示例]

2 个答案:

答案 0 :(得分:1)

var logNumber = 0;

// The _log() will be a global function so you could access it from any place in the code.
// You can pass parameters like : _log( 'Something', 'Anything', '...', '...' ) 
_log = function () {
    console.log( '========= Log number : ' + logNumber + '. =========' )
    for( var i = 0, l = arguments.length; i < l; i ++ ) {
        console.log( arguments[ i ] );
    }
    console.log( '====== End of log number: ' + logNumber + '. ======' )
    logNumber ++;
}

答案 1 :(得分:0)

console.multilog = function(str){
    var toLog = str.split(',');
    for (var i = 0, len = toLog.length; i < len; i++){
    console.log(toLog[i] + ' =>');
    console.log(eval(toLog[i]));
    }
};