gpt4 book ai didi

java - slf4j + log4j2 不写入文件

转载 作者:行者123 更新时间:2023-12-02 11:55:14 31 4
gpt4 key购买 nike

美好的一天!我有一个项目,编译为 WAR 文件并在 wildfly-10.1.0-FINAL 上运行。前段时间我配置了日志系统,一切都很好。 2-3 周后,在多次提交后,一位开发人员注意到日志记录仅记录到 wildfly 的 server.log 文件,而不记录到配置的日志。配置的登录为空。再次强调 - 日志配置没有改变。可能是什么?

pom.xml

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.19</version>
<scope>${artefact.scope}</scope>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>2.5</version>
<scope>${artefact.scope}</scope>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.5</version>
<scope>${artefact.scope}</scope>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.5</version>
<scope>${artefact.scope}</scope>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-jcl</artifactId>
<version>2.5</version>
<scope>${artefact.scope}</scope>
</dependency>

jboss-deployment-struct.xml

<deployment>
<dependencies>
<module name="org.jboss.ironjacamar.jdbcadapters" />
<module name="org.postgres" />
</dependencies>
<exclusions>
<module name="org.apache.log4j" />
</exclusions>
<exclude-subsystems>
<subsystem name="logging"/>
</exclude-subsystems>
</deployment>

log4j2.xml

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="trace">
<Properties>

<Property name="log-path">${env:LOG_HOME:-/opt/wildfly-10.1.0.Final/standalone/log/}/admin/</Property>
</Properties>
<Appenders>
<Console name="console-log" target="SYSTEM_OUT">
<PatternLayout pattern="[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %C:%c{1}:%L - %msg%n"/>
</Console>

<RollingFile name="ADMIN" fileName="${log-path}/admin.log"
filePattern="${log-path}/admin-%d{yyyy-MM-dd}.log.gz">
<PatternLayout>
<pattern>[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %C:%c{1}:%L - %msg%n</pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy interval="1" modulate="true"/>
</Policies>
</RollingFile>

</Appenders>
<Loggers>

<Logger name="my.package" level="INFO" additivity="false" includeLocation="true">
<appender-ref ref="ADMIN" level="INFO"/>
<appender-ref ref="console-log" level="DEBUG"/>
</Logger>

</Loggers>

PS。在我的 server.log 中我可以看到这一点 -

15:05:08,007 ERROR [stderr] (ServerService Thread Pool -- 111) SLF4J: Class path contains multiple SLF4J bindings.
15:05:08,007 ERROR [stderr] (ServerService Thread Pool -- 111) SLF4J: Found binding in [vfs:/content/admin-1.0-SNAPSHOT-dev.war/WEB-INF/lib/slf4j-jdk14-1.7.5.jar/org/slf4j/impl/StaticLoggerBinder.class]

15:05:08,008 ERROR [stderr] (ServerService Thread Pool -- 111) SLF4J: Found binding in [vfs:/content/admin-1.0-SNAPSHOT-dev.war/WEB-INF/lib/log4j-slf4j-impl-2.5.jar/org/slf4j/impl/StaticLoggerBinder.class]

15:05:08,008 ERROR [stderr] (ServerService Thread Pool -- 111) SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.

15:05:08,009 ERROR [stderr] (ServerService Thread Pool -- 111) SLF4J: Actual binding is of type [org.slf4j.impl.JDK14LoggerFactory]

问题是 - 文件已创建,但它是空的。有什么想法吗?

谢谢。

最佳答案

您必须解决依赖关系之间的冲突。您可以使用 mvn dependency:tree 来识别依赖项,然后删除或排除它们。

来自服务器日志:StaticLoggerBinder.class从log4j-slf4j和slf4j-jdk14加载,并且它是冲突的。所以你可以排除 slf4j-jdk14 库并尝试。进一步了解multiple binding

<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-jdk14</artifactId>
</exclusion>
</exclusions>

关于java - slf4j + log4j2 不写入文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47653520/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com