关于Log4j的总结

来源:互联网 发布:网络保险平台排名 编辑:程序博客网 时间:2024/05/22 01:31

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;    }}
原创粉丝点击