Writing Logs in Android Example and Tutorial

        The Android Logging System provides mechanism for collecting and reading system debug output. This output can be read from LogCat view in Eclipse. Logcat dumps a log of system messages, which include things such as stack traces when the emulator throws an error and
messages that you have written from your application by using the Log class.

        We can write these logs messages using Log class in Android. Common logging methods are as follows :

  • v(String, String) (verbose)
  • d(String, String) (debug)
  • i(String, String) (information)
  • w(String, String) (warning)
  • e(String, String) (error)
We can write logs with just one line of code as follows - 
Log.d("My Custom Tag", "CompileTimeError.com");

"My Custom Tag" - is the tag name which being the identity of the message. We can filter the messages in LogCat using TagName and "CompileTimeError.com" is the message that we wish to write. 
To write log messages we need to import android.util.Log

Opening LogCat in Eclipse
         LogCat is open in Eclipse by default or else we can open it as as shown below :

Windows - ShowView - Others - LogCat


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

public class MainActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
Log.d("Tag Name", "Log Message");
Log.d("My Custom Tag", "CompileTimeError.com");

public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;

Now run the program and open LogCat to see your log message :

Thats it.. We are done with writing and viewing log messages in android..



Post a Comment