我正在将log4j 1.2.8迁移到log4j 2.3。一切正常,除此之外,我没有找到任何替代PropertyConfigurator。log4j2 PropertyConfigurator
是否有另一个类来照顾PropertyConfigurator之前做过的事情?
我正在将log4j 1.2.8迁移到log4j 2.3。一切正常,除此之外,我没有找到任何替代PropertyConfigurator。log4j2 PropertyConfigurator
是否有另一个类来照顾PropertyConfigurator之前做过的事情?
Log4j 2当前支持使用XML,JSON或YAML进行配置。尽管在不久的将来可能会支持属性文件,但语法肯定会与Log4j 1不同。
也许这可以帮助您?
如何在具有特定配置文件的代码中重新配置log4j2? 请看下面的例子。请注意,此LoggerContext类不属于公共API,因此您的代码可能会与任何次要版本发生冲突。
// import org.apache.logging.log4j.core.LoggerContext;
LoggerContext context = (org.apache.logging.log4j.core.LoggerContext) LogManager.getContext(false);
File file = new File("path/to/a/different/log4j2.xml");
// this will force a reconfiguration
context.setConfigLocation(file.toURI());**
这里我解决了同样的问题:
的web.xml
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<context-param>
<param-name>isLog4jContextSelectorNamed</param-name>
<param-value>false</param-value>
</context-param>
<!-- ... and so on -->
</web-app>
LoggerHelper.java
import java.io.File;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.core.LoggerContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public final class LoggerHelper {
public static Logger INSTANCE;
public static void initialize() {
String fileName = "<PATH>/log4j.xml";
LoggerContext context = (LoggerContext)LogManager.getContext(false);
context.setConfigLocation(new File(fileName).toURI());
INSTANCE = LoggerFactory.getLogger(LoggerHelper.class);
String logMessage = String.format("Initialized (%s).", fileName);
System.out.println(logMessage);
INSTANCE.info(logMessage);
}
}