1. Dependency configuration
existAdd the following dependencies:
<dependency> <groupId></groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <!-- MyBatis-Plus Starter --> <dependency> <groupId></groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>Latest version</version> <!-- 请根据需要替换为Latest version --> </dependency> <!-- Spring Boot Starter Logging (Included SLF4J and Logback) --> <dependency> <groupId></groupId> <artifactId>spring-boot-starter-logging</artifactId> </dependency>
Things to note
-
Dependency conflict:
- When using Spring Boot starters, SLF4J and Logback are usually introduced automatically, so there is no need to add manually
slf4j-api
andlogback-classic
. If added manually, it may result in dependency conflicts.
- When using Spring Boot starters, SLF4J and Logback are usually introduced automatically, so there is no need to add manually
-
Default log level:
- The default log level of MyBatis-Plus is
INFO
, which means that if not explicitly configured, onlyINFO
Log information at level or above will be recorded and output. Therefore, in order to view SQL logs, it is usually necessary to set the log level of MyBatis toDEBUG
。
- The default log level of MyBatis-Plus is
-
Automatic configuration:
- Spring Boot will automatically configure the logging system, usually without manual settings.
or
. The system uses the default configuration to handle logging.
- Spring Boot will automatically configure the logging system, usually without manual settings.
-
Log level configuration:
- You can pass
or
File to configure the log level to ensure that the log level of the corresponding package is correctly set, especially your business package and MyBatis-related packages.
- You can pass
level: : debug # Set the log level of your own package : debug # Set the log level of MyBatis to debug
-
use
:
- If you need to have finer granular control over logging, such as custom log formats or output locations, you can create a
document. This is the recommended way, as Spring Boot will use this file first.
- If you need to have finer granular control over logging, such as custom log formats or output locations, you can create a
2. File configuration (optional)
existsrc/main/resources
Created in the directoryFiles and configure them as needed:
<!-- Console output --> <appender name="STDOUT" class=""> <encoder> <pattern>%d{yyyy-MM-dd HH:mm:ss} %-5level [%thread] %logger{36} - %msg%n</pattern> </encoder> </appender> <!-- Root log configuration --> <root level="debug"> <appender-ref ref="STDOUT" /> </root> <!-- MyBatis Log configuration --> <logger name="" level="debug" /> </configuration>
Detailed description of log configuration
-
Appenders:
-
ConsoleAppender
: Output logs to the console. You can add more appenders as needed, for exampleFileAppender
, output the log to the file.
-
-
Loggers:
-
root
: Define the root log level and output target. Logs for all packages not specified specifically will inherit this configuration. -
logger
: Set log levels for specific packages. Multiple loggers can be added as needed.
-
-
model:
-
%d{yyyy-MM-dd HH:mm:ss}
: Time format of log output. -
%-5level
: Log level width setting. -
[%thread]
: Output the current thread. -
%logger{36}
: Output the class name of the log, with a maximum length of 36. -
%msg
: Output log message. -
%n
: Line break.
-
Summarize
By using Spring Bootspring-boot-starter-logging
Dependencies, allowing for easier integration of SLF4J and Logback without worrying about version conflicts or configuration issues. When you need to custom log output, useIt is best practice to configure files. At the same time, make sure to set the log level correctly, especially to set the log level of MyBatis to
DEBUG
, so that you can see the required SQL log information during development and debugging.
The above is the detailed explanation of the code of MyBatis-Plus using sl4j logs to print SQL. For more information about MyBatis-Plus sl4j printing SQL, please pay attention to my other related articles!