How to safely wrap `console.log`?

后端 未结 10 526
日久生厌
日久生厌 2021-01-31 14:13

Suppose I want to include some calls to console.log for some legitimate production reason, say for something like a unit test harness. Obviously I would not want th

10条回答
  •  长情又很酷
    2021-01-31 14:59

    The problem with wrappers is that they will obfuscate file name and line number of the source of the log message.

    Simple IE7 and below shim that preserves Line Numbering for other browsers:

    /* console shim*/
    (function () {
        var f = function () {};
        if (!window.console) {
            window.console = {
                log:f, info:f, warn:f, debug:f, error:f
            };
        }
    }());
    

提交回复
热议问题