操作读取日志文件,
1、使用默认的日志文件,并验证默认级别
1 public void originalConfig() 2 { 3 Logger logger = Logger.getLogger(Logger.GLOBAL_LOGGER_NAME); 4 logger.setLevel(Level.FINE);//add level limit 5 logger.info("enter my info :"); 6 try 7 { 8 int t =9/0; 9 } 10 catch(Exception e) 11 { 12 logger.info("devide is '0',which is prohibit!" + e); 13 logger.fine("This is a fine level message!"); 14 } 15 }
2、构建自己的日志文件,并使用控制台打印(ConsoleHandler)
1 public void bypassConfig() 2 { 3 Logger mylogger = Logger.getLogger("com.lv.logger"); 4 mylogger.setLevel(Level.FINE); 5 mylogger.setUseParentHandlers(false); 6 Handler handler = new ConsoleHandler(); 7 handler.setLevel(Level.FINE); 8 mylogger.addHandler(handler); 9 10 mylogger.info("my own logger and handler!"); 11 mylogger.fine("this is a fine message!"); 12 }
3、构建自己的日志文件,使用输出文件方式(FileHandler),指定路径
1 public void myFileHandler() 2 { 3 Logger mylogger = Logger.getLogger("huihui.logger"); 4 mylogger.setLevel(Level.INFO); 5 mylogger.setUseParentHandlers(false); 6 FileHandler handler; 7 try { 8 handler = new FileHandler("../logs/mylogger%u.log"); 9 mylogger.addHandler(handler); 10 } catch (SecurityException e) { 11 e.printStackTrace(); 12 } catch (IOException e) { 13 e.printStackTrace(); 14 } 15 mylogger.info("this is a file log!"); 16 }