Deepesh Rathore
Deepesh Rathore

Reputation: 351

Logging pattern in spring boot application

How do I set the logging file pattern to something like server.log.2017-12-22.gz?

As of now in my application.properties file, I have set the logging pattern to:

logging.pattern.file= "%d{yyyy-MM-dd } [%thread] %-5level %logger{36} - %msg%n"

logging.file=/var/opt/VS_Logs/server.log

But I need to store the files in the following format: server.log.2017-12-22.gz

Upvotes: 0

Views: 12700

Answers (1)

Dimitri Mestdagh
Dimitri Mestdagh

Reputation: 44745

As soon as you want custom rolling and triggering policies, you can no longer rely on Spring boot's logging configuration, and you have to use the vendor specific logging configuration. Here is an example using Logback and a TimeBasedRollingPolicy:

<configuration>
    <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>server.log</file>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>server.log.%d{yyyy-MM-dd}.gz</fileNamePattern>
            <maxHistory>30</maxHistory>
            <totalSizeCap>3GB</totalSizeCap>
        </rollingPolicy>
        <encoder>
            <pattern>%d{yyyy-MM-dd } [%thread] %-5level %logger{36} - %msg%n</pattern>
        </encoder>
    </appender>

    <root level="debug">
        <appender-ref ref="FILE" />
    </root>
</configuration>

Logback will automatically gzip it when you use the .gz extension. If you save this file as logback.xml and put it on your classpath, Spring boot will automatically detect it, otherwise, you can use the logging.config property:

logging.config=classpath:logback.xml

Upvotes: 4

Related Questions