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.
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.
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.
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.
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,
- v (String,String) (verbose)
- d (String,String) (debug)
- i (String,String) (information)
- w (String,String) (warning)
- 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.