user1884155
user1884155

Reputation: 3736

Double log files in spring boot application

I have a spring boot application, called service1, which uses the following hierarchy of property files to configure the logs:

bootstrap.yml has:

application.yml has:

The log framework used is Slf4j, which is injected into every class by using Lombok's @Sfl4j annotation. spring boot claims to have default support for this, which it does, but what I see in /app/logs when the application starts are two logfiles:

I used the following declaration in my pom file to get spring boot, and I suspect perhaps some default logback stuff is also creating a logfile in addition to the Sfl4j I declared. Is there any way I can have only one logfile, with the name I specifiede and the correct loglevel? Perhaps I need to exclude a dependency?

            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-parent</artifactId>
                <version>${spring.boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring.cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

Upvotes: 5

Views: 3696

Answers (1)

Riz
Riz

Reputation: 1065

add the folowing to properties file

logging.config=logback.xml

Then create a logback.xml file. Here you can place any config you like, for example:

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

<!-- Logging to system file -->
<appender name="system-file" class="ch.qos.logback.core.rolling.RollingFileAppender">

    <!-- output filename -->
    <file>logs/app.log</file>


    <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
    <fileNamePattern>logs/app_%d{yyyy-MM-dd}.%i.log</fileNamePattern>
    <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
        <maxFileSize>500MB</maxFileSize>
    </timeBasedFileNamingAndTriggeringPolicy>
    </rollingPolicy>
    <encoder>
    <pattern>%d{dd-MM-yyyy HH:mm:ss} - %c [%t] %-5p - %m%n</pattern>
    </encoder>
</appender>


<!-- overide levels for specific loggers-->
<logger name="com.myproject.api" level="TRACE" />


<root level="INFO">
    <appender-ref ref="system-file"/>
</root>
</configuration>

Upvotes: 1

Related Questions