Adding Logging Support to Your Android Application

Before you start diving into the various features of the Android SDK, you should familiarize yourself with logging, a valuable resource for debugging and learning Android. Android logging features are in the Log class of the android.util package.

Some helpful methods in the android.util.Log class are shown in Table 3.2.

Method

Purpose

Log-e()

Log errors

Log.w()

Log warnings

Log-i()

Log informational

messages

Log.d()

Log Debug messages

Log.v()

Log Verbose mesages

To add logging support to MyFirstAndroidApp, edit the file MyFirstAndroidApp.java. First, you must add the appropriate import statement for the Log class:

import android.util.Log;

To save time in Eclipse, you can use the imported classes in your code and add the imports needed by hovering over the imported class name and choosing the Add Imported Class option.

You can also use the Organize imports command (Ctrl+Shift+O in Windows or Command+Shift+O on a Mac) to have Eclipse automatically organize your imports. This removes unused imports and adds new ones for packages used but not imported. If a naming conflict arises, as it often does with the Log class, you can choose the package you intended to use.

Next, within the MyFirstAndroidApp class, declare a constant string that you use to tag all logging messages from this class.You can use the LogCat utility within Eclipse to filter your logging messages to this debug tag:

private static final String DEBUG_TAG= "MyFirstAppLogging";

Now, within the onCreate() method, you can log something informational:

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

Easy Software Profits

Easy Software Profits

Here's How Anyone Can Start, Build and Run their Own Successful Software Empire Without Knowing the First Thing About Programming, Scripting Or Any 'Coder' Stuff. There is no doubt about it software sells. Software has a high percieved value and it solves problems for people.  A claim that can't always be made for PDF books, Video products and the like.

Get My Free Ebook


Responses

Post a comment