简体   繁体   中英

How can i get all logcat entries stored to a file in android

When i use command prompt on PC and connect device and run abd to execute below command

abd logcat -d > abc.txt

It returns a big file "abc.txt" with all the entries of logcat of all apps. But when I run the same command in an app on android to store it on SD card by making a file it just stores the recent logcat entries and only logs for this app and not even previous logs. Can somebody tell me why is this happening I have also given permission to READ_LOGS

Here is the android code

 String fullName = "appLog.txt";
    File file = new File (Environment.getExternalStorageDirectory(), fullName);

    //clears a file
    if(file.exists()){
        file.delete();
    }


    //write log to file
    try {
        String command = String.format("logcat -d");        
        Process process = Runtime.getRuntime().exec(command);

        BufferedReader reader = new BufferedReader(new   InputStreamReader(process.getInputStream()));
        StringBuilder result = new StringBuilder();
        String currentLine = null;

        while ((currentLine = reader.readLine()) != null) {

                   result.append(currentLine);
                   result.append("\n");

        }

        FileWriter out = new FileWriter(file);
        out.write(result.toString());
        out.close();


    } catch (IOException e) {
        Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_SHORT).show();
    }


    //clear the log
    try {
        Runtime.getRuntime().exec("logcat -c");
    } catch (IOException e) {
        Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_SHORT).show();
    }

In Jelly Bean this has changed. App can read only it's own logs and don't need anymore READ_LOGS permission.

As alternative you can use log4j for android . You can use file appender to store all the logs in some file and also LogCatAppender to print logs to logcat, so you can watch logs while developement within eclipse for eg

For catching all uncaught exceptions you can create class that implements UncaughtExceptionHandler and then use it with Thread.setDefaultUncaughtExceptionHandler(). This is then useful for printing crash reports (stack traces) to your log and/or to separate file.

public void appendLog(String text)
    {       
       File logFile = new File("/sdcard/log.txt");
       if (!logFile.exists())
       {
          try
          {
             logFile.createNewFile();
          } 
          catch (IOException e)
          {
             // TODO Auto-generated catch block
             e.printStackTrace();
          }
       }
       try
       {
          BufferedWriter buf = new BufferedWriter(new FileWriter(logFile, true)); 
          buf.append(text);
          buf.newLine();
          buf.close();
       }
       catch (IOException e)
       {
          // TODO Auto-generated catch block
          e.printStackTrace();
       }
}

And use the method to log from anywhere you need.

appendLog("your message" + anyvalueyouwanttolog );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM