首页 > 解决方案 > Open-liberty 如何设置 log4j2 配置?

问题描述

标题说明了一切。如何在我的 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>

标签: javalog4jlog4j2open-liberty

解决方案


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

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

我会提到还有一个关于将 log4j 与 Open Liberty 一起使用的存档存储库,您可能会发现它很有帮助,但请记住它已存档,因此它可能不完整且没有开发支持。


推荐阅读