繁体   English   中英

客户端Javascript的服务器端日志记录崩溃

[英]Server Side Logging Of Client Side Javascript Crashes

我有一个庞大的复杂Web应用程序,有数千行Javascript。 用户报告的是一小组间歇性Javascript错误。

我认为这些是竞争条件的附带现象 - 有些东西没有正确初始化,Javascript崩溃导致'下游'js不能运行。

反正有没有Javascript执行崩溃回登服务器端?

所有js日志库(如BlackbirdLog4JavaScript)都只是客户端。

我已经使用@pimvdb建议的window.onerror写了一个远程错误记录功能

Err = {};

Err.Remoterr = {};

Err.Remoterr.onerror = function (msg, errorfileurl, lineno) {

    var jsonstring, response, pageurl, cookies;

    // Get some user input
    response = prompt("There has been an error. " +
                      "It has been logged and will be investigated.", 
                      "Put in comments (and e-mail or phone number for" + 
                      " response.)");

    // get some context of where and how the error occured
    // to make debugging easier
    pageurl = window.location.href;
    cookies = document.cookie;

    // Make the json message we are going to post
    // Could use JSON.stringify() here if you are sure that
    // JSON will have run when the error occurs
    // http://www.JSON.org/js.html
    jsonstring = "{\"set\": {\"jserr\": " +
        "{\"msg\": \""         + msg + "\", " + 
        "\"errorfileurl\": \"" + errorfileurl + "\", " +
        "\"pageurl\": \""      + pageurl + "\", " +
        "\"cookies\": \""      + cookies + "\", " +
        "\"lineno\": \""       + lineno + "\", " +
        "\"response\": \""     + response + "\"}}}";

    // Use the jquery cross-browser post
    // http://api.jquery.com/jQuery.post/
    // this assumes that no errors happen before jquery has initialised
    $.post("?jserr", jsonstring, null, "json");

    // I don't want the page to 'pretend' to work 
    // so I am going to return 'false' here
    // Returning 'true' will clear the error in the browser
    return false;
};

window.onerror = Err.Remoterr.onerror;

我将其部署在网页的headbody标签之间。

您将需要更改JSON以及将其发布到的URL,具体取决于您将如何记录数据服务器端。

看看https://log4sure.com (披露:我创建了它) - 但它非常有用,请查看并自行决定。 它允许您记录错误/事件,还允许您创建自定义日志表。 它还允许您实时监控日志。 最好的部分,它是免费的。

您也可以使用bower来安装它,使用bower install log4sure

设置代码也非常简单:

 // setup var _logServer; (function() { var ls = document.createElement('script'); ls.type = 'text/javascript'; ls.async = true; ls.src = 'https://log4sure.com/ScriptsExt/log4sure.min.js'; var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ls, s); ls.onload = function() { // use your token here. _logServer = new LogServer("use-your-token-here"); }; })(); // example for logging text _logServer.logText("your log message goes here.") //example for logging error divide = function(numerator, divisor) { try { if (parseFloat(value) && parseFloat(divisor)) { throw new TypeError("Invalid input", "myfile.js", 12, { value: value, divisor: divisor }); } else { if (divisor == 0) { throw new RangeError("Divide by 0", "myfile.js", 15, { value: value, divisor: divisor }); } } } catch (e) { _logServer.logError(e.name, e.message, e.stack); } } // another use of logError in window.onerror // must be careful with window.onerror as you might be overwriting some one else's window.onerror functionality // also someone else can overwrite window.onerror. window.onerror = function(msg, url, line, column, err) { // may want to check if url belongs to your javascript file var data = { url: url, line: line, column: column, } _logServer.logError(err.name, err.message, err.stack, data); }; // example for custom logs var foo = "some variable value"; var bar = "another variable value"; var flag = "false"; var temp = "yet another variable value"; _logServer.log(foo, bar, flag, temp); 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM