2011-12-21 165 views
4

我有一个拥有数千行Javascript的大型复杂web应用程序。有一小部分间歇性的Javascript错误是由用户报告的。客户端Javascript崩溃的服务器端日志记录

我觉得这些都是竞争条件附带现象 - 这是没有正确初始化和JavaScript崩溃造成“下游” JS不运行。

反正有没有JavaScript执行崩溃登录服务器端?

的所有日志记录库,例如BlackbirdLog4JavaScript的JS是唯一的客户端。

+2

如果错误导致抛出错误,你可以看看'window.onerror',并在处理程序中发送一个ajax请求(一起发送事件参数)。这当然不会工作,如果应用程序真的崩溃(如浏览器崩溃)。 – pimvdb 2011-12-21 10:14:52

+0

@pimvdb这应该是一个答案:) – 2011-12-21 10:30:51

+0

@JaniHartikainen我同意 – 2011-12-21 13:17:12

回答

3

我写使用window.onerror远程错误日志记录功能的@pimvdb

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; 

的建议我的身体网页的标签之间部署此。

你会想改变JSON和您将其发布到这取决于你如何去记录的数据服务器端的URL。

+0

+1如果你使用jQuery,你也可以传递一个对象,而不是与逃避等(为了清楚起见)。它会自动序列化它。 – pimvdb 2011-12-21 13:31:15

+0

@ pimvdb - 不断给予的礼物。我们的代码库在整个过程中都使用JSON.stringify()。我想保持错误代码尽可能苗条,所以我做了手动:( – 2011-12-21 13:49:22

2

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

您也可以使用凉亭安装它,使用凉亭安装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);