log4j.properties配置文件放置的文件夾如上圖所示。
具體代碼的初始化如下圖所示:
private static void initLogRecord(){ Properties props = null; FileInputStream fis = null; try { props = new Properties(); fis = new FileInputStream("confs/log4j.properties"); props.load(fis); PropertyConfigurator.configure(props); } catch (Exception e) { e.printStackTrace(); } finally { if (fis != null) try { fis.close(); } catch (IOException e) { e.printStackTrace(); } fis = null; } }
這樣在Java項目中main方法中調用上面的方法就可以實現啟動日志了,注意這個方法放在main方法的首行初始化,這樣就可以打印程序啟動過程中的日志了,OK!