Monday, 14 March 2011

Debugging Android apps - LogCat

Logcat is a specifc system for android to print messages. It is similar to the System.out.println() of Java, but the messages are classified.

To use them, follow those steps
1. Enable the views
you can open the logcat tab in eclipse as I explained here:
http://www.jiahaoliuliu.com/2011/03/debugging-android-apps-basic-steps.html

2. Import the package
In your java code, you should include the code
import android.util.Log;

3. Choose the type of log to use
There are 7 types of logs, each one with different colour:
  • Verbose (Black)
  • Debug (Blue)
  • Information (Green)
  • Warning (Orange)
  • Error (Red)
  • Fatal (??)*
  • Silent#
They are sorted by the order of importance. The Verbose is the less important one, and Silent is the most important one. In fact, Silent is so restrictive that NOTHING will be displayed

To invoke the logs, use the function
  • Verbose (Black):
Log.v(tag, message);
  • Debug (Blue): 
Log.d(tag, message);
  • Information (Green): 
Log.i(tag, message);
  • Warning (Orange): 
Log.w(tag, message);
  • Error (Red):
Log.e(tag, message);
or
Log.e(tag, message, throwable);

As you maybe noticed, you invoke them using the function Log and then the first letter of the type of the log.

For the last log, Error, it can also include the throwable (exception, for example) in the log message.

In these functions, both tag and message are string. Tag is used to classified the log and message is what you want to show. If you are not going to classified it, it doesn't matter what you put in the tag field.

Optional: Create a specific filter
Optionally, the tag could be used to classified the logs. In most of the cases, the logcat shows you all the information in the system. If you just want to see the information about your specific app, you can use tag to group them.
1. In the LogCat tab, press on the button "+". It will ask you to create a new filter.
2. Enter any filter name
3. Enter the tag you want to see. For example, if you application is called "MyOwnApplication", you can put the tag as "MyOwnApplication" and fill this field with it. Make sure that the tag used for your application is unique in the system.
4. Click on "OK" button.

Once the filter is done, you will see a button in the logcat tab, near "Log" with your filter's name.

* I tried to find information about the error type "Fatal". Nothing is found. Even in the SDK, the "Fatal" log is not implemented as usual (has it been implemented?) If you find any information, please, write a comment.

#In fact, since Silent is too restrictive, not message will be displayed.Then no method could invoke a log which is "Silent".

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

No comments:

Post a Comment