方法一:
Properties prop = new Properties();
File f = new File("绝对路径");
FileInputStream is = null;
try {
is = new FileInputStream(f);
prop.load(is);
PropertyConfigurator.configure(prop);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
方法二:
ClassLoader cl = Thread.currentThread().getContextClassLoader();
InputStream is = null;
is = cl.getResourceAsStream("config/Log4j.properties");
Properties prop = new Properties();
prop.load(is);
PropertyConfigurator.configure(prop);
Properties prop = new Properties();
File f = new File("绝对路径");
FileInputStream is = null;
try {
is = new FileInputStream(f);
prop.load(is);
PropertyConfigurator.configure(prop);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
方法二:
ClassLoader cl = Thread.currentThread().getContextClassLoader();
InputStream is = null;
is = cl.getResourceAsStream("config/Log4j.properties");
Properties prop = new Properties();
prop.load(is);
PropertyConfigurator.configure(prop);
本文介绍了两种在Java中使用Log4j的配置方法。第一种方法通过加载本地文件实现配置,涉及创建Properties实例并从指定的绝对路径加载配置文件。第二种方法则是利用类加载器从资源目录中加载配置文件。
473

被折叠的 条评论
为什么被折叠?



