Logging in Android by Marko Gargenta

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

O'Reilly logo

Logging in Android

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";

Tip

Before your code will compile, you need to import the Log class. Eclipse has a useful feature under SourceOrganize 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 easy.

Note that 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 also a .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.

Warning

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required