How to force spring to do not zip log files, instead append date at end of log file

Viewed 5072

Currently I'm using spring boot logs and I'm configuring it through property file below are the sample logging property

spring.main.banner-mode=off
logging.level.root= INFO,ERROR,DEBUG
logging.level.org.springframework.web= ERROR
logging.level.com.concretepage= DEBUG 
logging.pattern.console=
logging.file = D://logTest.log
logging.file.max-size=100MB
spring.output.ansi.enabled=ALWAYS

The problem is that log file backup format is in .gz format like logTest.log.2019-06-14.0.gz

How do I exclude default zipping ?

I don't want to hard wire configuration in xml file and put it inside resource folder. I can only put rolling appender configuration xml file, but I want to make logging file path in property file, So I can dynamically set it for different environment.

Is there any way to achieve this configuration?

2 Answers

As an alternative to @simon-martinelli's correct answer, if you did not wish to use a custom logback-spring.xml file, the Spring configuration parameter logging.pattern.rolling-file-name can be set in your application.properties or application.yml file.

For example, to disable the compression used, remove the .gz suffix from the default file name pattern (${LOG_FILE}.%d{yyyy-MM-dd}.%i.gz as per https://docs.spring.io/spring-boot/docs/current/reference/html/spring-boot-features.html#boot-features-custom-log-configuration).

This would require the addition of the following element to the application.yml file:

logging:
  pattern:
    rolling-file-name: "${LOG_FILE}.%d{yyyy-MM-dd}.%i"

Or if you are using application.properties, it would be:

logging.pattern.rolling-file-name = ${LOG_FILE}.%d{yyyy-MM-dd}.%i

Create a logback-spring.xml file in src/main/resources

With this content

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <include resource="org/springframework/boot/logging/logback/base.xml"/>

<appender name="FILE"
        class="ch.qos.logback.core.rolling.RollingFileAppender">
        <encoder>
            <pattern>${FILE_LOG_PATTERN}</pattern>
        </encoder>
        <file>${LOG_FILE}</file>
        <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
            <cleanHistoryOnStart>${LOG_FILE_CLEAN_HISTORY_ON_START:-false}</cleanHistoryOnStart>
            <fileNamePattern>${LOG_FILE}.%d{yyyy-MM-dd}.%i.log</fileNamePattern>
            <maxFileSize>${LOG_FILE_MAX_SIZE:-10MB}</maxFileSize>
            <maxHistory>${LOG_FILE_MAX_HISTORY:-7}</maxHistory>
            <totalSizeCap>${LOG_FILE_TOTAL_SIZE_CAP:-0}</totalSizeCap>
        </rollingPolicy>
    </appender>

</configuration>

If the fileNamePattern don't end with gz (or any other compression format) logback will not compress the files.

Related