SpringBoot configures the log output method through yml and xml files

  • 2021-07-13 05:10:01
  • OfStack

Logback is used for log output by default in SpringBoot, which can be configured by using application. yml of SpringBoot framework or logback. xml of logback.

Configured through application. yml


<?xml version="1.0" encoding="UTF-8"?>
<configuration debug="false">
 <!-- Define the storage address of log files   Don't be in  LogBack  Use relative paths in the configuration of -->
 <property name="LOG_HOME" value="/test/log" />
 <!--  Console output  -->
 <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
 <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
  <!-- Formatting output: %d Represents a date, %thread Represents the thread name, %-5level : Levels are displayed from the left 5 Width of characters %msg Log messages, %n Is a newline character -->
  <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern>
 </encoder>
 </appender>
 <!--  Generate log files on a daily basis  -->
 <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
 <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
  <!-- File name of log file output -->
  <FileNamePattern>${LOG_HOME}/my.log.%d{yyyy-MM-dd}.log</FileNamePattern>
  <!-- Log file retention days -->
  <MaxHistory>30</MaxHistory>
 </rollingPolicy>
 <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
  <!-- Formatting output: %d Represents a date, %thread Represents the thread name, %-5level : Levels are displayed from the left 5 Width of characters %msg Log messages, %n Is a newline character -->
  <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern>
 </encoder>
 <!-- Maximum Log File Size -->
 <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
  <MaxFileSize>10MB</MaxFileSize>
 </triggeringPolicy>
 </appender>

 <!--  Log output level  -->
 <root level="INFO">
 <appender-ref ref="STDOUT" />
 <appender-ref ref="FILE" />
 </root>
</configuration>

Configuration via yml

There is no way to configure the log file in the same format as xml, which can output it as ${LOG_HOME}/my. log.% d {yyyy-MM-dd}. log.


logging:
 pattern:
  file: "my.log.%d{yyyy-MM-dd}.log"
  console: "%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n"
 path: "./logs"
 file:
  max-history: 30
  max-size: 10MB
 level:
 root: INFO

Related articles: