Save Data of LogCat in android

前端 未结 5 1529
盖世英雄少女心
盖世英雄少女心 2021-02-04 19:09

I want to save all the contents of log cat into specific file in Android. I used Eclipse IDE to develop the android application.

How i can achieve this ?

Thanks.

相关标签:
5条回答
  • 2021-02-04 19:36
    I want to save all the contents of log cat into specific file in Android.
    

    LogCat files are stored as circular memory buffers on the device.

    If you run "adb logcat > myfile" on your host system, you can retrieve the content into a file.

    See this : https://sites.google.com/site/androidhowto/how-to-1/save-logcat-to-a-text-file

    Other Ways to Extract LogCat:

    1. @Blundell's Way :

      Select the lines of LogCat that you want to save and then simply press Ctrl + C (for copying) and then use Ctrl+V (for paste) in any text file.

    2. @Niek's Way :

      In the LogCat tab, select the lines that you want to save. Then at the right top, click on the Small Triangle Pointing Down, called 'View Menu', and select 'Export Selection as text.." It will ask you where to save your LogCat file.

    0 讨论(0)
  • 2021-02-04 19:42

    In the logcat tab, select all the lines. Then at the right top, click on the small triangle pointing down, called 'View Menu', and select 'Export Selection as text.."

    0 讨论(0)
  • 2021-02-04 19:52

    My method allows to get log in testing situation when the device is not connected to ADB

    Just create the clone of LogWrapper and in constructor create the file in external memory. I selected DIRECTORY_PICTURES because it is present on all devices. Constructor public LogWrapperExt(){ sdf = new SimpleDateFormat("\n hh:mm:ss.SSS"); String path; isValid = true; boolean isPresent; try { topDirPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES); if(topDirPath != null ) path = topDirPath.getCanonicalPath();

        } catch (java.io.IOException e) {
            isValid = false;
        } catch (java.lang.NoSuchFieldError e) {
            isValid = false;
        }
        if(isValid){
            logsFolder =   new File(topDirPath + File.separator + "MyLogs");// put your name
            if (!logsFolder.exists()) {
                isPresent = logsFolder.mkdir();
            }
            try {
                String formattedDate = new Date().toString() ;  
                formattedDate = formattedDate.replaceAll(" ", "_");
                String fileName = "test_"+ formattedDate + ".txt";
                logFile = new File(logsFolder.getAbsolutePath(),fileName);
                outputStreamLogFile = new FileOutputStream(logFile);
            } catch (java.io.FileNotFoundException e) {
    
            }
        }
    }
    

    then in public void println make your print also.

        Log.println(priority, tag, useMsg);
    
        String myp =  dateStr + " " + level_name[priority] + "  " + tag + " " + useMsg;
        try{
            outputStreamLogFile.write(myp.getBytes());
        } catch (java.io.IOException e) {
    
        }
    

    Some overhead - true, but data yours in all situations

    Dont forget to replace LogWrapper name to yours

    0 讨论(0)
  • 2021-02-04 19:56

    Although @Kartik show's you the proper way.

    For quickness you can copy and paste. Ensure the logcat view is open Eclipse > DDMS > logcat

    then click the line you want to start at

    hold shift (for multiline select)

    click the line you want to finish at

    and simply ctrl+c (copy)

    ctrl+v (paste whereever you want)

    0 讨论(0)
  • 2021-02-04 19:59

    Here is a way to get the Logcat contents progrmatically.

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

    Check this link for more details.

    0 讨论(0)
提交回复
热议问题