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!