Logs are always necessary during development or after a project has been published to the PRD. It’s a great tool for troubleshooting problems. In this section we will discuss spring-boot logging.

demand

  1. With the ability to log files, simply exporting to the console without saving is no way to trace back to a distant problem.
  2. Service logs are treated differently from system logs. Only service logs are recorded in a specific file.
  3. Logs can be archived or fragmented to prevent large log files.

logback

Springboot’s Logback meets this requirement.

Spring Boot uses Commons Logging for all internal Logging, but leaves the underlying Logging implementation on. Default configurations for Java Util Logging, Log4J2, and Logback are provided. In each case, the logger is preconfigured to use console output, along with optional file output.

Logback-spring. XML in the resource directory is loaded by default.

<?xml version = "1.0" encoding = "UTF-8"? >
<configuration>
    <! -- Import default configuration, can be directly used -->
    <include resource="org/springframework/boot/logging/logback/defaults.xml" />
    <! Define a console output configuration -->
    <appender name = "STDOUT" class = "ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>${CONSOLE_LOG_PATTERN}</pattern>
        </encoder>
    </appender>
     <! Define a file output configuration -->
    <appender name="FILELOG" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <encoder>
            <pattern>${FILE_LOG_PATTERN}</pattern>
        </encoder>
        <file>${LOG_FILE}_BIZ.log</file>
        <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
            <fileNamePattern>${LOG_FILE}_BIZ.%d{yyyy-MM-dd}.%i.gz</fileNamePattern>
            <maxFileSize>${LOG_FILE_MAX_SIZE:-10MB}</maxFileSize>
            <maxHistory>${LOG_FILE_MAX_HISTORY:-0}</maxHistory>
        </rollingPolicy>
    </appender>
    <root level = "INFO">
        <appender-ref ref = "STDOUT"/> 
    </root>
      <! -- Do special processing for logs under specific packages -->
    <logger name="com.vison" additivity="false" level="debug">
        <! -- Reference custom appender -->
        <appender-ref ref="FILELOG"/>
        <! The console appender is also referenced here to see our logs in the console.
        <appender-ref ref="STDOUT"/>
    </logger>
</configuration>

Copy the code

The logging.file.name configuration defines the file name and path for logging file output