关于Log4j的总结

ITeye那边感觉不管不问了,正式转到csdn上来记录自己的学习心得

做移动开发久了了,发现log4j还不太会用,学一下吧,其实很简单也就时一个下午的上手时间,不过想要配的好可能还有不少路要走,至少官方的文档还没有读完。

关于log的理解,其实一就是为了自己调试方便,二就时出bug的时候至少自己知道bug出在什么地方,方便自己定位,估计写过代码的人都知道log的用意,方便自己也方便帮你解bug的人,所以我挺喜欢Android打印的风格,简单明了,其实自己实现一个log类的封装也不难

public class Main {

    private static final String LOG_TAG = Main.class.getSimpleName();

    public static void main(String[] args) {
        Main testMain = new Main();
        testMain.testLog("You will be see a beatiful log.");
    }

    public void testLog(String message) {
        Log.d(LOG_TAG, "testLog() entered");
        Log.w(LOG_TAG, message);
    }
}
public class Log {

    public static final int INFO = 0;
    public static final int DEBUG = 1;
    public static final int WARN = 2;
    public static final int ERR = 3;

    private static int bugLevel = DEBUG;

    public void i(String tag, String message) {
        printLog(INFO, tag, message);
    }

    public static void d(String tag, String message) {
        printLog(DEBUG, tag, message);
    }

    public static void w(String tag, String message) {
        printLog(WARN, tag, message);
    }

    public static void e(String tag, String message) {
        printLog(ERR, tag, message);
    }

    private static void printLog(int level, String tag, String message){
        String curTime = new SimpleDateFormat("YYYY-MM-dd HH:mm:ss").format(System.currentTimeMillis());
        String processName = ManagementFactory.getRuntimeMXBean().getName();
        long pid = Integer.valueOf(processName.substring(0, processName.indexOf("@")));
        long tid = Thread.currentThread().getId();
        if (level >= bugLevel) {
            System.out.println(curTime + " " + pid + " " + tid + ": " + tag + " " + message);
        }
    }

    public void setBugLevel(int level) {
        bugLevel = level;
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值