This is the 11th day of my participation in the Gwen Challenge in November. Check out the details: The last Gwen Challenge in 2021

1. Basic introduction

By default, Spring Boot projects log logs using Logback and output them to the console at the INFO level. The diagram below:We don’t need to add logback log dependencies directly in real development. Spring-boot-starter includes spring-boot-starter-logging, which is the default logging framework for Spring Boot.

Log levels are classified into:

TRACE < DEBUG < INFO < WARN < ERROR < FATAL
Copy the code

Only logs whose log level is greater than or equal to the specified level are displayed. That is, the default springboot level is INFO, and the log levels displayed on the console are INFO, WARN, ERROR, and FATAL

2. Configure the logback. XML log file

Depending on the logging system, you can organize the configuration file names as follows to get them loaded correctly:

Logback: logback-spring. XML, logback-spring.groovy, logback. XML, Logback. Properties, log4j-spring. XML, log4j.properties, log4j. XML Log4j2: Log4j2-spring. XML, log4j2. XML JDK (Java Util Logging) : logging.properties

Spring Boot officially recommends that you use a -spring file name for your log configuration (e.g. Logback-spring. XML instead of logback.xml). Spring Boot can add some Spring Boot-specific configuration items to it (mentioned below). If you want complete control over the log configuration but don’t want to use logback. XML as the name of the logback configuration, Application. Yml can specify a custom name via the logging.config property:

logging:
  config: classpath:logback-spring.xml
Copy the code

While it is not generally necessary to change the name of the Profile to write the code here, it can be useful if you want to use different logging configurations for different runtime profiles.Project log content;


      
<configuration>
    <! -- this XML is in spring-boot-1.5.3.release.jar -->
    <include resource="org/springframework/boot/logging/logback/defaults.xml" />
    <include resource="org/springframework/boot/logging/logback/console-appender.xml" />
    <! -- Springboot Admin allows you to dynamically control log levels via JMX -->
    <! --<jmxConfigurator/>-->

    <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <! --<File>/home/hfw-client/hfw_log/stdout.log</File>-->
        <! -- Log store location -->
        <File>D:/log/hfw-client/hfw_log/stdout.log</File>
        <encoder>
            <pattern>%date [%level] [%thread] %logger{60} [%file : %line] %msg%n</pattern>
        </encoder>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <! -- adding.gz history logs will enable compression to greatly reduce the size of log files.
            <! --<fileNamePattern>/home/hfw-client/hfw_log/stdout.log.%d{yyyy-MM-dd}.log</fileNamePattern>-->
            <fileNamePattern>D:/log/hfw-client/hfw_log/stdout.log.%d{yyyy-MM-dd}.log</fileNamePattern>
            <maxHistory>30</maxHistory><! -- Keep log for 30 days -->
        </rollingPolicy>
    </appender>
    <! -- Specify only the debug level for this path, otherwise print too many logs will not look good -->
    <logger name="com.ratel.link.dao" level="DEBUG" />

    <root level="INFO">
        <appender-ref ref="CONSOLE"/>
        <appender-ref ref="FILE"/>
    </root>
</configuration>
Copy the code

3. Print SQL logs

A silly way to do this is to change the level attribute of the root tag in the logback-spring. XML file to DEBUG:

<root level="DEBUG">
     <appender-ref ref="CONSOLE"/>
     <appender-ref ref="FILE"/>
</root>
Copy the code

If this is changed to DEBUG, the most detailed log will be printed, including the SQL statement of Mybatis (it is recommended to be used for development test only if the amount is too large).We typically set the DEBUG log for DAO packages: logback-spring.xml

 <! -- Specify only the debug level for this path, otherwise print too many logs will not look good -->
    <logger name="com.ratel.link.dao" level="DEBUG" />
Copy the code

In this case, print only SQL statements:

4. Print a log in the code

Previously, most of the time, we created log objects in each class to print information, which was quite troublesome:

private static final Logger logger = LoggerFactory.getLogger(YjServiceImpl.class);
 logger.error("xxx");
Copy the code

You can now declaratively annotate log objects directly on the class via @slf4J annotations:

<! --@Slf4j automation log object -log--> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> The < version > 1.16.16 < / version > < / dependency >Copy the code

Then you can use it directly:

@RestController
@Slf4j
@RequestMapping("slf4j")
public class Slf4jController {
    @GetMapping("test/{name}")
    public String  testslf4j(@PathVariable("name") String name){
        log.info(name+"Hello, this is testing @slf4j.");
        return name+"Hello, this is testing @slf4j."; }}Copy the code

Use a browser to access: (Note: Using postman test, it may be garbled, the general reason has been located to the @pathvariable annotation problem, so far no IOU scheme is found)

5. Complete project address

Github.com/Dr-Water/Le…

Refer to Spring Boot Log Configuration (hyperdetailed)