SpringBoot打印系統(tǒng)執(zhí)行的sql語句及日志配置指南
直接在application.yml/properties文件中進(jìn)行配置
引入依賴:
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <!-- boot父依賴 可以指定版本號(hào)(指定后boot其他依賴不加版本號(hào)不報(bào)錯(cuò) 否則報(bào)錯(cuò)) --> <version>2.3.6.RELEASE</version> <relativePath/> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <!-- 提供Web開發(fā)場景所需的底層所有依賴,springboot默認(rèn)集成了tomcat服務(wù)器 --> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-logging</artifactId> <!-- 點(diǎn)進(jìn)去里面有l(wèi)ogback、log4j和slf4j --> <!-- 如果添加了這個(gè)依賴,SpringBoot 應(yīng)用將自動(dòng)使用 logback 作為應(yīng)用日志框架 --> </dependency> </dependencies>
1、Mybatis內(nèi)置的日志工廠
Mybatis內(nèi)置的日志工廠提供日志功能,具體的日志實(shí)現(xiàn)有以下幾種工具:
slf4j
Apache Commons Logging
Log4j 2
Log4j
JDK logging
具體選擇哪個(gè)日志實(shí)現(xiàn)工具由MyBatis的內(nèi)置日志工廠確定。它會(huì)使用最先找到的(按上文列舉的順序查找)。如果一個(gè)都未找到,日志功能就會(huì)被禁用。
配置log-impl:可供選擇的有以下幾種
yml中:
mybatis-plus: configuration: log-impl: org.apache.ibatis.logging.stdout.StdOutImpl #這個(gè)是可以打印sql、參數(shù)、查詢結(jié)果的,只會(huì)打印到控制臺(tái)不會(huì)輸出到日志文件中 #org.apache.ibatis.logging.log4j.Log4jImpl:這個(gè)不打印查詢結(jié)果 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 # 重點(diǎn)
StdOutImpl控制臺(tái)輸出結(jié)果:
==> Preparing: select id, name, sex from t_user where sex = ? ==> Parameters: 0(Integer) <== Total: 8 /** *第一行是執(zhí)行的sql語句 *第二行是傳入的參數(shù) *第三行是返回結(jié)果的行數(shù) */
補(bǔ)充:
log-impl用于設(shè)置打印sql的日志實(shí)現(xiàn),指定的值為org.apache.ibatis.logging.Log接口的其中的一個(gè)實(shí)現(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就只會(huì)在控制臺(tái)窗口打印,不會(huì)記錄到日志文件。
如果需要保存打印SQL到文件就不能設(shè)置為StdOutImpl,可以設(shè)置為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); }
也可以不設(shè)置。然后對應(yīng)接口所在包設(shè)置logback對應(yīng)包的日志等級(jí)[debug\info\error\warn等],即下面所提到的第二種方式。
級(jí)別從高到低:OFF 、 FATAL 、 ERROR 、 WARN 、 INFO 、 DEBUG 、 TRACE 、 ALL
2、Spring Boot集成Mybatis
logging.file.name設(shè)置日志文件:支持絕對路徑、相對路徑,相對路徑的話是相對應(yīng)用的根目錄,比如logging.file.name=logs/demo.log;
默認(rèn):spring.log
logging.file.path設(shè)置日志路徑:支持絕對路徑、相對路徑,比如 logging.file.path=logs,日志文件默認(rèn)會(huì)存為 spring.log;
默認(rèn):根路徑/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: #加不加這個(gè)配置就看你的resources目錄下是否有l(wèi)ogbackxxxx之類的xml文件,有的話會(huì)自動(dòng)加載這個(gè),識(shí)別里面的logpath。如果外部的這個(gè)日志配置和xml文件中具有相同的配置的話,哪個(gè)優(yōu)先級(jí)更高一點(diǎn)?
此時(shí),如果有相應(yīng)的log配置文件,比如叫l(wèi)ogback-spring.xml。
配置文件中部分內(nèi)容如下:
<!--開發(fā)環(huán)境:打印控制臺(tái)--> <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標(biāo)簽指定了name,那么在application.yml文件中要啟用dev,prod,test中的一個(gè),不然輸出的內(nèi)容為空。logger標(biāo)簽中的name限定了范圍。 -->
相應(yīng)的yml中的logging配置:
logging: config: ${spring.config.location}logback-spring.xml # 不加也行,Springboot默認(rèn)的日志配置文件名稱為logback.xml和logback-spring.xml、logbackxxxx.xml spring: profiles: active: dev
3、總結(jié)
sql | 其它日志信息 | 簡述 | 配置 |
---|---|---|---|
只打印到控制臺(tái) | 不輸出 | 只打印sql到控制臺(tái) | 僅需log-impl配置為StdOutImpl |
只打印到控制臺(tái) | 輸出 | 其它日志信息保存到文件,sql只打印到控制臺(tái),不保存到文件中 | log-impl配置為StdOutImpl,再加上logging的配置(logging.config指定配置文件/ resources目錄下有符合spring boot自動(dòng)化配置文件名的logbackxxx.xml/ yml中l(wèi)ogging.level/file配置完整一些) |
打印到控制臺(tái)且輸出到日志文件中 | 輸出 | sql和其它日志信息保存到文件中,sql也打印到控制臺(tái) | log-impl為Slf4jImpl/或者不設(shè)置,總之不能設(shè)置為StdOutImpl,再加上logging的配置即可 |
配置為logback-test.xml后,控制臺(tái)的輸出:
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
配置完號(hào)后的緊接著的日志輸出:
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
可以直觀的感受到前面日志的時(shí)間格式的變化。
到此這篇關(guān)于SpringBoot打印系統(tǒng)執(zhí)行的sql語句及日志配置指南的文章就介紹到這了,更多相關(guān)SpringBoot打印系統(tǒng)執(zhí)行sql語句內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java實(shí)現(xiàn)的圖片上傳工具類完整實(shí)例
這篇文章主要介紹了Java實(shí)現(xiàn)的圖片上傳工具類,涉及java針對圖片文件的檢查、上傳、清除等相關(guān)操作技巧,需要的朋友可以參考下2017-10-10SpringBoot使用@Async注解實(shí)現(xiàn)異步調(diào)用
這篇文章主要介紹了SpringBoot使用@Async注解實(shí)現(xiàn)異步調(diào)用,異步調(diào)用是相對于同步調(diào)用而言的,同步調(diào)用是指程序按預(yù)定順序一步步執(zhí)行,每一步必須等到上一步執(zhí)行完后才能執(zhí)行,異步調(diào)用則無需等待,程序執(zhí)行完即可執(zhí)行,可以減少程序執(zhí)行時(shí)間,需要的朋友可以參考下2023-10-10SpringCloud服務(wù)接口調(diào)用OpenFeign及使用詳解
這篇文章主要介紹了SpringCloud服務(wù)接口調(diào)用——OpenFeign,在學(xué)習(xí)Ribbon時(shí),服務(wù)間調(diào)用使用的是RestTemplate+Ribbon實(shí)現(xiàn),而Feign在此基礎(chǔ)上繼續(xù)進(jìn)行了封裝,使服務(wù)間調(diào)用變得更加方便,需要的朋友可以參考下2023-04-04基于springboot微信公眾號(hào)開發(fā)(微信自動(dòng)回復(fù))
這篇文章主要介紹了基于springboot微信公眾號(hào)開發(fā)(微信自動(dòng)回復(fù)),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11SpringBoot統(tǒng)一功能處理實(shí)現(xiàn)的全過程
最近在做項(xiàng)目時(shí)需要對異常進(jìn)行全局統(tǒng)一處理,主要是一些分類入庫以及記錄日志等,下面這篇文章主要給大家介紹了關(guān)于SpringBoot統(tǒng)一功能處理實(shí)現(xiàn)的相關(guān)資料,文中通過圖文以及實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-01-01Java final 修飾符知識(shí)點(diǎn)總結(jié)(必看篇)
下面小編就為大家?guī)硪黄狫ava final 修飾符知識(shí)點(diǎn)總結(jié)(必看篇)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-09-09Java日常練習(xí)題,每天進(jìn)步一點(diǎn)點(diǎn)(8)
下面小編就為大家?guī)硪黄狫ava基礎(chǔ)的幾道練習(xí)題(分享)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧,希望可以幫到你2021-07-07