java 寫日誌到檔案
阿新 • • 發佈:2018-11-16
//日誌檔案 String path = System.getProperty("user.dir");//獲取專案絕對路徑 String logPath = path + "\\run.log"; java.util.logging.Logger log = java.util.logging.Logger.getLogger(logPath); log.setLevel(Level.ALL); FileHandler fileHandler = new FileHandler(logPath,true);//true是以追加的形式 fileHandler.setLevel(Level.ALL); fileHandler.setFormatter(new Formatter() { @Override public String format(LogRecord record) { Date date = new Date(); String sDate = date.toString(); return "[" + sDate + "]" + "[" + record.getLevel() + "]" + record.getClass() + record.getMessage() + "\n"; } }); log.addHandler(fileHandler); log.info("This is test java util log1"); log.warning("This is test java util log2"); fileHandler.close();
適當封裝下就可以啦
注意:引入相關類