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?
JavaScript is a client-side programming language and thus does not have direct access to the file system.
You need to send a server-side request to create/modify/delete the text file.
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.