【问题标题】:Open-liberty how to setup a log4j2 configuration?Open-liberty 如何设置 log4j2 配置?
【发布时间】:2021-08-25 18:43:35
【问题描述】:

标题说明了一切。如何在我的 Open-liberty 项目中设置 log4j2 配置?我在资源文件夹中添加了我的 log4j2.xml 文件,并在我的 pom.xml 中使用了以下依赖项:

<!-- https://mvnrepository.com/artifact/log4j/log4j -->
<dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>1.2.17</version>
</dependency>

这是我的 server.xml:

<server description="Intake Server">
    <featureManager>
        <feature>servlet-4.0</feature>
        <feature>mpConfig-1.4</feature>
        <feature>cdi-2.0</feature>
    </featureManager>

    <variable name="default.http.port" defaultValue="9080"/>
    <variable name="default.https.port" defaultValue="9443"/>
    <variable name="app.context.root" defaultValue="message"/>


    <httpEndpoint httpPort="${default.http.port}" 
    httpsPort="${default.https.port}" id="defaultHttpEndpoint"  host="*" />

    <library id="log4jConfig">
          <folder dir="/var/log/intake" scanInterval="5s" />
    </library>


    <webApplication id="intake" location="intake.war" contextRoot="${app.context.root}">
        <classloader commonLibraryRef="log4jConfig"/>
    </webApplication>
</server>

【问题讨论】:

  • 你的库不是 log4j2 而是 log4j 1.2.17。尝试最后一个 log4j2 版本:2.17.0

标签: java log4j log4j2 open-liberty


【解决方案1】:

您还需要将 log4j 添加到类路径中,有几种方法可以做到这一点(每种方法都有优缺点,但我建议 #2,除非您要制作的应用程序不止一个使用 log4j,在这种情况下,3 可能是更好的方法):

  1. 将其与您的应用打包(在战争中)
  2. 通过webApplication 下的classloader 属性将其添加到server.xml
  3. 将其放入global shared library folder${shared.config.dir}/lib/global${server.config.dir}/lib/global
  4. 通过 JVM 参数设置它:-Dlog4j.configurationFile=file:/path/to/log4j2.xml

我会提到还有一个 archived repo regarding using log4j with Open Liberty 可能对您有帮助,但请记住它已存档,因此它可能不完整且没有开发支持。

【讨论】:

  • 感谢您的回复!我按照存储库中提供的教程进行操作,但不幸的是它不起作用。我按照您的建议尝试了第二个选项。它找到我的 log4j2.xml 文件但没有实现它和 LogManager.getLogManager().getLogger(CLASS);返回空值。
  • 您能否更新问题以使用您为#2 添加的内容显示您的 server.xml sn-p?
  • 我更新了答案并添加了 server.xml 文件。
  • 我仍然有这个问题。我用 server.xml 文件更新了我的问题,如果您能快速浏览一下,我将不胜感激!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-10-16
  • 1970-01-01
  • 2013-12-20
  • 2014-04-29
  • 1970-01-01
  • 1970-01-01
  • 2018-05-25
相关资源
最近更新 更多