SpringBoot打印系統(tǒng)執(zhí)行的sql語句及日志配置指南
直接在application.yml/properties文件中進行配置
引入依賴:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<!-- boot父依賴 可以指定版本號(指定后boot其他依賴不加版本號不報錯 否則報錯) -->
<version>2.3.6.RELEASE</version>
<relativePath/>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<!-- 提供Web開發(fā)場景所需的底層所有依賴,springboot默認集成了tomcat服務器 -->
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
<!-- 點進去里面有l(wèi)ogback、log4j和slf4j -->
<!-- 如果添加了這個依賴,SpringBoot 應用將自動使用 logback 作為應用日志框架 -->
</dependency>
</dependencies>
1、Mybatis內置的日志工廠
Mybatis內置的日志工廠提供日志功能,具體的日志實現(xiàn)有以下幾種工具:
slf4j
Apache Commons Logging
Log4j 2
Log4j
JDK logging
具體選擇哪個日志實現(xiàn)工具由MyBatis的內置日志工廠確定。它會使用最先找到的(按上文列舉的順序查找)。如果一個都未找到,日志功能就會被禁用。
配置log-impl:可供選擇的有以下幾種

yml中:
mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl #這個是可以打印sql、參數(shù)、查詢結果的,只會打印到控制臺不會輸出到日志文件中
#org.apache.ibatis.logging.log4j.Log4jImpl:這個不打印查詢結果
mapper-locations: classpath:mapper/*.xml
type-aliases-package: com.example.plan.entity # 指定的包名
或properties中:
# mybatis plus mybatis-plus.mapper-locations=classpath:mapper/*Mapper.xml mybatis-plus.type-aliases-package=com.example.plan mybatis-plus.configuration.map-underscore-to-camel-case=true mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl # 重點
StdOutImpl控制臺輸出結果:
==> Preparing: select id, name, sex from t_user where sex = ? ==> Parameters: 0(Integer) <== Total: 8 /** *第一行是執(zhí)行的sql語句 *第二行是傳入的參數(shù) *第三行是返回結果的行數(shù) */
補充:
log-impl用于設置打印sql的日志實現(xiàn),指定的值為org.apache.ibatis.logging.Log接口的其中的一個實現(xiàn)類
在這里以StdOutImpl為例
public class StdOutImpl implements Log {
public StdOutImpl(String clazz) {
// Do Nothing
}
@Override
public boolean isDebugEnabled() {
return true;
}
@Override
public boolean isTraceEnabled() {
return true;
}
@Override
public void error(String s) {
System.err.println(s);
}
@Override
public void debug(String s) {
System.out.println(s);
}
@Override
public void trace(String s) {
System.out.println(s);
}
@Override
public void warn(String s) {
System.out.println(s);
}
}
方法里面的輸出,是用的System.out/error.println方法,所以如果配置為org.apache.ibatis.logging.stdout.StdOutImpl就只會在控制臺窗口打印,不會記錄到日志文件。
如果需要保存打印SQL到文件就不能設置為StdOutImpl,可以設置為Slf4jImpl。部分代碼如下,使用的是Log的方法,保存到文件中。
@Override
public void error(String s) {
log.error(s);
}
@Override
public void debug(String s) {
log.debug(s);
}
@Override
public void trace(String s) {
log.trace(s);
}
@Override
public void warn(String s) {
log.warn(s);
}
也可以不設置。然后對應接口所在包設置logback對應包的日志等級[debug\info\error\warn等],即下面所提到的第二種方式。
級別從高到低:OFF 、 FATAL 、 ERROR 、 WARN 、 INFO 、 DEBUG 、 TRACE 、 ALL
2、Spring Boot集成Mybatis

