Friday, May 4, 2012

How to display messages in the LogCat window from code


How to display messages in the LogCat window from code

A simple debugging technique is to display your own messages in the log window. For Java applications, this is done by printing messages in the console window usingSystem.out.println(String message) method.
For Android projects, there is a better solution. The Log class has some static methods used to print messages in the system log which is displayed by LogCat view. Each method prints a message with a predefined log level:
        Log.e("MyTag", "Error message with my own tag");
        Log.w("dalvikvm", "VM Warning message");
        Log.d("MyTag", "Debug message");
        Log.i("MainActivity","Information message");
        Log.v("MyTag", "Verbose message");
        Log.wtf("WTF", "What a Terrible Failure");
The methods have multiple signatures. The previous example shows only the (String Tag, String Message) one. For tags is recommended to use your own tags or the class name. This will allow you to filter them easily.

If you have problems with the examples or you don’t understand our explanations, put a question in the comments and we will help you. Also, any suggestion or comment that will improve this material is appreciated.

No comments: