自制上报错误与监控性能
in Web with 0 comment

自制上报错误与监控性能

in Web with 0 comment

下面是简单核心代码,通过图片外链进行跨域get请求,可能存在一些安全性问题的做法,可以通过限制外链提高难度,或者通过ajax 的post请求进行跨域请求方法。具体请求可以自行设定。

<!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.ua = navigator.userAgent;     
                    obj.pageurl = location.href;

                    var json = "{" , arr = [];
                    for (var i in obj) {
                        arr.push('"'+ i + '"' +":" + '"' + obj[i] + '"');
                    }
                    json += arr.join(',');
                    json += "}";
                    var src = "http://127.0.0.1:7001/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,那代码如下:

<!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 调研(转)

Responses