前段时间封装了一个LogUtils的工具类,每次写项目,都会或多或少的修改一点,渐渐的改成了目前的样子,分享如下:
public class LogUtils {
static StringclassName;//类名
static StringmethodName;//方法名
static int lineNumber;//行数
private static boolean isdebug =true; //控制是否打印日志
private LogUtils(){
/* Protect from instantiations */
}
public static boolean isDebuggable() {
return isdebug;
}
private static StringcreateLog( String log ) {
StringBuffer buffer =new StringBuffer();
buffer.append(log);
return buffer.toString();
}
private static void getMethodNames(StackTraceElement[] sElements){
className = sElements[1].getFileName();
StringBuffer buffer =new StringBuffer();
className = buffer.append("(").append(className).append(":").append(lineNumber).append(")").toString();
methodName = sElements[1].getMethodName();
lineNumber = sElements[1].getLineNumber();
}
public static void e(String message){
if (!isDebuggable())
return;
// Throwable instance must be created before any methods
getMethodNames(new Throwable().getStackTrace());
Log.e(className, createLog(message));
}
public static void i(String message){
if (!isDebuggable())
return;
getMethodNames(new Throwable().getStackTrace());
Log.i(className, createLog(message));
}
public static void d(String message){
if (!isDebuggable())
return;
getMethodNames(new Throwable().getStackTrace());
Log.d(className, createLog(message));
}
public static void v(String message){
if (!isDebuggable())
return;
getMethodNames(new Throwable().getStackTrace());
Log.v(className, createLog(message));
}
public static void w(String message){
if (!isDebuggable())
return;
getMethodNames(new Throwable().getStackTrace());
Log.w(className, createLog(message));
}
public static void wtf(String message){
if (!isDebuggable())
return;
getMethodNames(new Throwable().getStackTrace());
Log.wtf(className, createLog(message));
}
}
打印出来的效果就是这样子哦:
点击蓝色字体可以直接定位到当前java所在行数
网友评论