Slf4j记录未记录到wildfly中的文件

时间:2016-08-30 20:47:30

标签: logging log4j slf4j wildfly-10

我跟随父pom

            <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-api</artifactId>
                <version>1.7.9</version>
            </dependency>
            <dependency>
                <groupId>ch.qos.logback</groupId>
                <artifactId>logback-classic</artifactId>
                <version>1.1.7</version>
            </dependency>
            <dependency>
                <groupId>ch.qos.logback</groupId>
                <artifactId>logback-core</artifactId>
                <version>1.1.7</version>
            </dependency>

services模块中的resources文件夹具有以下logback.xml

<?xml version="1.0" encoding="UTF-8"?>
<configuration debug="true" scan="true" scanPeriod="60 seconds">

    <property name="MY_HOME" value="C:\Users\" />

<!--
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <File>${MY_HOME}/logs/mylog.log</File>
-->
 <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
    <layout class="ch.qos.logback.classic.PatternLayout">
      <Pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</Pattern>
    </layout>
  </appender>

  <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <File>C:/Users/logs/abc.log</File>
    <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
      <FileNamePattern>C:/Users/logs/my.%d{yyyy-MM-dd}.log</FileNamePattern>
      <MaxHistory>30</MaxHistory>
    </rollingPolicy>

    <layout class="ch.qos.logback.classic.PatternLayout">
      <Pattern>%d{HH:mm:ss.SSS} [%thread] %-5level - %msg%n</Pattern>
    </layout>
  </appender> 

  <root level="TRACE">
    <appender-ref ref="FILE"/>
  </root>
</configuration>

在课堂上它被称为:

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

...
...
  private static final Logger sl4jLogger = LoggerFactory.getLogger(ServiceBean.class);
    ...
    ...

        sl4jLogger.debug("debug : show up in logger");
        sl4jLogger.info("info : show up in logger");
        sl4jLogger.error("error : show up in logger");
        sl4jLogger.trace("trace : show up in logger");
        sl4jLogger.warn("warn : show up in logger");

控制台上只显示以下内容,但没有在任何地方创建日志文件。

12:33:07,262 INFO  [ServiceBean] (default task-63) info : show up in logger 

12:33:07,262 ERROR [ServiceBean] (default task-63) error : show up in logger

12:33:07,263 WARN  [ServiceBean] (default task-63) warn : show up in logger

AppServer jboss wildfly - 10.0.0

我还在webapp / META-INF /文件夹中添加/尝试了几个不同版本的jboss-deployment-structure.xml。 但这些都没有奏效。

<?xml version="1.0" encoding="UTF-8"?>
<jboss-deployment-structure>
    <deployment>
        <dependencies>
            <module name="org.slf4j" />
        </dependencies>
    </deployment>
</jboss-deployment-structure>

<!--<?xml version="1.0" encoding="UTF-8"?>
<jboss-deployment-structure xmlns="urn:jboss:deployment-structure:1.2">
    <deployment>
        <exclude-subsystems>
            <subsystem name="logging" />
        </exclude-subsystems>
    </deployment>
</jboss-deployment-structure>-->


<!--
<?xml version="1.0" encoding="UTF-8"?>
<jboss-deployment-structure>
    <deployment>
        <dependencies>
            <module name="org.slf4j" />
        </dependencies>
    </deployment>
</jboss-deployment-structure>
-->

<!--
<?xml version="1.0" encoding="UTF-8"?>
<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>
-->

    <!--
    <jboss-deployment-structure>
        <deployment>
            <exclusions>
                <module name="org.slf4j" />
                <module name="org.slf4j.impl" />
                <module name="org.slf4j.jcl-over-slf4j" />
                <module name="org.slf4j.ext" />
                <module name="org.apache.log4j" />
            </exclusions>
        </deployment>
    </jboss-deployment-structure>-->

该项目由几个差异模块组成。几场战争,jar,ejb。所有这些都包裹在耳朵里。这是问题吗?

我错过了什么?

1 个答案:

答案 0 :(得分:0)

slf4j记录器正在工作,它们只是没有绑定到您希望它们绑定到的日志管理器。当您包含slf4j依赖项时,您正在部署的可能是使用绑定到JBoss Log Manager的服务器提供的slf4j模块。您看到的日志消息由logging subsystem控制。

最佳选择是使用日志记录子系统配置日志记录。如果您使用Web控制台或CLI,您将能够在运行时进行更改,而无需重新启动服务器或重新部署应用程序。

如果您确实希望在部署中使用logback,最简单的方法是从部署中exclude the logging subsystem。然后将使用您提供的依赖项,并且slf4j应该绑定到logback。

另一个选项是将日志子系统上的add-logging-api-dependencies属性更改为false。这将阻止将日志记录API添加到已部署的所有部署中。

/subsystem=logging:write-attribute(name=add-logging-api-dependencies, value=false)