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

ele
  • 471
  • 1
  • 5
  • 15

8 Answers8

33

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>
Donovan Muller
  • 3,822
  • 3
  • 30
  • 54
  • 7
    Thanks Donovan, I was wondering if I could avoid having the logback.xml like in dropwizard I can define it in the config.yml. i.e. under logging I can define appenders with type: file, currentLogFilename: /mypath/myfile.log, archiveLogFilenamePattern: /mypath/myfile-d%(yyyy-MM-dd}.log.gz, archiveFileCount: 3, timeZone: UTC. It seems something like that is not possible in Spring Boot currently. – ele Apr 28 '15 at 13:17
  • Hmm, `logging.config` might be worth a look from [here](https://github.com/spring-projects/spring-boot/blob/master/spring-boot/src/main/java/org/springframework/boot/logging/LoggingApplicationListener.java) but not sure... – Donovan Muller Apr 28 '15 at 13:29
  • Thanks a lot for your help and prompt response. I will use the logback.xml for now as you suggested. – ele Apr 28 '15 at 14:00
  • 3
    faced the same issue, so @ele does it mean it's not possible to configure appender type in Spring Boot? – yetanothercoder Aug 14 '16 at 09:43
  • the OP asks about application.yml not logback.xml – Amnon Aug 31 '17 at 09:20
  • 2
    @Amnon Logback is the default logging implementation in Spring Boot and there is no generic way to configure this in `application.yml`, so you have to configure this in an implementation specific configuration file. – Donovan Muller Aug 31 '17 at 09:27
  • 1
    @Donovan, I see. So this should have been stated explicitly in your answer. – Amnon Aug 31 '17 at 10:39
  • More recent versions of Spring explicitly address this - [see here](https://docs.spring.io/spring-boot/docs/2.6.7/reference/html/features.html#features.logging.file-rotation) - but I must confess I haven't managed to make it work yet. – Paul Hodges Sep 02 '22 at 13:18
9

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>
xmatjar
  • 99
  • 1
  • 2
  • 1
    For me it works when removing the `file` attribute, then I specify the prop logging.file=path/fileName – Olivier Boissé Jan 16 '17 at 21:06
  • I was looking for this configuration, where the logging would be both on console and at the file. Thanks ! – Digao Feb 24 '17 at 20:59
  • Anyone have the solution through yml file? I am facing the same problem : http://stackoverflow.com/questions/43177232/spring-boot-logback-rolling-file-append-er-not-working – Arpan Das Apr 04 '17 at 05:21
  • 2
    the OP asks about application.yml not logback.xml – Amnon Aug 31 '17 at 09:20
7
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

Shawrup
  • 2,478
  • 2
  • 12
  • 21
  • 2
    This is working perfectly fine, If you want daily logs use as logging.pattern.rolling-file-name=MyApp-%d{yyyy-MM-dd}.%i.log, if you want hourly logs logging.pattern.rolling-file-name=MyApp-%d{yyyy-MM-dd-HH}.%i.log – Bhaskara Arani Oct 16 '20 at 13:13
  • 3
    **Deprecated.** See: [logging.logback.rollingpolicy.file-name-pattern](https://docs.spring.io/spring-boot/docs/current/reference/html/features.html#features.logging.custom-log-configuration) Pattern for rolled-over log file names (default `${LOG_FILE}.%d{yyyy-MM-dd}.%i.gz`) – lcnicolau Feb 05 '22 at 20:47
1

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

Xieyi
  • 1,316
  • 1
  • 14
  • 19
0

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>
Gutblender
  • 1,340
  • 1
  • 12
  • 25
anayagam
  • 121
  • 2
  • 4
0

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.
-1

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
Bhaskara Arani
  • 1,556
  • 1
  • 26
  • 44
-2

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
indranil32
  • 140
  • 1
  • 9