0
votes

I have created a test project with Spring Boot to learn about about using the logback-spring.xml file. I want to use Spring's default setting for writing to console so I am using the following line

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

And I also want to log to a file on a rolling basis and keep a maximum number of log files on the system. Writing to console is working as expected. However no logs are written to the log file. The folder named "logs" gets created and the file "logfile.log" also gets created. But nothing gets logged to it.

Below is the fill logback-spring.xml file

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

<configuration>
    <include resource="org/springframework/boot/logging/logback/base.xml" />
    <property name="LOG_PATH" value="logs" />
    <property name="LOG_ARCHIVE" value="${LOG_PATH}/archive" />

    <appender name="File-Appender" class="ch.qos.logback.core.FileAppender">
        <file>${LOG_PATH}/logfile.log</file>
        <encoder>
            <pattern>%d{dd-MM-yyyy HH:mm:ss.SSS} %magenta([%thread]) %highlight(%-5level) %logger{36}.%M - %msg%n</pattern>
            <outputPatternAsHeader>false</outputPatternAsHeader>
        </encoder>
    </appender>

    <logger name="test" level="DEBUG" />
</configuration>

and below is the TestApplication.java file which is part of the test package

@SpringBootApplication
public class TestApplication {

    private static final Logger logger = LoggerFactory.getLogger(TestApplication.class);

    public static void main(String[] args) {
        SpringApplication.run(TestApplication.class, args);
        logger.trace("Trace Message!");
        logger.debug("Debug Message!");
        logger.info("Info Message!");
        logger.warn("Warn Message!");
        logger.error("Error Message!");
    }

}

Why is nothing being logged to the file?

1

1 Answers

2
votes

I think there are a few issues.

First, remove the line <logger name="test" level="DEBUG" /> . This sets up a logger for classes under the package test but defines no appender, so nothing is logged.

Once that's gone, add

<root level="DEBUG">
    <appender-ref ref="File-Appender"/>
</root>

This will configure the root logger (which all loggers inherit) on debug level and to output all the logs to the File-Appender.

Also, I cannot recall if logback creates missing directories, so you might need to ensure the logs directory does exist before starting the application.