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

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

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);
    }

}

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:

确定