java - 运行时 log4j.PropertyConfigurator 错误说找不到合适的方法用于配置(FileInputStream)
问题描述
我在 Log4j 的构建阶段收到以下错误。它说没有为configure(FileInputSteam)找到这样的方法。
以下是错误的完整上下文。
任务:编译Java失败
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)
我的 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);
}
}
解决方案
将代码更新为此:
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);
}
}
推荐阅读
- angular - 对 ngrx 存储的可观察订阅未在状态更改时更新
- html - Angular8如何以良好的实践方式在子项目资产文件夹中加载资源?
- crystal-lang - 有可能以某种方式使用 forward_missing_to 来获取/设置哈希值吗?
- java - 应用程序正在成功发布数据,但我想检索令牌
- android - UI 测试和 E2E 测试有什么区别?以及各有什么好处?
- ios - UITableView 被释放,而键值观察者仍向其注册,仅在 iOS9、iOS10 中崩溃
- json - 尝试通过 API Gateway 将 Log 插入 Cloudwatch 时出现 SerializationException
- loopbackjs - 如何为 LoopbackJS 应用程序中的所有模型定义全局应用的 ACL 策略
- spring - 从控制器运行弹簧批处理作业
- python - python中效率低下的代码分配了很多内存