// 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());**
If you want to change some properties in the log4j2.properties file and make it effect immediatly. Here is the method:
private static void changeSomeProperties(String pathToPropertiesFile) {
File log4j2PropertiesFile= new File(pathToPropertiesFile, "log4j2.properties");
Properties properties = new Properties();
try (FileInputStream fileInputStream = new FileInputStream(log4j2PropertiesFile);
BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream)) {
properties.load(bufferedInputStream);
} catch (Exception e) {
//handle exception
}
properties.setProperty("property.logDirectory", "someLogPath");
File updatedLog4j2PropertiesFile = new File(pathToPropertiesFile, "updatedlog4j2.properties");
try (OutputStream output = new FileOutputStream(updatedLog4j2PropertiesFile)) {
properties.store(output, null);
} catch (IOException e) {
//handle exception
}
LoggerContext loggerContext = LoggerContext.getContext(false);
loggerContext.setConfigLocation(updatedLog4j2PropertiesFile.toURI());
loggerContext.reconfigure();
}
No need of PropertyConfigurator in log4j2.
It will automatically read your log4j2.property file.
Just make sure you have below jar included in your pom.
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.17.2</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.17.2</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>2.17.2</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.36</version>
</dependency>
----------------------------------------------------------------------
log4j2.properties
appenders=xyz
appender.xyz.type = Console
appender.xyz.name = myOutput
appender.xyz.layout.type = PatternLayout
appender.xyz.layout.pattern = [%d{yy-MMM-dd HH:mm:ss:S}] [%p] [%c{1}:%L] - %m%n
rootLogger.level = info
rootLogger.appenderRefs = abc
rootLogger.appenderRef.abc.ref = myOutputstrong text
----------------------------------------------------------------------
MyClass.java
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class MyClass {
public static void main(String[] args) {
Logger logger = LoggerFactory.getLogger(MyClass.class);
logger.info("----start-------");
}
}
----------------------------------------------------------------------
Output:
[22-Jul-22 17:11:09:128] [INFO] [MyClass:24] - ----start-------
重新设定
如果您想使用特定的配置文件在代码中重新配置log4j2,请使用下面的代码。
// 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());
7条答案
按热度按时间eiee3dmh1#
也许这个能帮到你?
nhaq1z212#
我的解决方案只是简单地按照Log4J站点https://logging.apache.org/log4j/2.x/manual/migration.html中的说明进行操作,也就是说,我将
与
即使使用类
org.apache.log4j.PropertyConfigurator
,也没有编译错误。这比尝试迁移到
正如@Christian所指出的那样,它不能替代PropertyConfigurator类。不可否认,如果不迁移,我将无法从Log4j 2的新功能中获益。
ki0zmccv3#
Log4j 2目前支持XML、JSON或YAML的配置。虽然不久的将来可能也会支持属性文件,但语法肯定会与Log4j 1不同。
knsnq2tg4#
下面我对同一个问题的解决方案:
网页.xml
日志记录器帮助程序.java
tyg4sfes5#
yhived7q6#
任何log4j 2代码前的第一行- configure sys prop到您的属性配置工厂
第一个
或扩展属性配置工厂
11dmarpk7#
如果您想使用特定的配置文件在代码中重新配置log4j2,请使用下面的代码。