Log use and Output line number method

Source: Internet
Author: User
Tags throwable
Output the log in the program, using the Android.util.Log class.
This class provides a number of static methods

LOG.V (string tag, string msg);
LOG.D (string tag, string msg);
LOG.I (string tag, string msg);
LOG.W (string tag, string msg);
LOG.E (string tag, string msg);

correspond to Verbose, Debug, Info, Warning,error respectively.

Tag is an identity, can be any string, you can usually use the class name + method name, primarily to provide a filter condition when viewing the log.


The program does not output any information in the IDE's console after it has been run.

If you want to view the log later, use the

ADB logcat

For more information on the ADB, please check the official website.

When the ADB logcat is executed, all log information is displayed in real time in a tail manner.

At this point we usually need to filter the information to display the information we need, at which time we specify the tag is useful.

ADB logcat-s myandroid:i

This will display only the tag as myandroid, the level I or the level higher than I (warning,error) log information.

The sample code is as follows:
Java code   package com.zijun;       import android.app.activity;    import android.content.context;    import android.graphics.canvas;    import android.os.bundle;    import android.util.log;    import android.view.motionevent;    import android.view.view;       public class myandroid extends activity {                protected static final String  Activity_tag= "Myandroid";                 @Override         protected void oncreate (bundle icicle)  {            super.oncreate (Icicle);            setcontentview (This) (New myview);  &nBsp     }        public class MyView extends  view {           public myview (Context c)  {               super (c);             }              @Override            protected void ondraw ( Canvas canvas)  {                             @Override             public boolean onmotionevent (motionevent event)  {               LOG.I (MyAndroid.ACTIVITY_TAG,  "= ===========================");                                 LOG.D (myandroid.activity_tag,  "HAHA   this is a DEBUG of MyAndroid.  ");                LOG.I (MyAndroid.ACTIVITY_ tag,  "haha , this is a info of myandroid. ");                LOG.W (MyAndroid.ACTIVITY_ tag,  "haha , this is a warning of myandroid. ");                   return  True            }                     }  &nbsP   }  


After the program is run, execute ADB logcat-s myandroid:i at the command line
Then click the mouse on the phone simulator screen to see the corresponding log information.

Print line numbers

Throwable state = new Throwable ();

stacktraceelement[] stacktraces = State.getstacktrace ();

for (Stacktraceelement st:stacktraces) {

System.out.println (St.tostring ());

}

Through Stacktraceelement, we can get the current class,filename,linenumber,methodname

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.