英文:
log4j.PropertyConfigurator error during runtime Says no suitable method found for configure(FileInputStream)
问题
我在构建Log4j时遇到了以下错误。它说在configure(FileInputSteam)中找不到此类方法。
下面是完整的错误上下文。
Task :compileJava FAILED
E:\GadleDemoProj\src\main\java\com\hal\brands\helper\Logger\LoggerHelper.java:28: error: 找不到适用的方法 configure(FileInputStream)
PropertyConfigurator.configure(inputStream);
^
方法 PropertyConfigurator.configure(Properties) 不适用
(参数不匹配;无法将FileInputStream转换为Properties)
方法 PropertyConfigurator.configure(String) 不适用
(参数不匹配;无法将FileInputStream转换为String)
方法 PropertyConfigurator.configure(URL) 不适用
(参数不匹配;无法将FileInputStream转换为URL)
我的Logger类如下:
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);
}
}
英文:
I am getting the below error during build phase for Log4j. It say no such method found for configure(FileInputSteam).
Below is full contex of error.
Task :compileJava FAILED
E:\GadleDemoProj\src\main\java\com\hal\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)
My Logger class is as below :
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);
}
}
答案1
得分: 0
更新代码如下:
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);
}
}
英文:
Update the code to this :
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);
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论