I want to log/write all Javascript errors to a text file whenever an error happens in my JavaScript functions. Using a try catch block, I am catching an exception. I need to log this exception to a text file. How can I achieve this using Sencha Touch 2?
Share
Assuming you are writing a mobile web application (based on your comment about Sencha Touch), you can’t do this directly with JavaScript – as Chris says, it doesn’t have access to the filesystem.Correction: not quite true. You may also be able to use the HTML5 File API, depending upon the platform you are on (it may be worth clarifying in your question which platforms you are developing for).Also, if you are prepared to re-work your application using a hybrid mobile framework, you can access the filesystem to a certain extent. For example, using Apache Cordova (previously known as PhoneGap), you can use its File API:
http://docs.phonegap.com/en/1.8.1/cordova_file_file.md.html#File
You’ll have to write some generic logging code to bridge between that API and the try/catches. You’ll still have to do a fair amount of copy ‘n’ paste, putting your generic logging code in each catch block. I’m not aware of a more cross-cutting way to do that.