SoFunction
Updated on 2025-03-03

MyBatis-Plus uses sl4j log to print SQL code details

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

  1. Dependency conflict

    • When using Spring Boot starters, SLF4J and Logback are usually introduced automatically, so there is no need to add manuallyslf4j-apiandlogback-classic. If added manually, it may result in dependency conflicts.
  2. Default log level

    • The default log level of MyBatis-Plus isINFO, which means that if not explicitly configured, onlyINFOLog 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
  3. Automatic configuration

    • Spring Boot will automatically configure the logging system, usually without manual settings.or. The system uses the default configuration to handle logging.
  4. Log level configuration

    • You can passorFile 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.
  level:
    : debug   # Set the log level of your own package    : debug  # Set the log level of MyBatis to debug
  1. use

    • If you need to have finer granular control over logging, such as custom log formats or output locations, you can create adocument. This is the recommended way, as Spring Boot will use this file first.

2. File configuration (optional)

existsrc/main/resourcesCreated 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

  1. Appenders
    • ConsoleAppender: Output logs to the console. You can add more appenders as needed, for exampleFileAppender, output the log to the file.
  2. 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.
  3. 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-loggingDependencies, 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 toDEBUG, 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!