Adding timestamps to all console messages

前端 未结 12 1122
鱼传尺愫
鱼传尺愫 2021-01-31 06:51

I have a complete, deployed, Express-based project, with many console.log() and console.error() statements throughout. The project runs using forever, directing the st

12条回答
  •  攒了一身酷
    2021-01-31 07:16

    This implementation is simple, supports original functionality of console.log (passing a single object, and variable substitution), doesn't use external modules and prints everything in a single call to console.log:

    var origlog = console.log;
    
    console.log = function( obj, ...placeholders ){
        if ( typeof obj === 'string' )
            placeholders.unshift( Date.now() + " " + obj );
        else
        {
            // This handles console.log( object )
            placeholders.unshift( obj );
            placeholders.unshift( Date.now() + " %j" );
        }
    
        origlog.apply( this, placeholders );
    };
    

提交回复
热议问题