gpt4 book ai didi

android - 从类中收集所有 Log.d 并将其保存到 SDCard

转载 作者:太空狗 更新时间:2023-10-29 14:20:22 24 4
gpt4 key购买 nike

嘿,在 Android 中是否可以从一个类中收集所有 Log.d 并继续追加并将其保存到 SDCard 中?

例如:

Class Android {

private final static String TAG = "hello";

public void abcd(){
Log.d(TAG, "it went into abcd method");
}

public void efgh(){
Log.d(TAG, "it went into efgh method");
}

/*Here collect all above LOGS and write to file in SDCard*/

}

最佳答案

您可以使用logcat 命令获取所有日志。你可以尝试这样的事情:

public static void printLog(Context context){
String filename = context.getExternalFilesDir(null).getPath() + File.separator + "my_app.log";
String command = "logcat -d *:V";

Log.d(TAG, "command: " + command);

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

您可以在此处获得有关 logcat 的更多信息:http://developer.android.com/tools/help/logcat.html

关于android - 从类中收集所有 Log.d 并将其保存到 SDCard,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17609520/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com