log4j2中的PropertyConfigurator[英] PropertyConfigurator in log4j2

本文是小编为大家收集整理的关于log4j2中的PropertyConfigurator的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我将log4j 1.2.8迁移到log4j 2.3.一切正常,除此之外,我找不到 propertyConfigurator .

是否还有其他类可以照顾属性configurator之前的工作?

推荐答案

也许这可以帮助您?

如何使用特定配置文件重新配置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());**

其他推荐答案

我的解决方案仅包括按照log4j站点中的说明 https://logging.apache.org/log4j/2.x/manual/migration.html ,也就是说,我更换了

<dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>1.2.17</version>
</dependency>

 <dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-1.2-api</artifactId>
    <version>2.14.1</version>
 </dependency>

即使使用类org.apache.log4j.PropertyConfigurator.

也没有编译错误

这比尝试迁移到

要容易
<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-core</artifactId>
    <version>2.14.1</version>
</dependency>

正如@christian指出的那样,没有替换class propertyconfigurator.诚然,通过不迁移,我将不会从log4j 2的新功能中受益.

其他推荐答案

log4j 2当前支持XML,JSON或YAML的配置.尽管在不久的将来也可以支持属性文件,但语法肯定会与log4j 1不同.

.

本文地址:https://www.itbaoku.cn/post/1574881.html

问题描述

I'm migrating log4j 1.2.8 to log4j 2.3. Everything works fine, beside that I'm not finding any any alternative for the PropertyConfigurator.

Is there another class to take care of what the PropertyConfigurator did before?

推荐答案

Maybe this can help you?

How do I reconfigure log4j2 in code with a specific configuration file? See the below example. Be aware that this LoggerContext class is not part of the public API so your code may break with any minor release.

// 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());**

其他推荐答案

My solution consisted simply in following the instructions in the Log4J site https://logging.apache.org/log4j/2.x/manual/migration.html, that is, I replaced

<dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>1.2.17</version>
</dependency>

with

 <dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-1.2-api</artifactId>
    <version>2.14.1</version>
 </dependency>

No compilation error, even with class org.apache.log4j.PropertyConfigurator.

This was easier than attempting to migrate to

<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-core</artifactId>
    <version>2.14.1</version>
</dependency>

which, as pointed out by @Christian, has no replacement for class PropertyConfigurator. Admittedly, by not migrating I won't benefit from the new capabilities of Log4j 2.

其他推荐答案

Log4j 2 currently supports configuration with XML, JSON or YAML. While properties files may also be supported in the near future the syntax will certainly be different from Log4j 1.