为了便于开发,特此封装了一个日志工具类,可以具体到类名,方法名,行数;


代码如下:

public enum  LogUtil {
    INSTANCE;

    private final static boolean all = true;

    private final static boolean i = true;

    private final static boolean e = true;

    private final static boolean d = true;

    private  static String className;

    private static String methodName;

    private static int lineName;

    private final static String TAG = LogUtil.class.getSimpleName();



    public void i(String msg){
        if(all && i){
            getName(new Throwable().getStackTrace());
            Log.i(TAG, createLog(msg));
        }
    }

    public void e (String msg){
        if(all && e){
            getName(new Throwable().getStackTrace());
            Log.e(TAG, createLog(msg) );
        }
    }

    public void d (String msg){
        if(all && d){
            getName(new Throwable().getStackTrace());
            Log.d(TAG, createLog(msg));
        }
    }


    private  static void getName(StackTraceElement []stackTraceElements){
        className = stackTraceElements[1].getClassName();
        methodName = stackTraceElements[1].getMethodName();
        lineName = stackTraceElements[1].getLineNumber();
    }

    private static String createLog(String msg){
        StringBuilder sb = new StringBuilder();
        return sb.append(className).append(methodName).append(lineName).append(":").append(msg).toString();
    }

}

效果:

关于Android中log日志(LogUtil)的封装

相关文章:

  • 2021-07-15
  • 2021-12-14
  • 2022-12-23
  • 2021-11-15
  • 2021-09-06
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-12-09
  • 2022-12-23
  • 2021-10-16
  • 2021-08-18
  • 2022-12-23
  • 2022-02-23
相关资源
相似解决方案