We use Spring Boot’s official Spring-boot-starter-logging as a logging dependency, and add the dependency to the POP.xml file to work out of the box:

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-logging</artifactId>
</dependency>
Copy the code

Console debugging

When we are developing, we will need to customize the output to the console for tracing. We can do this by adding @slf4J annotations to the class and then using the injected log variable inside the method:

log.warn("xfly demo.........");
Copy the code

Note that only ERROR, WARN, and INFO logs are output to the console by default. You can set the level to DEBUG during development, and set logging.level to DEBUG in the application-dev.yml file

Custom log format

It doesn’t matter what debug format you output in the console, but we often need to log to a file, especially in online environments, which is very useful for troubleshooting. There are also very mature heavy-duty solutions for logging, we can temporarily use the built-in Logback of Spring, which is more suitable for this project.

To configure Logback, create a new logback-spring. XML file in the Resources directory and configure:

<?xml version="1.0" encoding="UTF-8" ? >
<configuration>
    <springProperty scope="context" name="logging.file.path" source="logging.file.path"/>
    <contextName>wxbox</contextName>
    <appender name="consoleLog" class="ch.qos.logback.core.ConsoleAppender">
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <pattern>%yellow(%d{yyyy-MM-dd HH:mm:ss}) %red([%thread]) %highlight(%-5level) %cyan(%logger{50}) - %magenta(%msg) %n</pattern>
            <charset>UTF-8</charset>
        </encoder>
    </appender>

    <appender name="fileInfoLog" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>ERROR</level>
            <onMatch>DENY</onMatch>
            <onMismatch>ACCEPT</onMismatch>
        </filter>
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <pattern>
                %d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{50} - %msg%n
            </pattern>
            <charset>UTF-8</charset>
        </encoder>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>${logging.file.path}/wxbox.info.%d{yyyy-MM-dd}.log</fileNamePattern>
            <MaxHistory>90</MaxHistory>
            <totalSizeCap>1GB</totalSizeCap>
        </rollingPolicy>
    </appender>

    <appender name="fileErrorLog" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
            <level>ERROR</level>
        </filter>
        <encoder>
            <pattern>
                %d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{50} - %msg%n
            </pattern>
        </encoder>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>${logging.file.path}/wxbox.error.%d{yyyy-MM-dd}.log</fileNamePattern>
            <MaxHistory>90</MaxHistory>
        </rollingPolicy>
    </appender>
    <root level="info">
        <appender-ref ref="consoleLog"/>
        <appender-ref ref="fileInfoLog"/>
        <appender-ref ref="fileErrorLog"/>
    </root>
</configuration>
Copy the code

To outline a few blocks of content:

• A variable logging.file.path is declared and created via the springProperty tag corresponding to the logging.file.path configuration item in application.yml so that it can be used in the following tags, Dynamic configuration is implemented.

• each appender tag can be thought of as a printer, either as a ConsoleAppender for console output or as a RollingFIleAppender for file output.

• Each appender can use the Pattern tag to set the output format. For console output, additional colors can be set. For file output, there is no color or color.

• RollingFIleAppender can set scrolling policies such as logging by day, maximum number of days to log, maximum file size for a single file, etc.

• Finally create these appenders with the root tag.