6

此 logback.xml 正在记录到文件但无法登录到控制台?我希望有人能在这个配置中发现配置错误?这是我的基本记录器配置:

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
...
public static Logger logger = LoggerFactory.getLogger( "JUnit" );
...
logger.info("This comment fails to show in console but it shows in log file");

这是 logback.xml :

<configuration>

  <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
    <!-- encoders are assigned the type
         ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
    <encoder>
      <pattern>%-4relative [%thread] %-5level %logger{35} - %msg %n</pattern>
    </encoder>
  </appender>

  <appender name="FILE" class="ch.qos.logback.core.FileAppender">
    <file>junitOut.log</file>
    <append>false</append>
    <!-- encoders are assigned the type
         ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
    <encoder>
      <pattern>%-4r %-5level %logger{35}: %msg%n</pattern>
    </encoder>
  </appender>

  <root level="DEBUG">
    <appender-ref ref="FILE" />
  </root>

  <!-- We want error logging from this logger to go to an extra appender 
       It still inherits CONSOLE STDOUT from the root logger -->
  <logger name="junitOut" level="INFO">
      <appender-ref ref="STDOUT" />
  </logger>  

</configuration>
4

1 回答 1

7

您没有明确定义名为“JUnit”的记录器,因此记录消息将直接发送到根记录器。根记录器只有一个附加程序,即“FILE”,因此记录消息将仅写入文件。您可以在您的情况下将“STDOUT”附加程序添加到根记录器:

      <root level="DEBUG">
         <appender-ref ref="FILE" />
         <appender-ref ref="STDOUT" />
      </root>
于 2013-02-19T11:18:45.130 回答