2011-12-21 218 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);