How does logback log SQL statements?
To print SQL logs using logback, you need to follow these steps:
- pom.xml refers to the Project Object Model file in a Java project.
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
</dependency>
- The configuration file for logback is logback.xml.
- logger
<configuration>
<!-- 其他配置 -->
<logger name="com.example.MySQL" level="DEBUG">
<appender-ref ref="CONSOLE" />
</logger>
<!-- 其他配置 -->
</configuration>
In the
- logger is Slf4J
String url = "jdbc:mysql://localhost:3306/mydb?logger=Slf4J";
In this way, when you use logback to log SQL statements, the log will be printed to the output destination specified in the logback configuration file.
Please note that the logging level in the above example is set to DEBUG, which means only SQL statements at the DEBUG level and above will be recorded. You can adjust the logging level according to your needs.