utils String path = RunLog4j.class.getClassLoader().getResource("log4j.properties").getPath(); System.out.println(path); //输出/D:/D/idea_test_workplace/log4JDemo/target/classes/log4j.properties 文件的绝对路径 public class LogUtil
String path = RunLog4j.class.getClassLoader().getResource("log4j.properties").getPath(); System.out.println(path); //输出/D:/D/idea_test_workplace/log4JDemo/target/classes/log4j.properties 文件的绝对路径 public class LogUtil { public static void init(){ Properties props = new Properties(); try { FileInputStream istream = new FileInputStream(path); props.load(istream); istream.close(); PropertyConfigurator.configure(props); } catch (IOException e) { e.printStackTrace(); return; } } }使用
public class RunLog4j { //初始化log4j文件的配置 static { LogUtil.init(); } private static final Log logger = LogFactory.getLog(RunLog4j.class); public static void main(String[] args) { // logger.info("This is info message."); // logger.debug("This is debug message."); // // // logger.error("This is error message."); }