LogCat

Intro

Android devices, like other computers, keep a running log of system events.  In order to display this log, Android uses something to called LogCat (accessible in Eclipse via the Android Debug Bridge plugin).  It's a great tool that we'll use extensively for monitoring and debugging our code.  We'll discuss it later, but in the mean time, if you get a window like the one below, set it to verbose and click OK.

[screenshot of LogCat popup dialog lab2_5.png]

If you don't get the popup message, and the LogCat tab is not present in the bottom panel, you can open it manually:

  1. Select Window > Show View > Other...
  2. In the popup dialog, select Android > LogCat and click OK
  3. You should now see the LogCat tab

The Log Class

The Log class is used to output information to LogCat.  The syntax to create a LogCat entry is

Log.i(String TAG, String Message); 

You have five options when creating log entries.  Each type is color coded for easier parsing by the human eye (HINT: Red == Bad).They are (in order of most to least verbose):

From Google: 

Verbose should never be compiled into an application except during development. Debug logs are compiled in but stripped at runtime. Error, warning and info logs are always kept.

We'll use info logs for today's demonstration.  Also, a common method is to define a Constant String variable to use for the TAG string, e.g.

final static String TAG = "PEPIN";

Let's  write an info log entry to LogCat by adding the following code to the onCreate() method:

Log.i(TAG, "Inside onCreate()");

Now run your app and watch the LogCat output.  When you app is launched on the (virtual) device, you should see the new entry:

 

 

Notice in the Tag column we can see the value we defined in out Activity.  We can use the Tag to help narrow down our search when scouring through the LogCat for clues about our running app.  As you can imagine, LogCat can get really cluttered, really fast!  In order to show only the log entries we created, we can use a filter.  To create a filter, click on the green + icon in the Saved Filters window.  

 

 

You can name the filter whatever you want.  We want to filter by Log Tag.  Specifically, we want to filter by the TAG we defined in our Activity...so put that value in the by Log Tag field and click OK.

 

 

TaDa!