0

I have created a common log back-common.xml. I want to use this file in another file - logback.spring.xml. Please help me with how I can use this efficiently.

As of now, the application is starting but logs are not printed in the console, and logs are not populated to log file. Please help. Don't mark this as duplicate, because I have tried almost everything and I have invested 2 days in this. Other questions related to the same do not have a valid answer attached.

logback-spring.xml

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <property resource ="application.yml"/>
    <springProperty name="NAME" source="spring.application.name" />
    <include file="logback-common.xml"/>
</configuration>

logback-common.xml

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <include resource="org/springframework/boot/logging/logback/defaults.xml"/>
    <include resource="org/springframework/boot/logging/logback/base.xml"/>
    <include resource="org/springframework/boot/logging/logback/console-appender.xml"/>
    <property name="LOGS" value="./logs" />
    <appender name="Console"
        class="ch.qos.logback.core.ConsoleAppender">
        <layout class="ch.qos.logback.classic.PatternLayout">
            <Pattern>
                %black(%d{ISO8601}) %highlight(%-5level) [%blue(%t)] %yellow(%C{1.}): %msg%n%throwable
            </Pattern>
        </layout>
    </appender>

    <appender name="RollingFile"
        class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>${LOGS}/${NAME}.log</file>
        <encoder
            class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <Pattern>%d %p %C{1.} [%t] %m%n</Pattern>
        </encoder>

        <rollingPolicy
            class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <!-- rollover daily and when the file reaches 10 MegaBytes -->
            <fileNamePattern>${LOGS}/archived/spring-boot-logger-%d{yyyy-MM-dd}.%i.log
            </fileNamePattern>
            <timeBasedFileNamingAndTriggeringPolicy
                class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                <maxFileSize>10MB</maxFileSize>
            </timeBasedFileNamingAndTriggeringPolicy>
        </rollingPolicy>
    </appender>
    
    <!-- LOG everything at INFO level -->
    <root level="info">
        <appender-ref ref="RollingFile" />
        <appender-ref ref="Console" />
    </root>

    <!-- LOG "com.baeldung*" at TRACE level -->
    <logger name="com.ms" level="trace" additivity="false">
        <appender-ref ref="RollingFile" />
        <appender-ref ref="Console" />
    </logger>
    
    <logger name="org.springframework.core.env.PropertySourcesPropertyResolver" level="trace" additivity="true">
    </logger>

</configuration>

application.yml

spring:
  application:
    name: Logbacking-service

enter image description here

4

1 Answer 1

3

You need to use below to "resource" instead of "file" for your child configuration ( as it is present in classpath)

<include resource="logback-common.xml"/>

You would see information printed on console when logback is able to find and load all the configuration file as logback gets bootstrapped at very initial stage.

Update:- Regarding the error you shared - you also need to use included tag in your child configuration file instead of configuration tag. Tested below configurations and it's working fine for me.

logback-spring.xml

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <property resource ="application.yml"/>
    <springProperty name="NAME" source="spring.application.name" />
    <include resource="logback-common.xml"/>
</configuration>

And the child configuration as

logback-common.xml

<?xml version="1.0" encoding="UTF-8"?>
<included>
    <include resource="org/springframework/boot/logging/logback/defaults.xml"/>
    <include resource="org/springframework/boot/logging/logback/base.xml"/>
    <include resource="org/springframework/boot/logging/logback/console-appender.xml"/>
    <property name="LOGS" value="./logs" />
    <appender name="Console"
        class="ch.qos.logback.core.ConsoleAppender">
        <layout class="ch.qos.logback.classic.PatternLayout">
            <Pattern>
                %black(%d{ISO8601}) %highlight(%-5level) [%blue(%t)] %yellow(%C{1.}): %msg%n%throwable
            </Pattern>
        </layout>
    </appender>

    <appender name="RollingFile"
        class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>${LOGS}/${NAME}.log</file>
        <encoder
            class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <Pattern>%d %p %C{1.} [%t] %m%n</Pattern>
        </encoder>

        <rollingPolicy
            class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <!-- rollover daily and when the file reaches 10 MegaBytes -->
            <fileNamePattern>${LOGS}/archived/spring-boot-logger-%d{yyyy-MM-dd}.%i.log
            </fileNamePattern>
            <timeBasedFileNamingAndTriggeringPolicy
                class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                <maxFileSize>10MB</maxFileSize>
            </timeBasedFileNamingAndTriggeringPolicy>
        </rollingPolicy>
    </appender>
    
    <!-- LOG everything at INFO level -->
    <root level="info">
        <appender-ref ref="RollingFile" />
        <appender-ref ref="Console" />
    </root>

    
    <logger name="com.ms" level="trace" additivity="false">
        <appender-ref ref="RollingFile" />
        <appender-ref ref="Console" />
    </logger>
    
    <logger name="org.springframework.core.env.PropertySourcesPropertyResolver" level="trace" additivity="true">
    </logger>

</included>

enter image description here

Sign up to request clarification or add additional context in comments.

5 Comments

Spring Boot searches the classpath for alternative file locations (cf. documentation), so logback-spring.xml is correct.
@Piotr thansk for pointing that out. I have edited the answer.
Thankyou Shailendra :) grateful. On changing it to resource, i get this error---- Logging system failed to initialize using configuration from 'null' java.lang.IllegalStateException: Logback configuration error detected: ERROR in ch.qos.logback.core.joran.spi.Interpreter@2:16 - no applicable action for [configuration], current ElementPath is [[configuration][configuration]] ERROR in ch.qos.logback.core.joran.spi.Interpreter@3:77 - no applicable action for [include], current ElementPath is [[configuration][configuration][include]]
@Lisbon - regarding the error please see the update in the answer.
Thankyou sir so much :) understood now

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.