Android offers a system-wide logging capability. You can
log from anywhere in your code by calling
Log.d(TAG, message), where
TAG and message are some
strings. TAG should be a tag that is meaningful to you given your code.
Typically, a tag would be the name of your app, your class, or some
module. Good practice is to define TAG as a Java constant for your
entire class, such as:
private static final String TAG = "StatusActivity";
Before your code will compile, you need to import the
Log class. Eclipse has a useful feature under Source→Organize Imports, or Ctrl+shift+O for short.
Usually, this feature will automatically organize your import
statements. However, in the case of
Log, often there is a conflict because there
are multiple classes named Log. This is where you
have to use your common sense and figure it out. In this case, the
ambiguity is between the Android
Log and Apache
Log classes, so choice should be
Log takes different severity levels.
.d() is for debug
level, but you can also specify
.e() for error,
.w() for warning, or
.i() for info. There’s
.wtf() severity level
for errors that should never happen. (It stands for What a Terrible
Failure, in case you were wondering.) Eclipse color-codes log messages
based on their severity level.
Eclipse’s Organize Imports tool can sometimes lead to
hard-to-find problems. For example, if your project doesn’t have
R.java generated (which might happen because there’s an earlier problem ...