Android log output Tool

Source: Internet
Author: User

Package com. rctd. tmzs. util;


Import android. util. Log;


/**
* Log output Tool
* @ Author WuHao; Email: 1024778537@qq.com
* @ Version V1.0; Date: 11:35:20
*/
Public class LogUtil {


Private static String tag = "LogUtil ";

Private int logLevel = Log. VERBOSE;
Private static final boolean isDebug = true;
Private static LogUtil instance = null;


Public static synchronized LogUtil getInstance (){
Return getInstance (Log. VERBOSE );
}

Public static synchronized LogUtil getInstance (int level ){
If (instance = null)
Instance = new LogUtil ();
If (level> = 2)
Instance. setLevel (level );
Return instance;
}


Private LogUtil (){

}

Public void setLevel (int level ){
This. logLevel = level;
}


Private String getFunctionName (){
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 (this. getClass (). getName ())){
Continue;
}
Return "[" + Thread. currentThread (). getName () + "(" + Thread. currentThread (). getId () + "):" + st. getFileName () + ":" + st. getLineNumber () + "]";
}
Return null;
}


Private void outMsg (Object str ){
String msg = (str = null? "": Str. toString ());
String name = getFunctionName ();
String ls = (name = null? Msg. toString (): (name + "-" + msg ));
If (logLevel = Log. DEBUG ){
Log. d (tag, ls );
} Else if (logLevel = Log. INFO ){
Log. I (tag, ls );
} Else if (logLevel = Log. WARN ){
Log. w (tag, ls );
} Else if (logLevel = Log. ERROR ){
Log. e (tag, ls );
} Else {
Log. v (tag, ls );
}
}


Public void out (Object msg ){
If (isDebug ){
OutMsg (msg );
}
}
}

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.