How to Debug an Android application with LogCat?

SHARE & COMMENT :

This tutorial explains you how to debug a simple helloworld application with LogCat. In my previous post i have explained you about how to create a helloworld application. Let us now discuss how to debug that application with LogCat.

Unlike Java, Android doesn’t display its runtime exceptions in its console. It displays it using its LogCat. Android provides you a debugging tool called DDMS (Dalvik Debug Monitor Server) which contains a LogCat. LogCat is integrated into the DDMS which outputs the messages that you print out using a Log class.

1_logcat

What is LogCat?

LogCat is a command used for looking into the logs generated from various programs running on Android application environment. This logcat can be used from ADB shell to view the logs. The following is the syntax for using logcat command:

[adb] logcat [<option>] ... [<filter-spec>] ...,

The DDMS Perspective

The DDMS perspective in Eclipse lets you access all the features of DDMS. We use the LogCat feature of DDMS for Debugging our applictaion.  To access the DDMS perspective in eclipse, please go to Window->Open Perspective->DDMS. If DDMS doesnot appear go to Window->Open Perspective->Other->Select DDMS from the Open Perspective window. Once the DDMS perspective appears on the Ecilpse Click on it which opens the below screen.

2_ddms1

How to open LogCat?

you can find the LogCat window by switching to  DDMS perspective(refer the above screen) or you can go to window->Show View->Other->Android->click LogCat->Now you will be automatically switched to DDMS perspective where you can find LogCat.

openlogcat

The Android Logging provides a mechanism for collecting and viewing system debug output. Logcat displays Logs generated by system and also by your application. Every android log message has a tag and Priority associated with it.

  • Tag:  string indicating from which component the message originates.
  • Priority: is one of the character values ordered from lowest to highest priority. LogCat has different levels of logging.
  • V-Verbose(lowest priority)
  • D-Debug
  • I-Info
  • W-Warning
  • E-Error
  • F-Fatal
  • S-Silent

A sample LogCat which displays logs is as below.

logcat

As you can notice  the highlighted part in the above screen every message has a priority and a tag assosiated with it. Using LogCat you can filter your logs based on message TAG,PRIORITY or PID.

loglevelnew

You can create your own custom filter by clicking on the highlighted “+” symbol in the above picture which leads you to the below screen.

customfilter

Displaying your own messages in the Logcat

A simple Debugging technique is to display your own messages in Logcat window. To do this you have to add your messages using the methods of android Log class.The logging methods are,

  1. v (String,String) (verbose)
  2. d (String,String) (debug)
  3. i (String,String) (information)
  4. w (String,String) (warning)
  5. e (String,String) (error)

The below code shows how you can add the above methods into your java code.

package my.android.app;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
public class helloWorld extends Activity {
     /** Called when the activity is first created. */
private static final String TAG="hello world";
     @Override
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.main);
         Log.d(TAG,"I have just created my first android App");
      }
  }

For Tags use class name or your own Tags for better filtering. The below screen shows the LogCat displaying your own tag and message.

logcat_log

This is how you can debug your android applications using LogCat. I hope this article would have provided the enough details to debug the android applications. If you have any questions, please post it in the comments section.

Comments

comments

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Trackbacks

  1. [...] Follow this link: How to Debug an Android application with LogCat? – JavaBeat [...]

Speak Your Mind

*

Close
Please support the site
By clicking any of these buttons you help our site to get better