Can I extend the console object (for rerouting the logging) in javascript?

前端 未结 6 1829
误落风尘
误落风尘 2020-11-30 08:33

Is it possible to extend the console object?

I tried something like:

Console.prototype.log = function(msg){
    Console.prototype.log.call(msg);
             


        
6条回答
  •  情深已故
    2020-11-30 09:12

    You Can Also add log Time in This Way :

    added Momentjs or use New Date() instead of moment.

    var oldConsole = console.log;
    console.log = function(){
        var timestamp = "[" + moment().format("YYYY-MM-DD HH:mm:ss:SSS") + "] ";
        Array.prototype.unshift.call(arguments, timestamp);
        oldConsole.apply(this, arguments);
    };
    

提交回复
热议问题