The Code is as follows:
[Java]
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 ());
} Www.2cto.com
Private static int getLineNumber (){
Return Thread. currentThread (). getStackTrace () [5]. getLineNumber ();
}
}
If it is easy to use, remember to give praise, hey!