Save Data of LogCat in android

前端 未结 5 1530
盖世英雄少女心
盖世英雄少女心 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条回答
  •  -上瘾入骨i
    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

提交回复
热议问题