Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jsp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在android中仅将应用程序日志写入外部文件_Android - Fatal编程技术网

如何在android中仅将应用程序日志写入外部文件

如何在android中仅将应用程序日志写入外部文件,android,Android,我正在尝试将应用程序的日志写入外部文件。我的日志类似于Log.e(“Offset”,“”+mOffset);我正在使用以下代码: public String writeLogToFile() { try { Process process = Runtime.getRuntime().exec("logcat -d"); BufferedReader bufferedReader = new BufferedReader(

我正在尝试将应用程序的日志写入外部文件。我的日志类似于Log.e(“Offset”,“”+mOffset);我正在使用以下代码:

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();
}

你试过这个Log.VERBOSE吗?应该像这个Runtime.getRuntime().exec(“logcat Log.VERBOSE”);如果是,那么我的日志必须在哪个级别?Runtime.getRuntime().exec(“logcat-v time”);可能喜欢这个或Runtime.getRuntime().exec(“logcat-v”);尝试此链接代码可能会获得对您的帮助。。n使用大写字母“V”重试,即Runtime.getRuntime().exec(“logcat-V”);我应该把Log.v(“msg”,“msg”);或Log.e(“msg”、“msg”);很好