欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringBoot打印系統(tǒng)執(zhí)行的sql語句及日志配置指南

 更新時間:2023年10月25日 09:02:30   作者:Congee_porridge  
這篇文章主要給大家介紹了關于SpringBoot打印系統(tǒng)執(zhí)行的sql語句及日志配置的相關資料,在Java SpringBoot項目中如果使用了Mybatis框架,默認情況下執(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內(nèi)置的日志工廠

Mybatis內(nèi)置的日志工廠提供日志功能,具體的日志實現(xiàn)有以下幾種工具:
slf4j
Apache Commons Logging
Log4j 2
Log4j
JDK logging
具體選擇哪個日志實現(xiàn)工具由MyBatis的內(nèi)置日志工廠確定。它會使用最先找到的(按上文列舉的順序查找)。如果一個都未找到,日志功能就會被禁用。

配置log-impl:可供選擇的有以下幾種

在這里插入圖片描述

yml中:

mybatis-plus:
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl #這個是可以打印sql、參數(shù)、查詢結(jié)果的,只會打印到控制臺不會輸出到日志文件中
			 #org.apache.ibatis.logging.log4j.Log4jImpl:這個不打印查詢結(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 # 重點

StdOutImpl控制臺輸出結(jié)果:

==>  Preparing: select id, name, sex from t_user where sex = ?
==> Parameters: 0(Integer)
<==      Total: 8

/**
 *第一行是執(zhí)行的sql語句
 *第二行是傳入的參數(shù)
 *第三行是返回結(jié)果的行數(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。

配置文件中部分內(nèi)容如下:

	<!--開發(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中的一個,不然輸出的內(nèi)容為空。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、總結(jié)

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語句內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • Java實現(xiàn)的圖片上傳工具類完整實例

    Java實現(xiàn)的圖片上傳工具類完整實例

    這篇文章主要介紹了Java實現(xiàn)的圖片上傳工具類,涉及java針對圖片文件的檢查、上傳、清除等相關操作技巧,需要的朋友可以參考下
    2017-10-10
  • java 非對稱加密算法DH實現(xiàn)詳解

    java 非對稱加密算法DH實現(xiàn)詳解

    這篇文章主要介紹了java 非對稱加密算法DH實現(xiàn)詳解 ,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2019-07-07
  • SpringBoot使用@Async注解實現(xiàn)異步調(diào)用

    SpringBoot使用@Async注解實現(xiàn)異步調(diào)用

    這篇文章主要介紹了SpringBoot使用@Async注解實現(xiàn)異步調(diào)用,異步調(diào)用是相對于同步調(diào)用而言的,同步調(diào)用是指程序按預定順序一步步執(zhí)行,每一步必須等到上一步執(zhí)行完后才能執(zhí)行,異步調(diào)用則無需等待,程序執(zhí)行完即可執(zhí)行,可以減少程序執(zhí)行時間,需要的朋友可以參考下
    2023-10-10
  • Springboot消除switch-case過程解析

    Springboot消除switch-case過程解析

    這篇文章主要介紹了Springboot消除switch-case過程解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2019-10-10
  • SpringCloud服務接口調(diào)用OpenFeign及使用詳解

    SpringCloud服務接口調(diào)用OpenFeign及使用詳解

    這篇文章主要介紹了SpringCloud服務接口調(diào)用——OpenFeign,在學習Ribbon時,服務間調(diào)用使用的是RestTemplate+Ribbon實現(xiàn),而Feign在此基礎上繼續(xù)進行了封裝,使服務間調(diào)用變得更加方便,需要的朋友可以參考下
    2023-04-04
  • 基于springboot微信公眾號開發(fā)(微信自動回復)

    基于springboot微信公眾號開發(fā)(微信自動回復)

    這篇文章主要介紹了基于springboot微信公眾號開發(fā)(微信自動回復),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-11-11
  • SpringBoot統(tǒng)一功能處理實現(xiàn)的全過程

    SpringBoot統(tǒng)一功能處理實現(xiàn)的全過程

    最近在做項目時需要對異常進行全局統(tǒng)一處理,主要是一些分類入庫以及記錄日志等,下面這篇文章主要給大家介紹了關于SpringBoot統(tǒng)一功能處理實現(xiàn)的相關資料,文中通過圖文以及實例代碼介紹的非常詳細,需要的朋友可以參考下
    2023-01-01
  • Java final 修飾符知識點總結(jié)(必看篇)

    Java final 修飾符知識點總結(jié)(必看篇)

    下面小編就為大家?guī)硪黄狫ava final 修飾符知識點總結(jié)(必看篇)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2016-09-09
  • Java十道入門易踩坑題分析后篇

    Java十道入門易踩坑題分析后篇

    這篇文章總結(jié)分析了Java入門容易碰到的幾點易進坑的題目,對于新手小白剛開始學Java非常有益處,讓你少走避開彎路,感興趣的朋友快來看看吧
    2022-01-01
  • Java日常練習題,每天進步一點點(8)

    Java日常練習題,每天進步一點點(8)

    下面小編就為大家?guī)硪黄狫ava基礎的幾道練習題(分享)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧,希望可以幫到你
    2021-07-07

最新評論