Android logging

In this Android tip, I am going to introduce you a useful tool of Android system, logging tool. The logging tool allows you to write different types of logged messages to the Android logging system. The messages can be classified as verbose, debug, information, warning, and error messages. The error log is the most important message and the verbose message is the least important message. The methods used to write these messages (in order) are Log.v(), Log.d(), Log.w(), Log.e(). These methods have two arguments: tag and msg. The tag argument specifies the source of the logged message. It is used to identify the class or activity in which the log call occurs. The msg argument is the message to be logged. Based on the Android documentation, Verbose should never be compiled into an application except during development. Debug logs are compiled in but stripped at runtime. Error, warning and information logs are always kept. Personally, i generally use Log.i() and Log.e() methods during applications development process. I use Log.i() to write outputs to the logging system so that i can track an application's outputs and use the Log.e() to write error message in the try...catch blog to see something went wrong in my code.

To view the logged messages, you can use LogCat. It comes with Eclipse ADT. In the LogCat window, you can filter logged messages. Below is an example code. In the code, the Log.i() and Log.e() methods are used to write information and error messages.

package com.example.androidexample;

import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;

public class MainActivity extends Activity {

Activity context;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

String tag="MainActivity";
Log.i(tag,"Trying to convert a string to number.");

try{
String str="123abc";
int num=Integer.parseInt(str);
}catch(Exception e){
Log.e(tag,"Error in converting the string to number.");
}

}


}


Android-loggin

Posted by: Dara | post date: 09-13-2014 | Subject: Android Apps Development

write programming tip



This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.