我在Log4j的构建阶段遇到以下错误。它说没有找到针对configure(FileInputSteam)的方法。
以下是错误的完整说明。
任务:compileJava失败
E:\GadleDemoProj\src\main\java\com\inspire\brands\helper\Logger\LoggerHelper.java:28: error: no suitable method found for configure(FileInputStream)
PropertyConfigurator.configure(inputStream);
^
method PropertyConfigurator.configure(Properties) is not applicable
(argument mismatch; FileInputStream cannot be converted to Properties)
method PropertyConfigurator.configure(String) is not applicable
(argument mismatch; FileInputStream cannot be converted to String)
method PropertyConfigurator.configure(URL) is not applicable
(argument mismatch; FileInputStream cannot be converted to URL)
我的记录器类如下: public class LoggerHelper {
private static boolean root = false;
public static Logger getLogger(Class clas) {
if(root)
return Logger.getLogger(clas);
/*PropertyConfigurator.configure(ResourceHelper
.getResourcePath("configfile/log4j.properties"));*/
FileInputStream inputStream = null;
try {
inputStream = new FileInputStream(System.getProperty("user.dir")+"\\src\\main\\resources\\configFile\\log4j.properties");
PropertyConfigurator.configure(inputStream);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
root = true;
return Logger.getLogger(clas);
}
}
最佳答案
将代码更新为此:
public class LoggerHelper {
private static boolean root = false;
public static Logger getLogger(Class clas) {
if(root)
return Logger.getLogger(clas);
/*PropertyConfigurator.configure(ResourceHelper
.getResourcePath("configfile/log4j.properties"));*/
FileInputStream inputStream = null;
try {
inputStream = new FileInputStream(System.getProperty("user.dir")+"\\src\\main\\resources\\configFile\\log4j.properties");
Properties properties = new Properties();
properties.load(inputStream);
PropertyConfigurator.configure(properties);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
root = true;
return Logger.getLogger(clas);
}
}
关于java - 运行时发生log4j.PropertyConfigurator错误说找不到适合configure(FileInputStream)的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63940801/