Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to include date in log file's name with Spring Boot / slf4j?

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
like image 619
sinedsem Avatar asked Feb 22 '16 11:02

sinedsem


People also ask

How do I specify a logging file in Spring Boot?

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.

Does Spring Boot uses SLF4J along with Logback implementation to log messages?

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.

How do I set logging levels in application properties?

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.

How is Logback configured for logging in Spring Boot?

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.


1 Answers

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 need spring-boot-starter-web since it depends transitively on the logging starter.

enter image description here

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

like image 80
Mike Shauneu Avatar answered Sep 20 '22 11:09

Mike Shauneu