How to configure rolling file appender within Spring Boot's application.yml
56,552
Solution 1
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>
Solution 2
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>
Solution 3
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
Author by
ele
Updated on October 17, 2020Comments
-
ele over 3 years
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 about 9 yearsThanks 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.
-
Donovan Muller about 9 yearsHmm,
logging.config
might be worth a look from here but not sure... -
ele about 9 yearsThanks a lot for your help and prompt response. I will use the logback.xml for now as you suggested.
-
yetanothercoder over 7 yearsfaced the same issue, so @ele does it mean it's not possible to configure appender type in Spring Boot?
-
Olivier Boissé over 7 yearsFor me it works when removing the
file
attribute, then I specify the prop logging.file=path/fileName -
Digao about 7 yearsI was looking for this configuration, where the logging would be both on console and at the file. Thanks !
-
Arpan Das about 7 yearsAnyone have the solution through yml file? I am facing the same problem : stackoverflow.com/questions/43177232/…
-
Amnon over 6 yearsthe OP asks about application.yml not logback.xml
-
Amnon over 6 yearsthe OP asks about application.yml not logback.xml
-
Donovan Muller over 6 years@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. -
Amnon over 6 years@Donovan, I see. So this should have been stated explicitly in your answer.
-
Do Will over 5 yearsThat is not a RollingFileAppender
-
Bhaskara Arani over 3 yearsThis 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
-
lcnicolau over 2 yearsDeprecated. See: logging.logback.rollingpolicy.file-name-pattern Pattern for rolled-over log file names (default
${LOG_FILE}.%d{yyyy-MM-dd}.%i.gz
)