Free Android app covering all aspects of addiction, including prevention and treatment

Using Android's Log class API to debug Android application code

  • Written by  Clive

Using LogCat in Android Studio or Eclipse to debug your code

Android logcat icon

You’ve spent hours developing your app and each time you test it, it either bombs or returns results that you never expected. You would love to see what’s causing the problem. Well you can. Android’s Log class enables you to do just that. You can use the Log class to display messages as the app runs and even filter the messages so that only messages that you are interested in are displayed.


Here’s an example.  Let’s say you have a method that calculates the combined value of two ages. You pass the two ages as parameters and the method calculates and returns the total. You run the app and for some reason the method does not return the results that you expected. By using Android’s Log class, you can add the two (Log.i(tag, msg)) lines to the calculateCombinedAge() method like this:

Android LogCat debug example code

Now when you run the app, the system displays the values of ageOne, ageTwo and combinedAges, and you are able to check if these are correct. If the ages passed as parameters are correct but the return value is incorrect, then there is probably something wrong with the logic that calculates the total. On the other hand if the ages are wrong, then the problem possibly lies elsewhere, perhaps where these ages are input. You could then add more log tags to your code and eventually track down the error.

You should not use LogCat messages in live apps as it will affect the performance of the app.

Filter the Log output to debug Android code

Order of priority

All log messages have a priority indicator and a tag making it easy for you to identify them:

LogCat debug display for Android Studio

Here we see (marked in yellow) the I (priority) and System.out (tag) identifiers associated with these messages. The tag identifies the component where the message originated while the priority indicates the priority of the message.

Here’s the order of priority (from lowest to highest):

  • V – Verbose
  • D – Debug
  • I – Info
  • W – Warning
  • E – Error
  • F – Fatal
  • S – Silent

Have a look at the preceding image and you’ll see a couple of Debug and Info messages and one Error message, which is in red.

The logcat continually prints messages to the LogCat while the app is running. You probably won’t be interested in most of these messages.

As mentioned earlier, you can create your own tags to easily identify the messages that you are interested in. You filter the messages by using filter expressions which isolate certain tags, the tags that you are interested in, so you won’t see any of the other messages. Here’s an example:

First, you need to declare a tag string. Good coding practice is to declare the tag as a constant:

Declare Android LogCat tag in code

You can then add this line, Log.i(tag,msg), anywhere in your code which will cause the logcat to display an appropriate message. So for example:

,Log tag in Android code for LogCat debugging

Will display the following in the LogCat:

LogCat display filter

Notice the priority I (Info), the tag (CLOCKS) and the message (Action done – name: france).

To debug your code, display the LogCat while your app is running

Once you’ve declared the tag as a constant and included the Log.i(tag,msg) line in your code, you need to run your app and display the logcat.

Displaying the LogCat in Android Studio

To display the LogCat in Android Studio, from the Main menu select View>Tool Buttons to display the tool buttons at the bottom-left of the screen. Click on 6: Android to display the LogCat:

Display LogCat in Android Studio

You can also display the LogCat by pressing Alt + 6.

Displaying the LogCat in Eclipse

To display the LogCat in Eclipse, select DDMS from the top menu bar:

Displaying LogCat in Eclipse for debugging

Then from the top menu select Window>Show View>LogCat

Filtering the LogCat using filter expressions

At this stage, the logcat probably displays a lot of messages and you won’t be able to see your specific messages. You’ll need to set a filter to display only your messages.

Setting a filter expression in Android Studio

To set a filter expression in Android Studio, make sure that the LogCat window is open and then display the drop-down list of filters (see image below):

Android LogCat filter in Android Studio

Select Edit Filter Configuration, click on the green + and type in the name of your filter as well as the  Log Tag and press OK to save:

Creating LogCat filter in Android Studio

Now (using the above example) select the Clocks filter and you should see all the messages with the CLOCKS tag:

LogCat filter display

Setting a filter expression in Eclipse

Make sure that the LogCat is displayed:

LogCat display in Eclipse

The Eclipse LogCat display differs from the Android Studio’s display. Notice the priority level on the left and the separate Tag column. Errors are displayed in red and Warnings in orange.

To set up a filter expression, you need to display the filters on the left. If they’re not showing, you can display them by selecting the button to the right of the red cross:

Display the saved filters view in Eclipse

Tip: use the scroll lock button (the arrow) to stop the display from scrolling.

To set up a filter, click on the green cross and a pop-up window displays:

Set up LogCat filter in Eclipse

Fill in the Filter Name and Log Tag.

This is what is displayed in the LogCat when you select the files filter:

LogCat display in Eclipse

I hope this tutorial on using the LogCat helps you to debug your Android apps.


Subscription form