A log class on android that can trace code specific to a function line

Source: Internet
Author: User

The Code is as follows: Copy codeThe Code is as follows: package xiaogang. enif. utils;

/**
* The Class LogUtils for log printing, which help us
* Easy to trace our codes or logics in the project.
*
* @ Author zhao xiaogang
* @ Time 2011.4.12
*/
Public class LogUtils {

Private final static int VERBOSE = 0;
Private final static int DEBUG = 1;
Private final static int INFO = 2;
Private final static int WARN = 3;
Private final static int ERROR = 4;
Private final static int DEFAULT_LEVEL =-1;

Private int level;

Private final String clazz;

Private static final String TAG = "LogUtils ";

Public static LogUtils getDebugLog (Class <?> Clazz, int l ){
LogUtils log = new LogUtils (clazz );
Log. level = l;
Return log;
}

Public static LogUtils getLog (Class <?> Clazz ){
Return new LogUtils (clazz );
}

Public LogUtils (Class <?> Clazz ){
This. clazz = "[" + clazz. getSimpleName () + "]";
Level = DEFAULT_LEVEL;
}

Public void verbose (String message ){
Verbose (message, null );
}

Public void debug (String message ){
Debug (message, null );
}

Public void info (String message ){
Info (message, null );
}

Public void warn (String message ){
Warn (message, null );
}

Public void error (String message ){
Error (message, null );
}

Public void verbose (String message, Throwable t ){
If (VERBOSE <level)
Return;
If (message! = Null)
Android. util. Log. v (TAG, clazz + "Line:" + getLineNumber () + ":" + message );
If (t! = Null)
Android. util. Log. v (TAG, clazz + "Line:" + getLineNumber () + ":" + t. toString ());
}

Public void debug (String message, Throwable t ){
If (DEBUG <level)
Return;
If (message! = Null)
Android. util. Log. d (clazz, clazz + "Line:" + getLineNumber () + ":" + message );
If (t! = Null)
Android. util. Log. d (clazz, clazz + "Line:" + getLineNumber () + ":" + t. toString ());
}

Public void info (String message, Throwable t ){
If (INFO <level)
Return;
If (message! = Null)
Android. util. Log. I (TAG, clazz + "Line:" + getLineNumber () + ":" + message );
If (t! = Null)
Android. util. Log. I (TAG, clazz + "Line:" + getLineNumber () + ":" + t. toString ());
}

Public void warn (String message, Throwable t ){
If (WARN <level)
Return;
If (message! = Null)
Android. util. Log. w (TAG, clazz + "Line:" + getLineNumber () + ":" + message );
If (t! = Null)
Android. util. Log. w (TAG, clazz + "Line:" + getLineNumber () + ":" + t. toString ());
}

Public void error (String message, Throwable t ){
If (ERROR <level)
Return;
If (message! = Null)
Android. util. Log. e (TAG, clazz + "Line:" + getLineNumber () + ":" + message );
If (t! = Null)
Android. util. Log. e (TAG, clazz + "Line:" + getLineNumber () + ":" + t. toString ());
}

Private static int getLineNumber (){
Return Thread. currentThread (). getStackTrace () [5]. getLineNumber ();
}
}

If it is easy to use, remember to give praise, hey!

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.