Friday 6 January 2012

Log in Android

Every Android developer use Logging during the developing of android applications.
Android provides a great interface to work with the systems logcat.
actually a number of developers don’t turn down their logging when they
go to ship their app to the android market.

This short post demonstrates how to wrap calls to the Log class
to control the amount of information visible in logcat.

The benefit here is you won’t have to worry about removing print lines or
maintaining variables for levels that you might forget
to change before building your release apk in Android market.

Changing the log level on device is actually very simple.
Simply run adb shell setprop log.tag.<YOUR_LOG_TAG> <LEVEL>

In Android Log class contains some methods to print debugging information to the logcat utility.
Logcat is visible in the debug perspective in Eclipse.

Here below is a sample Class showing log level checking prior to printing to the log.

import com.androidsurya.util.Log;

public class MyLogClass {

public static void d(String tag, String msg) {
if (Log.isLoggable(tag, Log.DEBUG)) {
Log.d(tag, msg);
}
}

public static void i(String tag, String msg) {
if (Log.isLoggable(tag, Log.INFO)) {
Log.i(tag, msg);
}
}

public static void e(String tag, String msg) {
if (Log.isLoggable(tag, Log.ERROR)) {
Log.e(tag, msg);
}
}

public static void v(String tag, String msg) {
if (Log.isLoggable(tag, Log.VERBOSE)) {
Log.v(tag, msg);
}
}

public static void w(String tag, String msg) {
if (Log.isLoggable(tag, Log.WARN)) {
Log.w(tag, msg);
}
}
}

MyLogClass is simply calls to the android Log class
Now you can simply reference  
MyLogClass.d(“MyAndroidApp”, “This is sample debug message”);
in your Android activity and if the device/emulator running the app has the log level set
to debug the message will appear.running the app has the log level set to debug the message will appear.

For more information about Log:

http://developer.android.com/reference/android/util/Log.html

No comments:

Post a Comment

Android SQLite Database Viewer or Debuging with Stetho

Every Android Developer uses SQLite Database to store data into the Android Application data. But to view the data in SQLite have a lot of...