Print to console in Android using Eclipse: Android

  • June 4, 2010

For the Java programmer, while the move to Android is for the most part great and smooth, there are a few hiccups that he or she must overcome. The most annoying of which is the seeming inability to view System.out.print messages. These are in fact quite easy to get if we are developing in Eclipse however it goes against best practices to use System.out.print.  Instead in Android we should be using the Log class.  See below the screen shot for an example of the Log class.

In Eclipse:
Window > Show View > Other > Android > LogCat > OK

We can now see the print statements in the LogCat. Side Note: LogCat logs almost everything the phone is doing so you might have to wade through the other data to find your print statements.

LogCat Screen Shot

IF IT DOESN’T SHOW ANYTHING: Sometimes when we first use LogCat, the logs are empty. This is often because the emulator or device doesn’t have focus. To fix that go to the DDMS perspective and click the emulator or device on the top left that you want to have focus. When you go back to the Java perspective you should see your logs.

How to Use the Log Class

If you have any control over the code you should try to use the Log class as much as possible.  An example use of Log would be the following.

import android.util.Log;
Log.i("App_Name", "This is the meet of the log");

Here I use .i to indicate an info level log.  The following levels are availible

  • DEBUG – Log.d
  • ERROR – Log.e
  • INFO – Log.i
  • VERBOSE – Log.v
  • WARN – Log.w

Using the Log class allows us to filter on the tags by the tag name or logging level.  Production tag names will typically be the app name but in my debugging I like to use my last name as the tag.  This allows me to quickly filter on my last name and see only the debugging logs without having my last name clutter up the logs themselves.

No Comments

You can leave the first : )

Leave a Reply

Your email address will not be published. Required fields are marked *