Read Logs Programmatically in Android

Its easy to read logs from logcat and debug the application. But what if you have requirement of collecting that logs and send it as report to server?
 
What you need to do this is for is a permission to read logs and some source of code.
 
Step 1: 
 
Write following line in your AndroidManifest.xml file to give permission to read logs.
  1. <uses-permission android:name="android.permission.READ_LOGS" /> 
 Step 2:
 
Copy paste following code snippet in your application. This class has readLog method which return you logs read by that method.
  1. import java.io.BufferedReader;  
  2. import java.io.IOException;  
  3. import java.io.InputStreamReader;  
  4.   
  5. public class LogsUtil {  
  6.   
  7.     public static StringBuilder readLogs() {  
  8.         StringBuilder logBuilder = new StringBuilder();  
  9.         try {  
  10.             Process process = Runtime.getRuntime().exec("logcat -d");  
  11.             BufferedReader bufferedReader = new BufferedReader(  
  12.                     new InputStreamReader(process.getInputStream()));  
  13.   
  14.             String line;  
  15.             while ((line = bufferedReader.readLine()) != null) {  
  16.                 logBuilder.append(line + "\n");  
  17.             }  
  18.         } catch (IOException e) {  
  19.         }  
  20.         return logBuilder;  
  21.     }  

Now everything is done. If you want to read logs in application, just use
  1. LogsUtil.readLogs();
You can also use more options and filters while reading logs. Please read Reading and Writing Logs.