使用 log4j2 在 JBoss Wildfly 中记录错误
我已经实现了 log4j 2.12.4 作为我的日志框架。我正在开发的应用程序由四个 WAR 文件组成。三个 WAR 文件与我的团队直接相关,而另一个团队正在处理第四个 WAR 文件。我在三个 WAR 文件中配置了 log4j2.xml,而第四个文件则配置了 log4j.xml - 表示 log4j 1.x。
我的三个 WAR 文件日志正在写入单个日志文件 (ABC.log) - 我已经定义并希望以这种方式。第四个 WAR 文件的日志正在写入另一个日志文件(XYZ.log)。现在,ABC.log 的日志记录工作正常,但在控制台上,日志模式非常随意。
我使用 Wildfly 9.0.2 作为我的应用程序服务器。在控制台上,日志以如下方式打印:
INFO : stdout - [23:17:05,641] [INFO] ClassA:177 - DateString: 2022-01-01
但实际上,我想将其显示为:
[23:17:05,641] [INFO] ClassA:177 - DateString: 2022-01-01
为什么会出现日志消息开头的附加文本?我的 POM 文件对 log4j2 有以下依赖项:
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.12.4</version>
</dependency>
<dependency>
<groupId>org.jboss.logmanager</groupId>
<artifactId>log4j2-jboss-logmanager</artifactId>
<version>1.0.0.Final</version>
</dependency>
这是我的 log4j2.xml 配置:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="INFO">
<Appenders>
<Console name="LogToConsole" target="SYSTEM_OUT">
<PatternLayout pattern="[%d{HH:mm:ss,SSS}] [%p] %c{1}:%L - %m%n"/>
</Console>
<RollingFile name="LogToRollingFile" fileName="c:/logs/ABC.log"
filePattern="c:/logs/ABC-%d{yyyy-MM-dd}-%i.log.gz">
<PatternLayout>
<Pattern>[%d{HH:mm:ss,SSS}] [%p] %c{1}:%L - %m%n</Pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy />
<SizeBasedTriggeringPolicy size="500 KB"/>
</Policies>
<DefaultRolloverStrategy max="20"/>
</RollingFile>
</Appenders>
<Loggers>
<Logger name="com.example" level="info" additivity="false">
<AppenderRef ref="LogToConsole"/>
<AppenderRef ref="LogToRollingFile"/>
</Logger>
</Logger>
<Logger name="org.jboss" level="error" additivity="false">
<AppenderRef ref="LogToConsole"/>
</Logger>
</Logger>
<Root level="error">
<AppenderRef ref="LogToConsole"/>
</Root>
</Loggers>
</Configuration>
除了控制台上的错误日志格式之外,另一个问题是我的三个 WAR 文件的日志也写入了第四个 WAR 的日志文件中“XYZ.log”并且格式也错误。
我不想将我的 WAR 文件日志写入“XYZ.log”,并且我没有在任何地方配置它,但它正在写入。
我尝试通过以下配置添加 jboss-deployment-struct.xml 但它没有得到修复:
<jboss-deployment-structure>
<deployment>
<exclusions>
<module name="org.apache.commons.logging" />
<module name="org.apache.log4j" />
<module name="org.jboss.logging" />
<module name="org.jboss.logging.jul-to-slf4j-stub" />
<module name="org.jboss.logmanager" />
<module name="org.jboss.logmanager.log4j" />
<module name="org.slf4j" />
<module name="org.slf4j.impl" />
</exclusions>
</deployment>
</jboss-deployment-structure>
请让我知道控制台上错误的日志记录格式可能是什么问题,并将日志写入另一个我没有的日志文件中定义的。任何帮助将非常感激!
I have implemented log4j 2.12.4 as my logging framework. The application on which I am working consists of four WAR files. Three WAR files are directly related to my team while the other team is working on fourth WAR file. I have configured log4j2.xml in three of my WAR files while the fourth one have log4j.xml - means log4j 1.x.
My three WAR files logs are writing to a single log file (ABC.log) - I have defined and want in this way. And the logs of fourth WAR file are writing to another log file (XYZ.log). Now, the logging for ABC.log is working fine but on Console the logs pattern is very haphazard.
I am using Wildfly 9.0.2 as my application server. On console the logs are printing in a way like:
INFO : stdout - [23:17:05,641] [INFO] ClassA:177 - DateString: 2022-01-01
But in actual, I want to display it as:
[23:17:05,641] [INFO] ClassA:177 - DateString: 2022-01-01
Why the additional text in start of the log message is appearing? My POM file have following dependencies for log4j2:
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.12.4</version>
</dependency>
<dependency>
<groupId>org.jboss.logmanager</groupId>
<artifactId>log4j2-jboss-logmanager</artifactId>
<version>1.0.0.Final</version>
</dependency>
And here are my log4j2.xml configurations:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="INFO">
<Appenders>
<Console name="LogToConsole" target="SYSTEM_OUT">
<PatternLayout pattern="[%d{HH:mm:ss,SSS}] [%p] %c{1}:%L - %m%n"/>
</Console>
<RollingFile name="LogToRollingFile" fileName="c:/logs/ABC.log"
filePattern="c:/logs/ABC-%d{yyyy-MM-dd}-%i.log.gz">
<PatternLayout>
<Pattern>[%d{HH:mm:ss,SSS}] [%p] %c{1}:%L - %m%n</Pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy />
<SizeBasedTriggeringPolicy size="500 KB"/>
</Policies>
<DefaultRolloverStrategy max="20"/>
</RollingFile>
</Appenders>
<Loggers>
<Logger name="com.example" level="info" additivity="false">
<AppenderRef ref="LogToConsole"/>
<AppenderRef ref="LogToRollingFile"/>
</Logger>
</Logger>
<Logger name="org.jboss" level="error" additivity="false">
<AppenderRef ref="LogToConsole"/>
</Logger>
</Logger>
<Root level="error">
<AppenderRef ref="LogToConsole"/>
</Root>
</Loggers>
</Configuration>
Besides this wrong logging format on Console, one other issue is that the logs of my three WAR files are writing in the log file of fourth WAR also which is "XYZ.log" and in the wrong format too.
I don't want to get write my WAR files logs into "XYZ.log" and I have not configured that anywhere but it is writing.
I tried by adding jboss-deployment-structure.xml with following configurations but it didn't get fixed:
<jboss-deployment-structure>
<deployment>
<exclusions>
<module name="org.apache.commons.logging" />
<module name="org.apache.log4j" />
<module name="org.jboss.logging" />
<module name="org.jboss.logging.jul-to-slf4j-stub" />
<module name="org.jboss.logmanager" />
<module name="org.jboss.logmanager.log4j" />
<module name="org.slf4j" />
<module name="org.slf4j.impl" />
</exclusions>
</deployment>
</jboss-deployment-structure>
Please let me know what can be the issue of wrong logging format on Console and writing the logs in another log file which I haven't defined. Any help would be really appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论