简单的封装log

public class MyLogger {

    public static boolean IS_DEBUG = true;

    /**
     * 最终的打印方法
     * 打印内容:tag:>>>>>>>>>>;
     * msg:方法名(类名.java:行数): 要打印的信息
     *
     * @param msg 信息
     */
    private static void takeLogE(String methodLine, String msg) {
        Log.e(">>>>>>>>>>", methodLine + msg);
    }

    public static void e(Object msg) {
        if (IS_DEBUG) {
            String methodLine = getMethodAndLine();
            takeLogE(methodLine, String.valueOf(msg));
        }
    }

    /**
     * 打印方法的类名
     *
     * @return 类名前缀
     */
    public static String getClassName() {
        StackTraceElement traceElement = ((new Exception()).getStackTrace())[2];
        String className = traceElement.getFileName();
        //去除文件名中的后缀
        if (className.contains(".java")) {
            className = className.substring(0, className.length() - 5);
        }
        return className;
    }

    /**
     * 打印的方法+类名+所在行数(显示超链)
     */
    private static String getMethodAndLine() {
        String result = "";
        StackTraceElement traceElement = ((new Exception()).getStackTrace())[2];
        result += traceElement.getMethodName();
        result += "(" + traceElement.getFileName();
        result += ":" + traceElement.getLineNumber() + "): ";
        return result;
    }

    /**
     * 打印分割线,主要用于程序启动或销毁时在log中做标记
     *
     * @param msg 信息
     */
    public static void line(String msg) {
        if (IS_DEBUG) {
            takeLogE("---====================================---\n" +
                    "\t我是" + msg + "的分割线-" + getCurrentTime() +
                    "\n---====================================---", null);
        }
    }

    public static String getCurrentTime() {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return dateFormat.format(System.currentTimeMillis());
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值