Chrome JavaScript developer console: Is it possible to call console.log() without a newline?

前端 未结 12 853
生来不讨喜
生来不讨喜 2020-11-29 05:07

I\'d like to use console.log() to log messages without appending a new line after each call to console.log(). Is this possible?

12条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-29 05:45

    Useful for debugging or learning what long chained maps are actually doing.

    let myConsole = (function(){
        let the_log_buffer=[[]], the_count=0, the_single_line=false;
        const THE_CONSOLE=console, LINE_DIVIDER='  ~  ', ONE_LINE='ONE_LINE',     
              PARAMETER_SEPARATOR= ', ', NEW_LINE = Symbol();
              
        const start = (line_type='NOT_ONE_LINE') => {
            the_log_buffer=[[]];
            the_count=0;
            the_single_line = line_type == ONE_LINE;   
            console = myConsole;  
        }
        const stop = () =>  {
            isNewline();
            console = THE_CONSOLE; 
        };                          
        const isNewline = a_param => {
            if (the_single_line && a_param==NEW_LINE) return;
            const buffer_parts = the_log_buffer.map(one_set=> one_set.join(PARAMETER_SEPARATOR))
            const buffer_line = buffer_parts.join(LINE_DIVIDER);    
            if (the_single_line) {                           
              THE_CONSOLE.clear();
            }
            THE_CONSOLE.log( buffer_line ); 
            the_log_buffer = [[]];
            the_count=0;
        }
        const anObject = an_object => {            
            if (an_object instanceof Error){
                const error_props = [...Object.getOwnPropertyNames(an_object)];
                error_props.map( error_key => an_object['_' + error_key] = an_object[error_key] );
            }
            the_log_buffer[the_count].push(JSON.stringify(an_object));
        }
        const aScalar = a_scalar => {
            if (typeof a_scalar === 'string' && !isNaN(a_scalar)) {
                the_log_buffer[the_count].push("'" + a_scalar + "'");
            } else {
                the_log_buffer[the_count].push(a_scalar);
            }
        }
        const notNewline = a_param => typeof a_param === 'object' ? anObject(a_param):aScalar(a_param);
        const checkNewline = a_param => a_param == NEW_LINE ? isNewline(a_param) : notNewline(a_param);
        const log = (...parameters_list) => {   
            the_log_buffer[the_count]=[];
            parameters_list.map( checkNewline );
            if (the_single_line){
                isNewline(undefined);
            }else{
                const last_log = parameters_list.pop();
                if (last_log !== NEW_LINE){
                    the_count++;
                }
            }
        }
        return Object.assign({}, console, {start, stop, log, ONE_LINE, NEW_LINE});
    })();
    
    function showConcatLog(){
        myConsole.stop();
        myConsole.start();
        console.log('a');
        console.log('bb');  
        console.dir({i:'not', j:'affected', k:'but not in step'})
        console.log('ccc');
        console.log([1,2,3,4,5,'6'], {x:8, y:'9'});
        console.log("ffffdd", 1, '2', 3, myConsole.NEW_LINE);
        console.log("z", myConsole.NEW_LINE, 8, '7');
        console.log(new Error("error test"));
        myConsole.stop();
    }
    
    myConsole.start(myConsole.ONE_LINE);
    var stop_callback = 5;
    function myCallback(){
        console.log(stop_callback, 'Date.now()', myConsole.NEW_LINE, Date.now());
        stop_callback--;
        if (stop_callback>0){
            window.setTimeout(myCallback, 1000);
        }else{
            showConcatLog();
        }
    }       
    window.setTimeout(myCallback, 1000);
    

提交回复
热议问题