How to configure rolling file appender within Spri

2019-01-09 08:39发布

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

i.e. 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

3条回答
唯我独甜
2楼-- · 2019-01-09 08:53

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
查看更多
一夜七次
3楼-- · 2019-01-09 08:56

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>
查看更多
Evening l夕情丶
4楼-- · 2019-01-09 09:12

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

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

I.e. 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>
查看更多
登录 后发表回答