Springboot 2.0.3 Configuration Log

Keywords: Java SpringBoot log4j xml SQL

Reason: SpringBoot uses slf4j log by default. When introducing other frameworks, it only needs to exclude the log framework that the framework depends on.

When I wanted to introduce log4j today, the pom file kept reporting errors, indicating that the jar package of log4j could not be found, which should be the reason for the springboot version of 2.0.3.

Instead of continuing to introduce, SpringBoot's slf4j logs are used. If you have the opportunity to test the efficiency comparison between log4j and slf4j logs.

First, the application.yml configuration log printing level, default is info (can also choose different directories):

#slf4j log configuration
logging:
  # Configuration level
  level:
    #Subcontracting configuration levels, i.e. different levels can be used in different directories
    com.sblueice.controller: debug

2. All configurations in logback.xml:

Configure the path of the print file (one day for 30 days):

    <! - Development and Test Environment
    <springProfile name="dev,test">
        <! - Define the path to log storage, and do not configure the relative path - >.
        <property name="FILE_PATH" value="E:/temp0918/SpringBoot-log/cccf.%d{yyyy-MM-dd}.%i.log" />
    </springProfile>

Configure to print sql logs:
 <logger name="com.sblueice.mapper.UserMapper" level="DEBUG" />
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
 
    <!-- Format output:%date Indicates the date.%thread Represents the thread name.%-5level: Level shows 5 character widths from left %msg: Log messages,%n Newline character-->
    <property name="LOG_PATTERN"
              value="%date{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n" />
 
    <!-- Development and Test Environment -->
    <springProfile name="dev,test">
        <!-- Define the path to log storage, do not configure the relative path -->
        <property name="FILE_PATH" value="E:/temp0918/SpringBoot-log/cccf.%d{yyyy-MM-dd}.%i.log" />
    </springProfile>
    
    <!-- Production environment -->
    <springProfile name="pro">
        <!-- Define the path to log storage, do not configure the relative path -->
        <property name="FILE_PATH" value="/user/lib/cccf/logs/cccf.%d{yyyy-MM-dd}.%i.log" />
    </springProfile>
 
    <!-- Console Output Log -->
    <appender name="console" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <!-- According to the above configuration LOG_PATTERN To print logs -->
            <pattern>${LOG_PATTERN}</pattern>
        </encoder>
    </appender>
 
    <!--Generate a log file every day and save it for 30 days. rollingFile It's used to split files. -->
    <appender name="rollingFile"
        class="ch.qos.logback.core.rolling.RollingFileAppender">
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>
                ${FILE_PATH}
            </fileNamePattern>
            <!-- keep 30 days' worth of history -->
            <maxHistory>30</maxHistory>
            <timeBasedFileNamingAndTriggeringPolicy
                class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                <!-- Maximum size of log files -->
                <maxFileSize>10MB</maxFileSize>
            </timeBasedFileNamingAndTriggeringPolicy>
        </rollingPolicy>
 
        <encoder>
            <pattern>${LOG_PATTERN}</pattern>
        </encoder>
    </appender>
    <!-- project default level -->
    <logger name="com.hiynn.cccf" level="INFO" />
 
    <!-- Log output level -->
    <root level="INFO">
        <appender-ref ref="console" />
        <appender-ref ref="rollingFile" />
    </root>

    <!--myibatis log configure-->
    <logger name="com.sblueice.mapper.UserMapper" level="DEBUG" />
</configuration>

Third, the controller layer outputs print logs.

package com.sblueice.controller;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sblueice.services.UserService;

/**
 * 
 * @author shaoz
 * @Date: 2019/10/09
 * 
 */

@Controller
public class UserController {
    
    
    Logger logger = LoggerFactory.getLogger(getClass());
    
    /**
     * 
    * @Title: testLog
    * @Description: TODO(Test springboot's own log printing)
    * @param: @return
    * @return: String
    * @throws
     */
    @RequestMapping("/testLog")
    @ResponseBody
    public String testLog() {
        
            // Level from low to high trace<debug<info<warn<error
            logger.trace("This is a trace Journal...");
            logger.debug("This is a debug Journal...");
            // SpringBoot The default is info Level, only output info Logs at or above the level
            logger.info("This is a info Journal...");
            logger.warn("This is a warn Journal...");
            logger.error("This is a error Journal...");
            String str = "https://www.cnblogs.com/steveshao/";
            logger.info("======Welcome to the footless bird blog:{}\n", str);
         
            return null;
    }

}
    

The above is the process of SpringBoot printing logs today, but also learn from many people's things. At present, there is no link for reference. If any students feel that I have copied your original text, please contact me to add the link of the original text to you.

Posted by Markx on Thu, 10 Oct 2019 07:18:23 -0700