public String writeLogTofile(){ try { Process process = Runtime.getRuntime().exec("logcat -d"); BufferedReader bufferedReader = new BufferedReader( new inputStreamReader(process.getinputStream())); StringBuilder log=new StringBuilder(); String line; while ((line = bufferedReader.readline()) != null) { log.append(line); } bufferedReader.close(); return log.toString(); } catch (IOException e) { // Todo auto-generated catch block e.printstacktrace(); return null; }}
它还打印系统级别的所有日志.是的我放-d所以它是打印但如果我把-e或-i它不写文件.我只想写Log.e(“Offset”,“”mOffset).我哪里做错了?
解决方法 以下是如何读取当前应用程序的日志.要过滤日志,我获取进程ID(pID)并将其与logcat输出的每一行进行比较.在logcat的参数中,我指定读取最后200行并随时间显示日志.
public String writeLogTofile() { StringBuilder logs = new StringBuilder(); int pID = androID.os.Process.myPID(); String pIDPattern = String.format("%d):",pID); try { Process process = new ProcessBuilder() .command("logcat","-t","200","-v","time") .redirectErrorStream(true) .start(); inputStream in = null; try { in = process.getinputStream(); BufferedReader reader = new BufferedReader(new inputStreamReader(in)); String line; while ((line = reader.readline()) != null) { if (line.contains(pIDPattern)) { logs.append(line).append("\n"); } } } finally { if (in != null) { try { in.close(); } catch (IOException e) { Log.e(TAG,"Cannot close input stream",e); } } } } catch (IOException e) { Log.e(TAG,"Cannot read logs",e); } return log.toString();}总结
以上是内存溢出为你收集整理的如何只将应用程序日志写入android中的外部文件全部内容,希望文章能够帮你解决如何只将应用程序日志写入android中的外部文件所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)