slf4j + log4j2 不要写入文件

slf4j + log4j2 dond write to file

美好的一天!我有一个项目,在 wildfly-10.1.0-FINAL 上编译为 WAR 文件和 运行。前段时间我配置了日志系统,一切都很好。 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-部署-structure.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>