-
Javascript Error Logging
A couple of weeks ago I attended a presentation on Enterprise Javascript Error Handling by Nicholas Zakas. This was during the Front-End Engineering Summit at Yahoo!. During the presentation he showed a cool way of logging Javascript errors on server side. Using this technique you can suppress all the Javascript errors on browser side and log the errors in error log on the server. He just went over the idea with a small code snippet. I decided to try it out and then write this page.
There are two parts to the process, first suppressing Javascript errors and second, logging the errors on server side. The first part makes use of window.onerror which is an error handler for error events. The idea is assigning a custom function reference to this error handler. This function will call another function to log the error on server side and returns true. When the function assigned to window.onerror returns true it prevents firing a default event handler and suppresses the error. Here is the javascript source code for the first step:
return (isDebug === 1) ? false : true;
}
onerror event handler is assigned to a function with three parameters– msg which is an actual error message, url is page url, and line is the line number where error occurred. This function calls logJSErrors function and passes an object literal to it. Passed object literal has four properties. First three are msg, url, line and fourth one is navgator object. We are passing navigator object just to be able to log the browser information. Second line of the function returns true or false. isDebug is a global variable which decides if we want to return true or false. This is an optional feature. We can have this only if we want to provide a debug mode. When you are in debug mode, above function returns false that means it wont suppress the errors and errors can be seen in browser.
Now the second step is writing a javascript function which does the job of logging the errors on server side. Here is the code for that:
First line creates an instance of an Image object. The src property of this newly created image is set to a php script (log.php) which actually logs the error information on server side. Input parameter of this function is an object literal with error information. All properties of the passed-in error object are passed to php script via url parameters.
Now following is the source code for log.php:
log.php collects all the error information that was sent to it via url parameters, creates a string and writes it to the errors.txt file.
Finally, here is the example php script which makes use of all the above explained functions. This file has following things:
- function that overrides default behavior of window.onerror
- logJSError()
- Example function which intentionally generates error. (used for testing)
done! check the error log to view JS error details.
When you load this file in browser it will generate JavaScript error but that error wont be shown in the browser instead it will be logged in the error log (in this case errors.txt). If ?debug=1 is passed to this php script then it will log the errors but wont suppress them since debug = 1 triggers debug mode.
26 Oct 2008 05:49 pmNo Comments »Leave a Reply
Hi, I am Manish Ranade. I am ex-Yahoo! and currently work at Netflix as a Senior UI Engineer.