简体   繁体   English

如何在Sencha Touch 2中将所有JavaScript异常写入文本文件?

[英]How to write all JavaScript exceptions to a text file in Sencha Touch 2?

I want to log/write all Javascript errors to a text file whenever an error happens in my JavaScript functions. 每当我的JavaScript函数发生错误时,我都希望将所有Javascript错误记录/写入文本文件。 Using a try catch block, I am catching an exception. 使用try catch块,我正在捕获异常。 I need to log this exception to a text file. 我需要将此异常记录到文本文件中。 How can I achieve this using Sencha Touch 2? 如何使用Sencha Touch 2做到这一点?

JavaScript is a client-side programming language and thus does not have direct access to the file system. JavaScript是一种客户端编程语言,因此无法直接访问文件系统。

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. 假设您正在编写一个移动Web应用程序(基于您对Sencha Touch的评论),则无法直接使用JavaScript进行操作-正如Chris所说,它无权访问文件系统。 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). 您还可以使用HTML5 File API ,具体取决于您所使用的平台(在您的问题中可能需要澄清针对哪个平台进行开发)。

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: 例如,使用Apache Cordova(以前称为PhoneGap),可以使用其File API:

http://docs.phonegap.com/en/1.8.1/cordova_file_file.md.html#File http://docs.phonegap.com/zh-CN/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. 您必须编写一些通用的日志记录代码以在该API和try / catches之间架起桥梁。 You'll still have to do a fair amount of copy 'n' paste, putting your generic logging code in each catch block. 您仍然需要进行大量复制'n'粘贴,将常规日志记录代码放入每个catch块中。 I'm not aware of a more cross-cutting way to do that. 我不知道一种更跨领域的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM