Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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>
like image 849
djangofan Avatar asked Feb 17 '13 19:02

djangofan


People also ask

How do I enable DEBUG logs in Logback xml?

debug=true to enable debugging of the logback setup. Unfortunately, there is no way to enable debugging via a System property. You have to use <configuration debug="true"> in the logback. xml .

Where is the Logback xml file?

In a Spring Boot application, you can put the Logback. xml file in the resources folder. If your Logback. xml file is outside the classpath, you need to point to its location using the Logback.


1 Answers

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>
like image 86
Jintian DENG Avatar answered Oct 04 '22 22:10

Jintian DENG