0
A
回答
0
一般的方式去了解這一點,但在實踐中,你應該使用緩衝流時可能。
在你onCreate(Bundle savedInstanceState)
回調或其他任何地方,你想這樣做記錄:
try
{
//normal application code
}
//called whenever an Exception is thrown
catch(Exception e)
{
//stream for writing text
FileWriter writer=null;
try
{
writer = new FileWriter(this.getExternalFilesDir());
//write to the SD card
} catch(Throwable t) {}
finally
{
if(writer != null) writer.close();
}
}
注意,我省略了檢查SD卡,但你應該總是這樣做。請參閱http://developer.android.com/guide/topics/data/data-storage.html#filesExternal
對不起,代碼格式糟糕。這個小文本框與Eclipse沒什麼兩樣。
11
我會創建我自己的記錄器類並使用它。其實這裏基本上是我做的,但你可以修改這個所以只寫了異常:
public class Logger {
private static Logger instance = null;
private static final int MAX_LOGS = 10;
public enum Level { DEBUG, INFO, WARN, ERROR }
private Level level = Level.ERROR;
private String tag;
private SimpleDateFormat format = new SimpleDateFormat("MM/dd/yy hh:mm:ss z");
private File log;
private PrintWriter writer;
private String supportEmail;
public void LoggerSetup(String tag, String logFilename, Level level) {
//close previous
if(writer != null) {
writer.flush();
writer.close();
writer = null;
}
//open new
this.tag = tag;
this.log = createWriter(logFilename);
this.level = level;
}
public Logger(Context context) {
LoggerSetup(context.getPackageName(), "application.log", Level.INFO);
}
public static Logger getInstance(Context context) {
if(instance == null) {
instance = new Logger(context);
}
return instance;
}
public Logger() {
LoggerSetup("", "application.log", Level.INFO);
}
private File createWriter(String logFilename) {
try {
String state = Environment.getExternalStorageState();
if(state.equals(Environment.MEDIA_MOUNTED)) {
File dir = new File(Environment.getExternalStorageDirectory(), "com.yourpackage");
if(!dir.mkdirs()) {
Log.w(tag, "Could not create log directory: " + dir.getAbsolutePath());
}
File log = new File(dir, logFilename);
if(log.exists()) {
rotate(log);
}
Log.i(tag, "Opening " + log.getAbsolutePath());
writer = new PrintWriter(new FileWriter(log), true);
return log;
} else {
Log.w(tag, "Could not create log file because external storage state was " + state);
}
} catch(IOException ioe) {
Log.e(tag, "Failed while opening the log file.", ioe);
}
return null;
}
private void rotate(File log) {
int index = log.getName().lastIndexOf('.');
if(index < 0) index = log.getName().length();
String prefix = log.getName().substring(0, index);
String extension = log.getName().substring(index);
int lastLog = MAX_LOGS - 1;
File lastLogFile = new File(log.getParentFile(), prefix + "-" + lastLog + extension);
if(lastLogFile.exists()) lastLogFile.delete();
for(int i = lastLog; i >= 1; --i) {
String filename = prefix + "-" + i + extension;
File l = new File(log.getParentFile(), filename);
if(l.exists()) {
File newLog = new File(log.getParentFile(), prefix + "-" + (i+1) + extension);
l.renameTo(newLog);
}
}
log.renameTo(new File(log.getParentFile(), prefix + "-1" + extension));
}
public Level getLevel() {
return level;
}
public void setLevel(Level level) {
this.level = level;
}
public boolean isLoggable(Level level) {
return level.ordinal() >= this.level.ordinal();
}
public void debug(String message, Object... parameters) {
if(parameters.length > 0) {
Log.d(tag, MessageFormat.format(message, parameters));
} else {
Log.d(tag, message);
}
log(Level.DEBUG, message, parameters);
}
public void info(String message, Object... parameters) {
if(parameters.length > 0) {
Log.i(tag, MessageFormat.format(message, parameters));
} else {
Log.i(tag, message);
}
log(Level.INFO, message, parameters);
}
public void warn(String message, Object... parameters) {
if(parameters.length > 0) {
Log.w(tag, MessageFormat.format(message, parameters));
} else {
Log.w(tag, message);
}
log(Level.WARN, message, parameters);
}
public void error(String message, Object... parameters) {
if(parameters.length > 0) {
Log.e(tag, MessageFormat.format(message, parameters));
} else {
Log.e(tag, message);
}
log(Level.ERROR, message, parameters);
}
public void error(Throwable throwable) {
String message = Log.getStackTraceString(throwable);
Log.e(tag, message, throwable);
log(Level.ERROR, message);
}
public void close() {
if(writer != null) {
writer.flush();
writer.close();
writer = null;
}
}
private void log(Level level, String message, Object... parameters) {
if(writer != null && isLoggable(level)) {
Date date = new Date();
writer.print(format.format(date));
writer.print(" ");
writer.print(level.toString());
writer.print(" ");
writer.print(tag);
writer.print(" ");
writer.print(Thread.currentThread().getName());
writer.print(" - ");
if(parameters.length > 0) {
writer.println(MessageFormat.format(message, parameters));
} else {
writer.println(message);
}
}
}
protected void sendEmail(Context context) throws IOException {
File[] logs = log.getParentFile().listFiles(new FileFilter() {
@Override
public boolean accept(File pathname) {
return pathname.getName().endsWith(".log");
}
});
File temp = zipLogFiles(logs);
String[] mailto = { supportEmail };
Intent sendIntent = new Intent(Intent.ACTION_SEND);
sendIntent.setType("application/zip");
sendIntent.putExtra(Intent.EXTRA_EMAIL, mailto);
sendIntent.putExtra(Intent.EXTRA_SUBJECT, tag + ": Log File Attached");
sendIntent.putExtra(Intent.EXTRA_TEXT, "A user has requested you look at some logs.");
sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(temp));
sendIntent.setType("text/plain");
context.startActivity(Intent.createChooser(sendIntent, "Send Logs To Support"));
}
private File zipLogFiles(File[] logs) throws IOException {
File zipfile = File.createTempFile("brewster", ".zip");
ZipOutputStream stream = new ZipOutputStream(new FileOutputStream(zipfile));
try {
for(File log : logs) {
ZipEntry entry = new ZipEntry(log.getName());
stream.putNextEntry(entry);
copy(stream, log);
stream.closeEntry();
}
stream.finish();
return zipfile;
} finally {
stream.close();
}
}
private void copy(OutputStream stream, File file) throws IOException {
InputStream istream = new FileInputStream(file);
try {
byte[] buffer = new byte[8096];
int length = 0;
while((length = istream.read(buffer)) >= 0) {
stream.write(buffer, 0, length);
}
} finally {
istream.close();
}
}
}
+0
@謝謝你的回答 – John
相關問題
- 1. 使用Java EE編寫日誌文件
- 2. 使用Powerpoint編寫日誌文件
- 3. 如何編寫c#日誌文件?
- 4. 如何用log4php在日誌文件中輸出異常信息?
- 5. 如何將異常寫入日誌文件...?
- 6. 在c/C++中編寫日誌文件
- 7. 如何在文件中寫入日誌?
- 8. 如何在java中將錯誤日誌或異常寫入文件
- 9. 如何在C#.NET中將錯誤日誌或異常寫入文件
- 10. 如何使用CSOM將異常寫入ULS日誌?
- 11. 如何在日誌文件log4j中只寫入INFO日誌?
- 12. 無法在Linux中使用PHP編寫日誌文件
- 13. 如何在Objective C中編寫日誌文件?
- 14. 如何在使用hadoop時使用記錄器api編寫java日誌文件
- 15. 使用播放文件中的日誌異常
- 16. 異常日誌在Android Market廣場
- 17. 異步寫入錯誤日誌文件
- 18. 如何從java日誌文件中提取異常?
- 19. 如何編寫事件日誌類別
- 20. 如何使用Python的日誌模塊將日誌文件中的上一行寫入日誌文件?
- 21. 日誌文件循環IOException異常
- 22. 異常的單獨日誌文件
- 23. 如何使用vbscript寫事件日誌
- 24. EnterpriseLibrary異常處理不寫入我的日誌文件
- 25. 如何在FxCop中編寫AvoidCallingProblematicMethods異常?
- 26. 使用python 2.7.9編寫日誌文件中的消息格式
- 27. 如何在ASP.NET中編寫內存使用情況日誌
- 28. WriteToEventLog日誌記錄異常的sql aspnet_db日誌文件
- 29. 將日誌寫入「鎖定編輯」文件,該文件引發異常。如何解決這個問題?
- 30. 如何編寫applet應用程序的日誌文件?
@謝謝你的回答 – John