log4j.PropertyConfigurator运行时错误 说找不到适合的方法来配置(FileInputStream)

huangapple go评论96阅读模式
英文:

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类如下:

  1. public class LoggerHelper {
  2. private static boolean root = false;
  3. public static Logger getLogger(Class clas) {
  4. if(root)
  5. return Logger.getLogger(clas);
  6. /*PropertyConfigurator.configure(ResourceHelper
  7. .getResourcePath("configfile/log4j.properties"));*/
  8. FileInputStream inputStream = null;
  9. try {
  10. inputStream = new FileInputStream(System.getProperty("user.dir")+"\\src\\main\\resources\\configFile\\log4j.properties");
  11. PropertyConfigurator.configure(inputStream);
  12. } catch (FileNotFoundException e) {
  13. // TODO Auto-generated catch block
  14. e.printStackTrace();
  15. }
  16. root = true;
  17. return Logger.getLogger(clas);
  18. }
  19. }
英文:

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

  1. E:\GadleDemoProj\src\main\java\com\hal\brands\helper\Logger\LoggerHelper.java:28: error: no suitable method found for configure(FileInputStream)
  2. PropertyConfigurator.configure(inputStream);
  3. ^
  4. method PropertyConfigurator.configure(Properties) is not applicable
  5. (argument mismatch; FileInputStream cannot be converted to Properties)
  6. method PropertyConfigurator.configure(String) is not applicable
  7. (argument mismatch; FileInputStream cannot be converted to String)
  8. method PropertyConfigurator.configure(URL) is not applicable
  9. (argument mismatch; FileInputStream cannot be converted to URL)

My Logger class is as below :

  1. public class LoggerHelper {
  2. private static boolean root = false;
  3. public static Logger getLogger(Class clas) {
  4. if(root)
  5. return Logger.getLogger(clas);
  6. /*PropertyConfigurator.configure(ResourceHelper
  7. .getResourcePath("configfile/log4j.properties"));*/
  8. FileInputStream inputStream = null;
  9. try {
  10. inputStream = new FileInputStream(System.getProperty("user.dir")+"\\src\\main\\resources\\configFile\\log4j.properties");
  11. PropertyConfigurator.configure(inputStream);
  12. } catch (FileNotFoundException e) {
  13. // TODO Auto-generated catch block
  14. e.printStackTrace();
  15. }
  16. root = true;
  17. return Logger.getLogger(clas);
  18. }
  19. }

答案1

得分: 0

更新代码如下:

  1. public class LoggerHelper {
  2. private static boolean root = false;
  3. public static Logger getLogger(Class clas) {
  4. if (root)
  5. return Logger.getLogger(clas);
  6. /*PropertyConfigurator.configure(ResourceHelper
  7. .getResourcePath("configfile/log4j.properties"));*/
  8. FileInputStream inputStream = null;
  9. try {
  10. inputStream = new FileInputStream(System.getProperty("user.dir")+"\\src\\main\\resources\\configFile\\log4j.properties");
  11. Properties properties = new Properties();
  12. properties.load(inputStream);
  13. PropertyConfigurator.configure(properties);
  14. } catch (FileNotFoundException e) {
  15. // TODO Auto-generated catch block
  16. e.printStackTrace();
  17. }
  18. root = true;
  19. return Logger.getLogger(clas);
  20. }
  21. }
英文:

Update the code to this :

  1. public class LoggerHelper {
  2. private static boolean root = false;
  3. public static Logger getLogger(Class clas) {
  4. if(root)
  5. return Logger.getLogger(clas);
  6. /*PropertyConfigurator.configure(ResourceHelper
  7. .getResourcePath("configfile/log4j.properties"));*/
  8. FileInputStream inputStream = null;
  9. try {
  10. inputStream = new FileInputStream(System.getProperty("user.dir")+"\\src\\main\\resources\\configFile\\log4j.properties");
  11. Properties properties = new Properties();
  12. properties.load(inputStream);
  13. PropertyConfigurator.configure(properties);
  14. } catch (FileNotFoundException e) {
  15. // TODO Auto-generated catch block
  16. e.printStackTrace();
  17. }
  18. root = true;
  19. return Logger.getLogger(clas);
  20. }
  21. }

huangapple
  • 本文由 发表于 2020年9月17日 22:56:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/63940801.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定