Make Android logs more useful

Source: Internet
Author: User

Address: http://marspring.mobi/android-log/

Android logs differ from those of Java EE such as log4j. For example, you cannot simply set the log to be printed by level, or print the information of the class that calls the log in the log, method Name, row number, and so on. Stacktraceelement is used to encapsulate the android log class. You can print the class in which the current log is located, the method name called, and the row number.

/*****/Package COM. XX. market. util; import Java. util. collection; import Java. util. hashmap; import Java. util. iterator; import Java. util. set; import android. util. log; public class logutility {public static Boolean getdecideresult (stacktraceelement Ele) {return true;} public static string getcallerinfo () {stacktraceelement [] stack = (New throwable ()). getstacktrace (); stacktraceelement ele = NULL; string classname = ""; string methodname = ""; int lineno = 0; If (stack. length> 2) {ele = stack [2]; try {classname = Class. forname (Ele. getclassname ()). getsimplename (); methodname = ELE. getmethodname (); lineno = ELE. getlinenumber ();} catch (classnotfoundexception e) {}} string callerinfo = classname + ":" + methodname + ":" + lineno + "=>"; return callerinfo ;} public static void I (string tag, double info) {If (constants. debug) {log. I (TAG, getcallerinfo () + String. valueof (Info) ;}} public static void I (string tag, float info) {If (constants. debug) {log. I (TAG, getcallerinfo () + String. valueof (Info) ;}} public static void I (string tag, int info) {If (constants. debug) {log. I (TAG, getcallerinfo () + String. valueof (Info) ;}} public static void I (string tag, long info) {If (constants. debug) {log. I (TAG, getcallerinfo () + String. valueof (Info) ;}} public static void I (string tag, string info) {If (constants. debug) {log. I (TAG, getcallerinfo () + String. valueof (Info) ;}} public static void W (string tag, string info) {If (constants. warn) {log. W (TAG, getcallerinfo () + info) ;}} public static void E (string tag, string info) {If (constants. error) {log. E (TAG, getcallerinfo () + info) ;}@ suppresswarnings ("rawtypes") public static void I (string tag, string startmsg, collection info) {If (constants. debug) {stringbuffer MSG = NULL; If (startmsg = NULL) {MSG = new stringbuffer (getcallerinfo ();} else {MSG = new stringbuffer (getcallerinfo () + startmsg);} iterator itr = info. iterator (); While (itr. hasnext () {MSG. append (itr. next (). tostring (); MSG. append (";");} log. I (TAG, MSG. tostring () ;}@ suppresswarnings ("rawtypes") public static void I (string tag, string startmsg, hashmap info) {If (constants. debug) {stringbuffer MSG = NULL; If (startmsg = NULL) {MSG = new stringbuffer (getcallerinfo ();} else {MSG = new stringbuffer (getcallerinfo () + startmsg);} set keyset = info. keyset (); iterator itr = keyset. iterator (); While (itr. hasnext () {object key = itr. next (); object value = info. get (key); MSG. append (key. tostring (); MSG. append ("="); MSG. append (value. tostring (); MSG. append (";");} log. I (TAG, MSG. tostring () ;}} public static void debugforimage (string info) {// remove if (constants. debug) {log. I ("market-Lion", getcallerinfo () + info) ;}} public static void debug (string info) {// remove if (constants. debug) {log. I ("list", getcallerinfo () + info );}}}

 

 

 

Related Article

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.