logging.file.name設置日志文件:支持絕對路徑、相對路徑,相對路徑的話是相對應用的根目錄,比如logging.file.name=logs/demo.log;
默認:spring.log
logging.file.path設置日志路徑:支持絕對路徑、相對路徑,比如 logging.file.path=logs,日志文件默認會存為 spring.log;
默認:根路徑/LOG_PATH_IS_UNDEFINED/
yml中:
#spring boot集成mybatis的方式打印sql
logging:
level:
com.xxx.mapper: debug # 包路徑為mapper文件包路徑
org.springframework: warn
org.apache.ibatis.logging: debug
file:
path/name: #加不加這個配置就看你的resources目錄下是否有l(wèi)ogbackxxxx之類的xml文件,有的話會自動加載這個,識別里面的logpath。如果外部的這個日志配置和xml文件中具有相同的配置的話,哪個優(yōu)先級更高一點?
此時,如果有相應的log配置文件,比如叫l(wèi)ogback-spring.xml。
配置文件中部分內容如下:
<!--開發(fā)環(huán)境:打印控制臺-->
<springProfile name="dev,prod,test">
<logger name="com.example.plan" level="info"/>
</springProfile>
<root level="info">
<appender-ref ref="CONSOLE"/>
<appender-ref ref="INFO_FILE"/>
<appender-ref ref="WARNING_FILE"/>
<appender-ref ref="ERROR_FILE"/>
</root>
<!-- 其中的springProfile標簽指定了name,那么在application.yml文件中要啟用dev,prod,test中的一個,不然輸出的內容為空。logger標簽中的name限定了范圍。 -->
相應的yml中的logging配置:
logging:
config: ${spring.config.location}logback-spring.xml # 不加也行,Springboot默認的日志配置文件名稱為logback.xml和logback-spring.xml、logbackxxxx.xml
spring:
profiles:
active: dev
3、總結
| sql | 其它日志信息 | 簡述 | 配置 |
|---|---|---|---|
| 只打印到控制臺 | 不輸出 | 只打印sql到控制臺 | 僅需log-impl配置為StdOutImpl |
| 只打印到控制臺 | 輸出 | 其它日志信息保存到文件,sql只打印到控制臺,不保存到文件中 | log-impl配置為StdOutImpl,再加上logging的配置(logging.config指定配置文件/ resources目錄下有符合spring boot自動化配置文件名的logbackxxx.xml/ yml中l(wèi)ogging.level/file配置完整一些) |
| 打印到控制臺且輸出到日志文件中 | 輸出 | sql和其它日志信息保存到文件中,sql也打印到控制臺 | log-impl為Slf4jImpl/或者不設置,總之不能設置為StdOutImpl,再加上logging的配置即可 |
配置為logback-test.xml后,控制臺的輸出:
15:50:13,315 |-INFO in ch.qos.logback.classic.LoggerContext[logback] - Found resource [logback-test.xml] at [file:/Users/congee/Desktop/code/GService/service/target/classes/logback-test.xml]
15:50:13,342 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - debug attribute not set
15:50:13,345 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - Will scan for changes in [file:/Users/congee/Desktop/code/GService/service/target/classes/logback-test.xml]
15:50:13,345 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - Setting ReconfigureOnChangeTask scanning period to 10 seconds
15:50:13,357 |-INFO in ch.qos.logback.classic.joran.action.ContextNameAction - Setting logger context name as [logback]
15:50:13,357 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - About to instantiate appender of type [ch.qos.logback.core.ConsoleAppender]
15:50:13,358 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - Naming appender as [CONSOLE]
15:50:13,362 |-INFO in ch.qos.logback.core.joran.action.NestedComplexPropertyIA - Assuming default type [ch.qos.logback.classic.encoder.PatternLayoutEncoder] for [encoder] property
15:50:13,381 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - About to instantiate appender of type [ch.qos.logback.core.rolling.RollingFileAppender]
15:50:13,382 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - Naming appender as [INFO_FILE]
15:50:13,382 |-INFO in ch.qos.logback.core.joran.action.NestedComplexPropertyIA - Assuming default type [ch.qos.logback.classic.encoder.PatternLayoutEncoder] for [encoder] property
15:50:13,386 |-INFO in c.q.l.core.rolling.TimeBasedRollingPolicy@1858609436 - No compression will be used
15:50:13,387 |-INFO in c.q.l.core.rolling.TimeBasedRollingPolicy@1858609436 - Will use the pattern /Users/congee/log/monitor//log-info-%d{yyyy-MM-dd}.%i.log for the active file
15:50:13,388 |-INFO in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@7276c8cd - The date pattern is ‘yyyy-MM-dd’ from file name pattern ‘/Users/congee/log/monitor//log-info-%d{yyyy-MM-dd}.%i.log’.
15:50:13,388 |-INFO in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@7276c8cd - Roll-over at midnight.
15:50:13,389 |-INFO in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@7276c8cd - Setting initial period to Thu Apr 13 15:50:13 CST 2023
15:50:13,389 |-WARN in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@7276c8cd - SizeAndTimeBasedFNATP is deprecated. Use SizeAndTimeBasedRollingPolicy instead
15:50:13,389 |-WARN in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@7276c8cd - For more information see http://logback.qos.ch/manual/appenders.html#SizeAndTimeBasedRollingPolicy
15:50:13,392 |-INFO in ch.qos.logback.core.rolling.RollingFileAppender[INFO_FILE] - Active log file name: /Users/congee/log/monitor//log-info-2023-04-13.0.log
15:50:13,392 |-INFO in ch.qos.logback.core.rolling.RollingFileAppender[INFO_FILE] - File property is set to [null]
15:50:13,392 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - About to instantiate appender of type [ch.qos.logback.core.rolling.RollingFileAppender]
15:50:13,392 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - Naming appender as [WARNING_FILE]
15:50:13,392 |-INFO in ch.qos.logback.core.joran.action.NestedComplexPropertyIA - Assuming default type [ch.qos.logback.classic.encoder.PatternLayoutEncoder] for [encoder] property
15:50:13,393 |-INFO in c.q.l.core.rolling.TimeBasedRollingPolicy@1414147750 - No compression will be used
15:50:13,393 |-INFO in c.q.l.core.rolling.TimeBasedRollingPolicy@1414147750 - Will use the pattern /Users/congee/log/monitor//log-warning-%d{yyyy-MM-dd}.%i.log for the active file
15:50:13,393 |-INFO in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@2e3fc542 - The date pattern is ‘yyyy-MM-dd’ from file name pattern ‘/Users/congee/log/monitor//log-warning-%d{yyyy-MM-dd}.%i.log’.
15:50:13,393 |-INFO in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@2e3fc542 - Roll-over at midnight.
15:50:13,393 |-INFO in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@2e3fc542 - Setting initial period to Thu Apr 13 15:50:13 CST 2023
15:50:13,394 |-WARN in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@2e3fc542 - SizeAndTimeBasedFNATP is deprecated. Use SizeAndTimeBasedRollingPolicy instead
15:50:13,394 |-WARN in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@2e3fc542 - For more information see http://logback.qos.ch/manual/appenders.html#SizeAndTimeBasedRollingPolicy
15:50:13,394 |-INFO in ch.qos.logback.core.rolling.RollingFileAppender[WARNING_FILE] - Active log file name: /Users/congee/log/monitor//log-warning-2023-04-13.0.log
15:50:13,394 |-INFO in ch.qos.logback.core.rolling.RollingFileAppender[WARNING_FILE] - File property is set to [null]
15:50:13,394 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - About to instantiate appender of type [ch.qos.logback.core.rolling.RollingFileAppender]
15:50:13,394 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - Naming appender as [ERROR_FILE]
15:50:13,394 |-INFO in ch.qos.logback.core.joran.action.NestedComplexPropertyIA - Assuming default type [ch.qos.logback.classic.encoder.PatternLayoutEncoder] for [encoder] property
15:50:13,395 |-INFO in c.q.l.core.rolling.TimeBasedRollingPolicy@22069592 - No compression will be used
15:50:13,395 |-INFO in c.q.l.core.rolling.TimeBasedRollingPolicy@22069592 - Will use the pattern /Users/congee/log/monitor//log-error-%d{yyyy-MM-dd}.%i.log for the active file
15:50:13,395 |-INFO in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@4524411f - The date pattern is ‘yyyy-MM-dd’ from file name pattern ‘/Users/congee/log/monitor//log-error-%d{yyyy-MM-dd}.%i.log’.
15:50:13,395 |-INFO in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@4524411f - Roll-over at midnight.
15:50:13,395 |-INFO in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@4524411f - Setting initial period to Thu Apr 13 15:50:13 CST 2023
15:50:13,395 |-WARN in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@4524411f - SizeAndTimeBasedFNATP is deprecated. Use SizeAndTimeBasedRollingPolicy instead
15:50:13,395 |-WARN in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@4524411f - For more information see http://logback.qos.ch/manual/appenders.html#SizeAndTimeBasedRollingPolicy
15:50:13,395 |-INFO in ch.qos.logback.core.rolling.RollingFileAppender[ERROR_FILE] - Active log file name: /Users/congee/log/monitor//log-error-2023-04-13.0.log
15:50:13,395 |-INFO in ch.qos.logback.core.rolling.RollingFileAppender[ERROR_FILE] - File property is set to [null]
15:50:13,396 |-ERROR in ch.qos.logback.core.joran.spi.Interpreter@91:31 - no applicable action for [springProfile], current ElementPath is [[configuration][springProfile]]
15:50:13,396 |-ERROR in ch.qos.logback.core.joran.spi.Interpreter@92:54 - no applicable action for [logger], current ElementPath is [[configuration][springProfile][logger]]
15:50:13,396 |-INFO in ch.qos.logback.classic.joran.action.RootLoggerAction - Setting level of ROOT logger to INFO
15:50:13,396 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [CONSOLE] to Logger[ROOT]
15:50:13,396 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [INFO_FILE] to Logger[ROOT]
15:50:13,396 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [WARNING_FILE] to Logger[ROOT]
15:50:13,396 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [ERROR_FILE] to Logger[ROOT]
15:50:13,396 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - End of configuration.
15:50:13,396 |-INFO in ch.qos.logback.classic.joran.JoranConfigurator@401e7803 - Registering current configuration as safe fallback point
配置完號后的緊接著的日志輸出:
2023-04-13 15:50:18.450 [background-preinit] INFO org.hibernate.validator.internal.util.Version - HV000001: Hibernate Validator 6.1.6.Final
2023-04-13 15:50:18.689 [main] INFO c.u.j.c.EnableEncryptablePropertiesBeanFactoryPostProcessor - Post-processing PropertySource instances
2023-04-13 15:50:18.715 [main] INFO c.u.j.EncryptablePropertySourceConverter - Converting PropertySource configurationProperties [org.springframework.boot.context.properties.source.ConfigurationPropertySourcesPropertySource] to AOP Proxy
可以直觀的感受到前面日志的時間格式的變化。
到此這篇關于SpringBoot打印系統(tǒng)執(zhí)行的sql語句及日志配置指南的文章就介紹到這了,更多相關SpringBoot打印系統(tǒng)執(zhí)行sql語句內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
java數(shù)據(jù)結構圖論霍夫曼樹及其編碼示例詳解
這篇文章主要為大家介紹了java數(shù)據(jù)結構圖論霍夫曼樹及其編碼示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步早日升職加薪2021-11-11
Java中使用DOM4J生成xml文件并解析xml文件的操作
這篇文章主要介紹了Java中使用DOM4J來生成xml文件和解析xml文件的操作,今天通過代碼給大家展示了解析xml文件和生成xml文件的方法,需要的朋友可以參考下2021-09-09
MyBatis-Plus實現(xiàn)公共字段自動填充功能詳解
在開發(fā)中經(jīng)常遇到多個實體類有共同的屬性字段,這些字段屬于公共字段,也就是很多表中都有這些字段,能不能對于這些公共字段在某個地方統(tǒng)一處理,來簡化開發(fā)呢?MyBatis-Plus就提供了這一功能,本文就來為大家詳細講講2022-08-08
SpringCloud Gateway的路由,過濾器和限流解讀
這篇文章主要介紹了SpringCloud Gateway的路由,過濾器和限流解讀,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-02-02
將SpringBoot屬性配置類@ConfigurationProperties注冊為Bean的操作方法
在現(xiàn)代 Spring Boot 應用開發(fā)中,外部化配置(Externalized Configuration)是實現(xiàn)應用靈活性和可維護性的核心機制之一,然而,許多開發(fā)者在使用的時候經(jīng)常會遇到如何確保該配置類被正確注冊為 Spring 容器中的 Bean,下面小編為大家詳細說說2025-12-12

