Search code examples
javaspring-mvcloggingspring-bootlogback

Logback can't write in console


I am using Logback in my spring boot application.

The problem is logback do not print my logger messages in 'eclipse' console for my both 2 packages dao and web.

The log file is written without any problem , and print my logger messages.

I am the root so probably I should see my logger messages in my console.

  • logger.info("Page X INFO");
  • logger.debug(" Page X Debug ");

Here is my logback.xml file :

<?xml version="1.0" encoding="UTF-8"?>
<configuration>

    <!-- Send debug messages to System.out -->
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <!-- By default, encoders are assigned the type ch.qos.logback.classic.encoder.PatternLayoutEncoder -->
        <encoder>
            <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{5} - %msg%n</pattern>
        </encoder>
    </appender>

    <!-- Send debug messages to a file at "C:/logs/Log.log" -->
    <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>C:/logs/Log.log</file>
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <Pattern>%d{yyyy-MM-dd_HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</Pattern>
        </encoder>

        <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
            <FileNamePattern>C:/logs/Log.%i.log.zip</FileNamePattern>
            <MinIndex>1</MinIndex>
            <MaxIndex>10</MaxIndex>
        </rollingPolicy>

        <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
            <MaxFileSize>10MB</MaxFileSize>
        </triggeringPolicy>
    </appender>

    <logger name="package.web" level="INFO" >
            <appender-ref ref="FILE" />
    </logger>
    <logger name="package.dao" level="DEBUG" >
            <appender-ref ref="FILE" />
    </logger>

    <!-- By default, the level of the root level is set to DEBUG -->
    <root level="DEBUG">
        <appender-ref ref="STDOUT" />
         <appender-ref ref="FILE" />
    </root>
</configuration>

Solution

  • <logger name="package.web" level="INFO" >
            <appender-ref ref="FILE" />
    </logger>
    

    You need to add the console appender.

    <logger name="package.web" level="INFO" >
            <appender-ref ref="FILE" />
            <appender-ref ref="STDOUT" />
    </logger>
    

    UPDATE: I just reread the logback config doku. Actually, those two should inherit both appenders from root. So, what you can try is to not specify any appender-ref on those and see what happens. If no output is written to the file then, neither - then there is something pretty strange. I'd expect that behavior if the additivity flag was set to false. But the default is appender accumulation.