Collect all Log.d from class and save it into SDCard

后端 未结 4 586
别跟我提以往
别跟我提以往 2021-01-20 05:46

Hey is it possible in Android to collect all Log.d from one class and keep on appending it and save it into SDCard ?

For example :

Class Android {

          


        
4条回答
  •  -上瘾入骨i
    2021-01-20 06:00

    You can get all logs with the logcat command. You can try something like:

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

    You can get more information about logcat here: http://developer.android.com/tools/help/logcat.html

提交回复
热议问题