Adding timestamps to all console messages

前端 未结 12 1091
鱼传尺愫
鱼传尺愫 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:39

    Create a file with the following:

    var log = console.log;
    
    console.log = function(){
      log.apply(console, [Date.now()].concat(arguments));
    };
    

    Require it in your app before you log anything. Do the same for console.error if needed.

    Note that this solution will destroy variable insertion (console.log("he%s", "y") // "hey") if you're using that. If you need that, just log the timestamp first:

    log.call(console, Date.now());
    log.apply(console, arguments);
    

提交回复
热议问题