简体   繁体   中英

How to configure rolling file appender within Spring Boot's application.yml

Is is possible to configure a daily file appender within the application.yml of a Spring Boot application?

ie filenamePattern: myfile.%d{yyyy-MM-dd-HH-mm-ss}.log

I have configuration such as the following in my application.yml file.

logging:

   file: /mypath/myfile.log

   level:
     mypackage: INFO

Thanks

The default file appender is size based (10MB).

In your logback.xml just configure a TimeBasedRollingPolicy as described here

Ie something like:

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

  <appender name="ROLLIN" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <file>${LOG_FILE}</file>
    <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">

        <!-- daily rollover -->
        <fileNamePattern>${LOG_FILE}.%d{yyyy-MM-dd}.log</fileNamePattern>

    </rollingPolicy>
  </appender>

  <root level="INFO">
    <appender-ref ref="ROLLIN" />
  </root>

  <logger name="org.springframework.web" level="INFO"/>
</configuration>

To override the default file appender and change it to daily rollover, you could use a logback-spring.xml looking like this:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <include resource="org/springframework/boot/logging/logback/defaults.xml"/>
    <property name="LOG_FILE" value="${LOG_FILE:-${LOG_PATH:-${LOG_TEMP:-${java.io.tmpdir:-/tmp}}/}spring.log}"/>
    <include resource="org/springframework/boot/logging/logback/console-appender.xml"/>

    <appender name="ROLLING-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.TimeBasedRollingPolicy">
            <!-- daily rollover -->
            <fileNamePattern>${LOG_FILE}.%d{yyyy-MM-dd}.log</fileNamePattern>
        </rollingPolicy>
    </appender>

    <root level="INFO">
        <appender-ref ref="CONSOLE"/>
        <appender-ref ref="ROLLING-FILE"/>
    </root>

</configuration>
logging.file.name=MyApp.log
logging.pattern.rolling-file-name=MyApp-%d{yyyy-MM-dd-HH-mm-ss}.%i.log

Working with Spring Boot 2.3.4 and 2.2.10
Not working with Spring Boot 2.1.17

application.yml

logging:
  logback:
    rollingpolicy:
      file-name-pattern: ${LOG_FILE}.%d{yyyy-MM-dd}.%i.gz # by date
      max-file-size: 10MB # by size

Works on springboot v.2.5.10

You can also configure rolling policy based on file size in your logback-spring.xml. In the below, we are specifying max file size as 10MB for SizeBasedTriggeringPolicy :

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

    <configuration>

        <include resource="org/springframework/boot/logging/logback/defaults.xml" />
        <property name="LOG_FILE" value="${LOG_FILE:-${LOG_PATH:-${LOG_TEMP:-${java.io.tmpdir:-/tmp}}/}spring.log}"/>
        <include resource="org/springframework/boot/logging/logback/console-appender.xml" />

        <appender name="ACTUAL_LOG_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.FixedWindowRollingPolicy">
                <!-- gz extension to enable file deletion by logrotator  -->
                <fileNamePattern>${LOG_FILE}.%i.gz</fileNamePattern>
                 <minIndex>1</minIndex>
                <maxIndex>10</maxIndex>
            </rollingPolicy>
            <triggeringPolicy
                class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
                <MaxFileSize>10MB</MaxFileSize>
            </triggeringPolicy>
        </appender>

        <root level="INFO">
            <appender-ref ref="ACTUAL_LOG_FILE" />
        </root>

    </configuration>

A little late to the party... but I was able to get my log file to roll (by size) with the following configuration in application.yaml and no logback.xml configuration whatsoever:

logging:
    file: /var/log/webapps/app/app.log

    # Roll the log file when it reaches max size
    file.max-size: 1024KB

    # Limit the number of log files retained
    file.max-history: 50

    pattern:
        console: "%d %-5level %logger : %msg%n"
        file: "%d %-5level [%thread] %logger : %msg%n"

    level:
        root:                                           info
        my.package.of.app:                              debug
        org.springframework:                            error
        # etc. etc.

In the application.properties file add these lines of code, use any these according to your requirement

logging.file.name=myinfo.log
#daily rolling logs
logging.pattern.rolling-file-name=myinfo-%d{yyyy-MM-dd}.%i.log
#per hour rolling logs
logging.pattern.rolling-file-name=myinfo-%d{yyyy-MM-dd-HH}.%i.log
#per minute rolling logs
logging.pattern.rolling-file-name=myinfo-%d{yyyy-MM-dd-HH-mm}.%i.log
#per secs rolling logs
 logging.pattern.rolling-file-name=myinfo-%d{yyyy-MM-dd-HH-mm-ss}.%i.log

From this link :-

logging:
  file: logs/application-debug.log
  pattern:
    console: "%d %-5level %logger : %msg%n"
    file: "%d %-5level [%thread] %logger : %msg%n"
  level:
    org.springframework.web: ERROR
    com.howtodoinjava: INFO
    org.hibernate: ERROR

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM