如何将我的日志输出从 logcat 重定向到 Android 设备上的 SD 卡?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3359692/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 09:53:55  来源:igfitidea点击:

How to redirect my log output from logcat to the SD-Card on an android device?

androidlogging

提问by Santhosh Shettigar

I'm trying to redirect the log of my app to the sdcard file. But i failed to do so. I'm trying something like this.

我正在尝试将我的应用程序日志重定向到 sdcard 文件。但我没有这样做。我正在尝试这样的事情。

String cmd= "logcat -v time   ActivityManager:W  myapp:D  *:* >\""+file.getAbsolutePath()+"\"";
Runtime.getRuntime().exec(cmd);

I tried the -f option also but it is not working either.

我也尝试了 -f 选项,但它也不起作用。

回答by broderix

This is my worked version:

这是我的工作版本:

try {
    File filename = new File(Environment.getExternalStorageDirectory()+"/logfile.log"); 
    filename.createNewFile(); 
    String cmd = "logcat -d -f "+filename.getAbsolutePath();
    Runtime.getRuntime().exec(cmd);
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

find your logfile /sdcard/logfile.log

找到您的日志文件 /sdcard/logfile.log

回答by adamk

use logcat -f <filename>in order to dump it to a file in the filesystem.
Make sure the filename you're using is in the sdcard, i.e. starts with /sdcard/....

用于logcat -f <filename>将其转储到文件系统中的文件中。
确保您使用的文件名在 SD 卡中,即以/sdcard/....

Also, in order to pass arguments to the logcatprogram, you should pass the execmethod an array of strings (and not one string):

此外,为了向logcat程序传递参数,您应该向exec方法传递一个字符串数组(而不是一个字符串):

String[] cmd = new String[] { "logcat", "-f", "/sdcard/myfilename", "-v", "time", "ActivityManager:W", "myapp:D" };

Finally, if all else fails, use the full path for logcat: /system/bin/logcatinstead of just logcat.

最后,如果所有其他方法都失败了,请使用 logcat: 的完整路径,/system/bin/logcat而不仅仅是logcat.

回答by bear

   /**
     * Launches a logcat process.
     * To stop the logcat preserve the use:
     * process.destroy();
     * in the onBackPressed()
     *
     * @param filename destination of logcat output
     * @return Process logcaat is running on
     */

    public Process launchLogcat(String filename) {
        Process process = null;
        String cmd = "logcat -f " + filename + "\n";
        try {
            process = Runtime.getRuntime().exec(cmd);
        } catch (IOException e) {
            process = null;
        }
        return process;
    }

回答by Green goblin

If you are getting an empty log file, try change the extension of file from .logto .txt.

如果您收到一个空的日志文件,尝试文件的扩展名从改变.log.txt

回答by Krishna

public static void saveLogInSDCard(Context context){
    String filename = Environment.getExternalStorageDirectory() + File.separator + "project_app.log";
    String command = "logcat -d *:V";

    try{
        Process process = Runtime.getRuntime().exec(command);

        BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
        String line = null;
        try{
            File file = new File(filename);
            file.createNewFile();
            FileWriter writer = new FileWriter(file);
            while((line = in.readLine()) != null){
                writer.write(line + "\n");
            }
            writer.flush();
            writer.close();
        }
        catch(IOException e){
            e.printStackTrace();
        }
    }
    catch(IOException e){
        e.printStackTrace();
    }
}

回答by sai

Try using a space after every element in the string. Otherwise, it will consider as a single line command without spaces and do nothing.

尝试在字符串中的每个元素后使用空格。否则,它将被视为没有空格的单行命令并且什么也不做。