Log Management in Mobile development

Source: Internet
Author: User

In Android mobile development, logs provide a lot of convenience for us. But after the application is published and does not want the application to output the log information, it is possible to design a log switch to manage the log in the application uniformly. The following simple log class to complete such a function, there is a need for friends to refer to.

 Packagecom.hitech.jni4cppdemo.utils; Public classLog {//App Name    Private Static FinalString TAG = "Resxtrojan"; //Log Switch    Private Static BooleanSwitches =false; //Log Level    Private Static intLevel =Android.util.Log.VERBOSE;  Public Static voidV (Object log) {if(switches) {if(Level <=Android.util.Log.VERBOSE) {if(functionname ()! =NULL) {android.util.log.v (TAG, functionname ()+log); } Else{android.util.log.v (TAG, log.tostring ()); }            }        }    }     Public Static voidd (Object log) {if(switches) {if(Level <=Android.util.Log.DEBUG) {if(functionname ()! =NULL) {android.util.log.d (TAG, functionname ()+log); } Else{android.util.log.d (TAG, log.tostring ()); }            }        }    }     Public Static voidI (Object log) {if(switches) {if(Level <=Android.util.Log.INFO) {if(functionname ()! =NULL) {android.util.log.i (TAG, functionname ()+log); } Else{android.util.log.i (TAG, log.tostring ()); }            }        }    }     Public Static voidW (Object log) {if(switches) {if(Level <=Android.util.Log.WARN) {if(functionname ()! =NULL) {ANDROID.UTIL.LOG.W (TAG, functionname ()+log); } Else{ANDROID.UTIL.LOG.W (TAG, log.tostring ()); }            }        }    }     Public Static voide (Object log) {if(switches) {if(Level <=Android.util.Log.ERROR) {if(functionname ()! =NULL) {android.util.log.e (TAG, functionname ()+log); } Else{android.util.log.e (TAG, log.tostring ()); }            }        }    }    Private StaticString functionname () {stacktraceelement[] sts=Thread.CurrentThread (). Getstacktrace (); if(sts = =NULL) {            return NULL; }         for(stacktraceelement st:sts) {if(St.isnativemethod ()) {Continue; }            if(St.getclassname (). Equals (Thread.class. GetName ())) {                Continue; }            if(St.getclassname (). Equals (Log.class. GetName ())) {                Continue; }            return"At" + st.getfilename () + ", line" +St.getlinenumber ()+ ", Method:" + st.getmethodname () + ","; //return "Thread:" + thread.currentthread (). GetName () + "; Source: "//+ st.getfilename () + "\nat line:" + st.getlinenumber ()// + "; Method: "+ st.getmethodname ();        }        return NULL; }}

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.