It is the same question as Setting a log file name to include current date in Log4j , but how to apply it to Spring Boot, which comes with slf4j?
application.properties
spring.application.name=keywords
logging.file=logs/${spring.application.name}.log
To make Spring Boot write its log to disk, set the path and filename. With this configuration, Spring Boot will write to the console and also to a log file called spring. log , at the path you specify.
By default, Spring Boot includes SLF4J along with Logback implementations. If Logback is available, Spring Boot will choose it as the logging handler. You can easily configure logging levels within the application. properties file without having to create logging provider-specific configuration files such as logback.
All the supported logging systems can have the logger levels set in the Spring Environment (for example, in application. properties ) by using logging. level. <logger-name>=<level> where level is one of TRACE, DEBUG, INFO, WARN, ERROR, FATAL, or OFF.
By default, Spring Boot picks up the native configuration from its default location for the system (such as classpath:logback. xml for Logback), but you can set the location of the config file by using the "logging. config" property.
As described here
Spring Boot has a LoggingSystem abstraction that attempts to configure logging based on the content of the classpath.
To employ it
The simplest way to do that is through the starter poms which all depend on
spring-boot-starter-logging
. For a web application you only needspring-boot-starter-web
since it depends transitively on the logging starter.
Because Logback is available it is the first choice.
To configure the more fine-grained settings of a logging system you need to use the native configuration format supported by the LoggingSystem in question. By default Spring Boot picks up the native configuration from its default location for the system (e.g. classpath:logback.xml for Logback), but you can set the location of the config file using the "logging.config" property.
If default is ok for you just create logback.xml
and add corresponding file appender, e.g.
<appender name="rollingFileAppender" class="ch.qos.logback.core.rolling.RollingFileAppender">
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<FileNamePattern>LogFile.%d{yyyy-MM-dd}.log</FileNamePattern>
<MaxHistory>30</MaxHistory>
</rollingPolicy>
<encoder>
<pattern>%d %-5level [%thread] %logger{0}: %msg%n</pattern>
</encoder>
</appender>
Additional documentation could be found here
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With