js获取控制台信息

项目开发完毕,在手机上测试的时候经常会遇见和在PC上测试不一样的情况,然而又无法看见控制台输出的日志,十分不方便解决问题。查了查找了个比较简单方便的方案。

  if (console) {
      var _console = {
          log    : console.log,
          info : console.info,
          debug : console.debug,
          warn : console.warn,
          error : console.error,
    };
     console.log = function(data) {
        // do sth
        _console.log.apply(this, Array.prototype.slice.call(arguments, 0));
    };
    console.info = function(data) {
       // do sth
        _console.info.apply(this, Array.prototype.slice.call(arguments, 0));
    };console.debug = function(data) {
         // do sth
        _console.debug.apply(this, Array.prototype.slice.call(arguments, 0));
    };console.warn = function(a) {
         // do sth
        _console.warn.apply(this, Array.prototype.slice.call(arguments, 0));
    };console.error = function(a) {
        // do sth
        _console.error.apply(this, Array.prototype.slice.call(arguments, 0));
    };
}

重写console方法, 在其中注入想要的操作,是打印在页面上还是发送给服务端就随心所欲了。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容