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

Server Side Logging Of Client Side Javascript Crashes

本文关键字:服务器端 日志 崩溃 Javascript 客户端      更新时间:2023-09-26

我有一个大型复杂的web应用程序,有数千行Javascript。有一小部分断断续续的Javascript bug是由用户报告的。

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

是否有办法让Javascript执行崩溃日志回服务器端?

所有的js日志库,如Blackbird和Log4JavaScript只在客户端。

我用窗口写了一个远程错误记录函数。@pimvdb

建议的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;

我将它部署在网页的头部正文标记之间。

您将需要更改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);