Android Logo - Android

Debugging and Logging

java -> android -> debug and log ...

Before we develop more substantial apps we need to figure out how to conveniently debug an application and to conveniently log and read status information.

First, let's "enhance" our first application created earlier with a method that will generate an error. Add the following code to

public void createError()
	throw new Error("Intentional Problem");

This method will create a forced error when called, but it needs to be called. Change the onCreate method to explicitly call createError, then run the modified application.

You will see your application "crash" with a generic error message and you will need to "force quit" it.


To better track down the error, you can run your application in "debug" mode. Right-click and pick "Debug as ..." or select "Debug " from the Run menu. The first time you need to confirm that you want to open the debug perspective - confirm it. Your app will now run, but will stop when it generate the error so you can inspect exactly where the error occurs and - hopefully - can figure out why. To resume you application, press the "play" button or hit "F8".

You can also right-click in the source code to introduce "breakpoints" to pause execution of your program at specific locations to inspect the inner workings of your program. If you add a breakpoint and debug your application, it will stop at the indicated point so you can inspect your program's state. Don't forget to remove all breakpoints if you no longer need them.

Note that in the upper-right corner you should be able to switch between the "Debug" and "Java" perspective.


Another convenient method to find out what you application is doing while running is to add various log messages. You need to import the Log class from the android.util package, then you can log information or other messages that can be found in the LogCat window of the Debug perspective. There are five log messages you can generate:

Each call to a Log method has two parts, a tag and a message part. You can use the tag to filter out those messages you generated from all log messages. Here is how it all works:

  1. Remove the call to createError so that your application runs without problems again.
  2. Add a tag constant to your class, like:

       private static final String DEBUG_TAG = "FirstAppLogging";
  3. Import the Log class (you can let Eclipse do this for you):

       import android.util.Log;
  4. Add one or more calls with your debug tag to log informative messages, like:

       Log.i(DEBUG_TAG, "Info about FirstApp");

Now execute your app in debug mode. You will find your log messages in the LogCat window of the debug perspective, and you can add a filter (by the tag you defined) to filter out your messages only. If you can't see the LogCat window, open it by selecting "Window | Show View | LogCat" from the Eclipse menu

Now you have two ways to peek at your applications while it is running. Use what you find more convenient - log messages can be particularly helpful - but do not forget to turn logging off when you release a working, finished application. A convenient way is to create a central "logging" method in your app that logs all messages only when a boolean variable is set to true. That way you can centrally turn logging on and off for your app (even programmatically via application settings or preferences).