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

2

5

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?

Ilana answered 14/6, 2019 at 10:40 Comment(0)
F
3

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.

Frow answered 14/6, 2019 at 12:31 Comment(1)
Thanks @Simon. It worked. I just need to do minor correction for property file. I have to remove this empty property logging.pattern.console=, And also need to remove <file>${LOG_FILE}</file> as I have already mentioned log file in property.Ilana
P
9

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
Parthenogenesis answered 20/12, 2019 at 5:1 Comment(2)
Yourapplication.properties version works fine. You should propably also add .log to the file like: ${LOG_FILE}.%d{yyyy-MM-dd}.%i.logLunn
As of Spring Boot 2.4+, it is now logging.logback.rollingpolicy.file-name-pattern. github.com/spring-projects/spring-boot/issues/…Clarke
F
3

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.

Frow answered 14/6, 2019 at 12:31 Comment(1)
Thanks @Simon. It worked. I just need to do minor correction for property file. I have to remove this empty property logging.pattern.console=, And also need to remove <file>${LOG_FILE}</file> as I have already mentioned log file in property.Ilana

© 2022 - 2024 — McMap. All rights reserved.