Monday, July 2, 2012

Read & Store Log-cat Programmatically in Android

Android provide Logcat option with ADB to Debugg the Application.

So we can test app on Emulator and with the help of Logcat we can easily trace the Error details.

But if We are testing on Real Android Device and that is not connected with system then its hard know where we are getting exception, error etc.

so for that i wrote this Tutorial

We can read the Logcat details programmatically and also at run-time we can store in Text file and later we can see the error details.

so what we will do in this code:

  • Read the logcat
  • Showing the logcat on TextView
  • Create a Text file
  • store logcat into Text file
  • and finally save file into SDCard.

see below code snap..

//to read the logcat programmatically
try {
 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);
       }      
} 
catch (IOException e) {
}

//to create a Text file name "logcat.txt" in SDCard
File sdCard = Environment.getExternalStorageDirectory();
File dir = new File (sdCard.getAbsolutePath() + "/myLogcat");
dir.mkdirs();
File file = new File(dir, "logcat.txt");


don't forget to add permission for read log-cat in manifest file



add permission for writing text file to SDCard file



So lets create a small App to do this things

-------------------------------------------
App Name: LogCollect
Package Name: com.rdc
Android SDK: Android SDK 2.3.3 / API 10
Default Activity Name: LogTest
-------------------------------------------

LogTest.java
package com.rdc;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.widget.TextView;

public class LogTest extends Activity {
 
 private StringBuilder log;
 
 @Override
 public void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.main);
   try {
    Process process = Runtime.getRuntime().exec("logcat -d");
    BufferedReader bufferedReader = new BufferedReader(
    new InputStreamReader(process.getInputStream()));
 
    log=new StringBuilder();
    String line;
    while ((line = bufferedReader.readLine()) != null) {
      log.append(line);
    }
    TextView tv = (TextView)findViewById(R.id.textView1);
    tv.setText(log.toString());
  } catch (IOException e) {
  }       

 
 //convert log to string
 final String logString = new String(log.toString());
 
 //create text file in SDCard
 File sdCard = Environment.getExternalStorageDirectory();
 File dir = new File (sdCard.getAbsolutePath() + "/myLogcat");
 dir.mkdirs();
 File file = new File(dir, "logcat.txt");

 try {  
  //to write logcat in text file
  FileOutputStream fOut = new FileOutputStream(file);
  OutputStreamWriter osw = new OutputStreamWriter(fOut); 

        // Write the string to the file
        osw.write(logString);            
        osw.flush();
        osw.close();
 } catch (FileNotFoundException e) {
  e.printStackTrace();
 } catch (IOException e) {
  e.printStackTrace();
 }
  }
}

main.xml

 
 
 


and the manifest file: make sure you need to add permission

    
    
    
 

    
        
            
                
                
            
        

    


you can check the stored file open DDMS in eclipse and go to this path (in my cash)

File Explorer

data/mnt/sdcard/myLogcat/logcat.txt

now you can pull out this "logfile.txt" and store in local system. see below snap shot


also if you execute this app it will show you the log-cat on TextView with Scrolling like this

You can download the complete source code zip file here : LogCollect


cheers!!

I'd love to hear your thoughts!

3 comments:

  1. Many thanks for this tutorial Ramdhan. I adapted it to save logcat to a file and then upload it to server, where I can use it to diagnose problems etc. I have just one question, if I may: how to trigger this activity? I have another app running and ideally the logcat activity is triggered whenever this main app crashes. Is that possible at all in your opinion? Is there like a general error system-generated intent that I can listen for and then call the logcat activity? Thanks in advance for any ideas.

    ReplyDelete
  2. Hi RDC,
    Thanks for the tutorial.
    I have a question:
    Is it possible to get only the app specific logs not all the logs that the device is producing using some filters or so?

    Thx

    ReplyDelete
    Replies
    1. I am not sure dear, because i haven't tried so for, well this weekend i try and let you know if anything I can add.

      Delete