Java记录日志Logger程序测试

Java记录日志Logger程序测试

public class Test{
    private static final Logger log = Logger.getLogger("com.cn.log");

    int read(String s,String ss){
        log.entering("Test", "read",new String[] {s,ss});
        int count = 1;
        log.exiting("Test", "read",count);
        return count;
    }
    public static void main(String[] args){
        // TODO Auto-generated method stub
        //要想日志可以记录消息,消息的级别必须大于等于日志记录器的级别
        log.setLevel(Level.FINER);
        log.severe("WARNING");
        //log.entering()与log.exiting()将生成FINER级别的日志记录,默认情况下控制台的级别为INFO
        Test t = new Test();
        t.read("专注","该做什么的时候专心做什么,不要分心");
        //测试子记录器与父记录器的日志级别继承问题
        System.out.println("--------------------------------");
        Logger logChild = Logger.getLogger("com.cn.lo.child");
        log.finer("Child log");
        //日志消息本地化测试
        Logger logLocal = Logger.getLogger("com.locallog", "持有对象.logmsg");
        logLocal.info("readingFile");
        logLocal.info("renamingFile");
        logLocal.log(Level.INFO, "renamingFile", new String[]{"专注","继续专注"});
        //处理器测试,本次测试生成的log文件位于C:\Users\Administrator,跟《java核心技术》书中所说不一样
        try {
            Handler handler = new FileHandler();
            log.addHandler(handler);
            t.read("a", "b");
            log.severe("fileHandle");
        } catch (SecurityException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

你可能感兴趣的:(Java记录日志Logger程序测试)