djangofan
djangofan

Reputation: 29669

This logback.xml is logging to file but fails to log to console?

This logback.xml is logging to file but fails to log to console? I am hoping someone can spot the configuration error in this config? Here is my basic logger config:

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");

And here is the 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>

Upvotes: 6

Views: 8464

Answers (1)

Jintian DENG
Jintian DENG

Reputation: 3202

You don't explicitly define a logger named "JUnit", so the logging message will go to root logger directly. The root logger has just one appender, i.e. "FILE", so the logging message will be written to the file only. You can add the "STDOUT" appender to the root logger in your case:

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

Upvotes: 7

Related Questions