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

后端 未结 4 585
别跟我提以往
别跟我提以往 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条回答
  • 2021-01-20 05:55

    You can override the Log class and do that yourself. But is it really required for your purposes?

    0 讨论(0)
  • 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

    0 讨论(0)
  • 2021-01-20 06:00

    no, Log.* methods write to the console, however you can use Logger http://developer.android.com/reference/java/util/logging/Logger.html and a FileHandler

    0 讨论(0)
  • 2021-01-20 06:00

    If you want to add logs with tag name and

    logcat -s <Tagname>
    

    Is not working for you then you can do this trick

    //... Brtle 's code..(accepted one )
        file.createNewFile();
           FileWriter writer = new FileWriter(file);
             while((line = in.readLine()) != null){
                 if(line.contains("TAG_NAME")){ // Trick :-)
                    writer.write(line + "\n");
                   }
                 }
    
    0 讨论(0)
提交回复
热议问题