我们在具有log4j 1.x版本的java项目中使用属性配置。客户要求从1.x版本升级到2.16。我将jar文件从log4j 1.2.4替换为log4j 2.16。我唯一的错误就是
进口org.apache.log4j.PropertyConfigurator;
在主要功能中,我使用了PropertyConfigurator.configure(property.getProperty("LOG_PATH"));
PropertyConfigurator是log4j 1.2.4JAR中可用的类。因为我用log4j 2.16JAR替换了它,所以这个PropertyConfigurator类不可用。我的问题是:( 1)我应该怎么做才能消除这个错误。2) log4j 2.16中替代PropertyConfigurator类的备用类是什么?
我们只使用属性文件。不是xml.Please帮我对付这个家伙。
发布于 2021-12-19 16:01:06
请参阅下面的Log4J2文档https://logging.apache.org/log4j/2.0/faq.html#reconfig_from_code
注意:确保将log4j.xml/properties文件重命名为log4j2.xml/properties
示例:
import org.apache.logging.log4j.core.LoggerContext;
public void configureLog4j() throws IOException
{
String path = getLog4jConfigFilePath();
File file = new File(path);
if (file.exists())
{
LoggerContext context = (LoggerContext)LogManager.getContext(false);
context.setConfigLocation(file.toURI());
}else
{
throw new FileNotFoundException(path);
}
}https://stackoverflow.com/questions/70391544
复制相似问题