Selasa, 10 Agustus 2010

Reader Feedback: On Log Tags

Sam's Teach Yourself
Android Application Development
in 24 Hours
For the Sam's Teach Yourself Android Application Development in 24 Hours book, we received a question regarding debug tag code in Hours 9 and 10.

Log.d(DEBUG_TAG, "SHARED_PREFERENCES"); 
// (From Hour 10, Page 177)

A couple of readers have asked us: What is DEBUG_TAG? Where is it defined?

The short answer is: you need to define it! By this point in the book, we aren't displaying every line of code you need for the application to compile--only the relevant new code for that particular lesson. We assume that you've mastered certain fundamental Android skills in the previous chapters of the book. One of these skills is understanding the Log calls, such as Log.v or Log.d. which were discussed in detail in Hour 3, Page 54.  This skill is essential for debugging Android applications and is used regularly throughout the book.

Log method calls simply display diagnostic information to the Logcat logging tool, providing the developer with important information as the application executes. These calls have no impact on the actual application functionality. The calls just log string output for debugging purposes. You can include these logging calls in your applications or simply leave them out (and not provide diagnostic logging). If you want to log information using the logging calls, you will need to supply a tag string as the first parameter for each Log method. In Hour 3, we suggest that you define a logging tag for each and every Android project or class you create.

The logging tag is shown in the log output of logcat, so you can tell that the information is coming from your application, Activity, class, or method. It's that simple.
LogCat output showing all device logs. Cluttered.

You can also filter on just the tag, which shows the real power of it.
LogCat, with filter to show only the tag "GestureFunActivity"

So, when you run across a line in the book that uses such a call, we do not provide the definition of DEBUG_TAG variable. Instead, we assume you know that this is a generic logging call, and you should supply the definition of the variable somewhere in your class. For example, if you want to add logging calls to an Activity class called QuizHelpActivity, class, you should add a definition such as:
private static final String DEBUG_TAG = "QuizHelpActivity";
Or maybe you'd prefer:
private static final String DEBUG_TAG = "QuizHelpActivity Logging Info: ";
Or even something for your entire application:
private static final String DEBUG_TAG = "MyAppLogging";
Those of you who have Java experience will likely recognize that the DEBUG_TAG variable is a constant. That is to say, it has the static final qualifiers. Since the input type to the Log functions are String objects, you then know this to be a static final String. Remember that the logging tag should be descriptive for the specific application or class. In fact, it's often set to the class name, but there is no requirement to do so.

We hope this helps clear up this question. As always, feel free to email us or post a comment right here!

0 komentar:

Posting Komentar

ANDROID BOOK © 2008 Template by:
SkinCorner