Search code examples
javaspring-bootlogback

logback doesn't work in Spring boot


I try to use logback in Spring boot with below config,

I tried to sperate the log as two types: system log file and business log file,

but the log only show on Console,

no log file generated, anybody can help on this?

Is there necessary to do some config in spring boot config file?

    <configuration>
        <include resource="org/springframework/boot/logging/logback/base.xml"/>

        <appender name="ROOT_APPENDER" class="ch.qos.logback.core.rolling.RollingFileAppender">

            <file>logs/sif-interface-sys.log</file>

            <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">

                <fileNamePattern>${LOG_DIR}/${SYSTEM_NAME}/system.%d{yyyy-MM-dd}.%i.log</fileNamePattern>

                <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">

                    <maxFileSize>500MB</maxFileSize>
                </timeBasedFileNamingAndTriggeringPolicy>
            </rollingPolicy>
            <encoder>
                <pattern>%-20(%d{yyy-MM-dd HH:mm:ss.SSS} [%X{requestId}]) %-5level - %logger{80} - %msg%n
                </pattern>
            </encoder>
        </appender>
        <appender name="BUSINESS_APPENDER" class="ch.qos.logback.core.rolling.RollingFileAppender">

            <file>logs/sif-interface-business.log</file>

            <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">

                <fileNamePattern>${LOG_DIR}/${SYSTEM_NAME}/business.%d{yyyy-MM-dd}.%i.log</fileNamePattern>

                <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">

                    <maxFileSize>500MB</maxFileSize>
                </timeBasedFileNamingAndTriggeringPolicy>
            </rollingPolicy>

            <encoder>
                <pattern>%-20(%d{yyy-MM-dd HH:mm:ss.SSS} [%X{requestId}]) %-5level - %logger{80} - %msg%n
                </pattern>
            </encoder>
        </appender>

        <logger name="org.springframework.web" level="INFO">
            <appender-ref ref="ROOT_APPENDER" />
        </logger>

        <logger name="{project-package}" level="INFO">
            <appender-ref ref="BUSINESS_APPENDER" />
        </logger>

    </configuration>

Solution

  • I just work it out. As I use Spring boot, so it has to set a config like this, these two log files will generated

    logging.config: classpath:logback-spring.xml
    

    BTW, it can also display colorful log with below config:

    spring.output.enabled: DETECT