前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >自制上报错误与监控性能

自制上报错误与监控性能

作者头像
张炳
发布2019-08-02 15:54:58
5270
发布2019-08-02 15:54:58
举报
文章被收录于专栏:web全栈工程师的取经之路

下面核心代码:

代码语言:javascript
复制
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <script type="text/javascript">
        (function () {
            try {
                var report = function (id, obj,eventtype) {
                    obj.pageurl = kk;
                    obj.ua = navigator.userAgent; 
                    var json = "{ ";
                    for (var i in obj) {
                        json += "" + i + ":" + obj[i] + ", "
                    }
                    json += "}";
                    var src = "http://www.xxx.com/logservice?eventtype=" + eventtype + "&eventid=" + id +
                    "&extend=";
                    var img = new Image();
                    img.src = src + encodeURIComponent(json);
                    img.onload = img.onerror = function () {
                        img = null
                    }
                };
                if (window.addEventListener) {
                    var ingore = ["localhost"];
                    window.addEventListener("error", function (e) {
                        //抓取文件404报错
                        if (e.target && e.target.src) {
                            var obj = {};
                            obj.file = e.target.src;
                            for (var i = 0, len = ingore.length; i < len; i++) {
                                if (obj.file.indexOf(ingore[i]) > -1) {
                                    return
                                }
                            }
                            report(404, obj,'jserror');
                        }
                    }, true);
                }
                window.onerror = function (msg, file, line) {
                    //抓取js常规报错
                    var obj = {};
                    obj.errmsg = msg;
                    obj.errfile = file;
                    obj.errline = line;
                    report(500, obj,'jserror');
                }

                window.onload = function()
                {
                    setTimeout(function()
                    {
                        if (window.performance && window.performance.timing) {
                            //抓取页面性能时间
                            var perf = window.performance.timing ,obj = {};
                            var arr = ['navigationStart','domainLookupEnd','connectEnd','domContentLoadedEventEnd','loadEventEnd'];
                            arr.forEach(function(curv,i){
                                obj[curv] = perf[curv];
                            });
                            report(200, obj,'performance');
                        }    
                    },1000);
                }


            } catch (e) {}
        })();
    </script>
</head>
<body>
    <img src="http://www.test.com/aa.jpeg">
    <img src="http://www.test.com/bb.jpeg">
    <script type="text/javascript">
        showme1();
    </script>
</body>
</html>

注意 注释 的“抓取文件404报错”,“抓取js常规报错”,“抓取页面性能时间”,代码很好理解,将整个操作放在闭包内执行,以免污染外面。 这段代码必须放在head标签头部内,若头部有js外联,那该段代码必须放在js外联之上,若将这段代码放置在http://www.xxx.com/logservice.js,那代码如下:

代码语言:javascript
复制
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <script type="text/javascript" src="http://www.xxx.com/logservice.js"></script>
    <script type="text/javascript" src="其他js"></script>
</head>
<body>
    //常规业务代码
</body>
</html>

在获取页面性能时间方面,先获取,后台再去计算。关于window.performance 方面,文章很多,具体里面每个单词代表的时间意义,可以自行搜查文档。如:前端性能监控方案window.performance 调研(转)

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
应用性能监控
应用性能监控(Application Performance Management,APM)是一款应用性能管理平台,基于实时多语言应用探针全量采集技术,为您提供分布式性能分析和故障自检能力。APM 协助您在复杂的业务系统里快速定位性能问题,降低 MTTR(平均故障恢复时间),实时了解并追踪应用性能,提升用户体验。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档