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

时间:2011-12-21 10:09:33

标签: javascript logging remote-debugging

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

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

无论如何都要让Javascript执行崩溃以备份服务器端吗?

BlackbirdLog4JavaScript等所有js日志记录库都只是客户端。

2 个答案:

答案 0 :(得分:3)

我使用@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;

我在网页的 head body 标记之间进行部署。

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

答案 1 :(得分:2)

看一下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);