java - 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>
解决方案
您还需要将 log4j 添加到类路径中,有几种方法可以做到这一点(每种方法都有优缺点,但我建议 #2,除非您有多个应用程序将使用 log4j ,在这种情况下,3 可能是更好的方法):
- 将其与您的应用程序打包(在战争中)
- 通过webApplication下的classloader属性将其添加到 server.xml
- 将其放在全局共享库文件夹中:
${shared.config.dir}/lib/global
或${server.config.dir}/lib/global
- 通过 JVM 参数设置它:
-Dlog4j.configurationFile=file:/path/to/log4j2.xml
我会提到还有一个关于将 log4j 与 Open Liberty 一起使用的存档存储库,您可能会发现它很有帮助,但请记住它已存档,因此它可能不完整且没有开发支持。
推荐阅读
- python - Python boto3 检查区域内的有效存储桶
- python - 如何编写一个函数来计算 5 个不对称和传递的元素的可能二元关系的数量
- c++ - gltf,矩阵是否要转置?
- r - 使用 R ggplot 控制条形图中的条形顺序
- npm - npm 不工作 - 安装 hdwallet-provider 时“读取 ECONNRESET”
- asp.net-web-api - 向经典 ASP.NET Web 窗体应用和 Web API 应用添加共享身份验证
- swiftui - SwiftUI GKLeaderboard loadEntries
- sql-server - TSQL唯一过滤索引不会阻止插入
- node.js - Vue/Node 错误:规则只能有一个资源源
- javascript - JQuery 链式承诺数组