Wednesday, April 12th, 2006
Catching users JavaScript errors in your server logs
Markku Uttula admitted to coding while drunk and decided to write a little hack that captures JavaScript errors and sends them to his web server log using Ajax.
Would you want to do this? Maybe as part of a usability testing framework?
Implementation
He has one PHP file that does both the error writing, and also acts as the script src= to capture errors:
-
-
window.onerror = function (msg, url, lno) {
-
... ajax call back to the server log ...
-
Quirky.












Only thing I can think of is what happens if a piece of code that is polling the server every 5 seconds or whatever starts returning js errors? Your logs could get pretty big fast.
Brian; I don’t think it will be a problem. We log every asp, .net and javascript error (also including errors during development) and the log is very big.
Anyway, I have also made a similar script. It doesn’t include the server-side logging though. See http://sleepyhead81.blogspot.com/2005/08/javascript-error-logging-with-ajax.html.
“Why would you want to do this?” You track down and fix exceptions in the server side logs, right? Why should exceptions in the client be any different? If you’re not doing this, and you didn’t test exhaustively in every possible browser version and OS, how do you know your application is actually working?
window.onerror actually works again? Just as PPK, I found the event is not fired anymore.
http://www.quirksmode.org/js/events_compinfo.html
Martin, since when was it not fired? And also, what browser? Works fine for me…
This is pretty old technology, has been published on the web for a while and used it in a project succesfully last year to log detailed information in a database.
I did find out that using a javascript debugger like Venkman could interfere with the onerror handler.
The test on PPK has a syntactical error which will not trigger the onerror.
For example, trying to run ‘myfunction()’ where myfunction doesn’t exist will trigger onerror. But running ‘myfunction(’ where there’s a missing bracket will not trigger it.
[...] Via Ajaxian, descubro esta utilidad mediante ajax en la cual su uso está más que justificado e incluso permite llegar a lugares donde la mente aun no ha llegado,(… me he pasado ). Bueno, la idea es que es muy interesante… el pie para empezar es este. [...]
Firing up an XMLHttp object to send a string one way is a bit more overhead than is necessary. Corporate no-ActiveX security policies will make it not work on IE, too. It’s pretty light and effective to use an image instead:
function logit(msg){
var i = new Image();
i.src=”logit.php?msg=” + escape(msg);
i = null;
}
logit.php doesn’t have to actually return anything.
Triggering window.onerror
Ajaxian covers a post on logging client-side errors on the server-side using AJAX. A commenter made mention of inconsistent behaviour when using the onerror event as detailed on PPK. In determining the issue, I noticed what appeared to be the…
I’ve expanded on the trigger issue a little after further testing.
As said, I was a bit drunk (ok, I was all the way down the drunken path:) and just decided to try if I could do something usefull. This just seemed like a crazy enough idea to experiment with, and honestly I had no idea it had been done before (though I do understand that these days that’s hardly ever the case) - Espen beat me by half a year, and on his page, a comment by Patrick Corcoran was also a very interesting one (about how to achieve this *without* Ajax).
And yes, I too think using Ajax for something that’s really very trivial like this might be a bit like shooting mosquitoes with a bazooka, but hey… it sure provided me with nice pastime :)
YOU SHOULD HAVE SEARCHED THROUGH PAST ARTICLES!!!
DUPE!!!!!!!!!!!
Oh…sorry I thought I was posting on Digg.
For some reason, that never occured to me last night. Beats me how ever could that have happened - I should know better than trying to reinvent the wheel… and making it square in the process :)
Using Ajax instead of the error log of your webserver (by fetching an image for example) has it’s benefits. I logged everything from logged in user to the data entered in forms in a structured database. Using Ajax saved me the hassle of parsing the server logs, gave me a nice overview of errors and I didn’t have to wait for the server logs (my provider only makes them available every 24h)
Error-loggin isn’t trivial but crucial when you make professional software :)
I’ve set up something like this about a year ago for a web content editing/management system that i developed. If there are any javascript errors, the client e-mails me with the function that caused the error, and the username. the users are always surprised when they get a call within 10 seconds of something going awry. In some cases, the error gets fixed before they can even complain about it.
Fortunately, there aren’t many bugs and not that many users on the system, but it has helped me fix a few things.
I just rechecked. Indeed, it works as expected in Firefox again. I could have sworn it did not trigger.
It is ignored by Opera, but this is because it does not implement onerror.
Well, everything is fine then :-)
log4js at http://log4js.berlios.de supports also the feature to send via AjaxAppender the errors to the server. As you can use different Layout implementations, you can define how the data is sent. Default it is sent in log4j-xml format. so you can store the logs on server and read them with a log4j-enabled reader.
You can also send the logs using the JSONLayout as JSON formatted objects.
Pah - I did that in 1999 for a Microsoft extranet site :-) Nothing’s new in the world of the Internet…
What about Opera support in this logging solution?
We used this idea in our application to log errors and notify users - http://gsmsync.net
But I don’t found any way how to implement this for Opera clients.
[...] Pues bien, vÃa Ajaxian encontramos este código: [...]