0


SpringBoot打印系统执行的sql语句以及日志的配置

SpringBoot打印系统执行的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开发场景所需的底层所有依赖,springboot默认集成了tomcat服务器 --></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-logging</artifactId><!-- 点进去里面有logback、log4j和slf4j --><!--  如果添加了这个依赖,SpringBoot 应用将自动使用 logback 作为应用日志框架 --></dependency></dependencies>

1、Mybatis内置的日志工厂

Mybatis内置的日志工厂提供日志功能,具体的日志实现有以下几种工具:
slf4j
Apache Commons Logging
Log4j 2
Log4j
JDK logging
具体选择哪个日志实现工具由MyBatis的内置日志工厂确定。它会使用最先找到的(按上文列举的顺序查找)。如果一个都未找到,日志功能就会被禁用。

配置log-impl:可供选择的有以下几种
在这里插入图片描述

yml中:

mybatis-plus:configuration:log-impl: org.apache.ibatis.logging.stdout.StdOutImpl #这个是可以打印sql、参数、查询结果的,只会打印到控制台不会输出到日志文件中#org.apache.ibatis.logging.log4j.Log4jImpl:这个不打印查询结果mapper-locations: classpath:mapper/*.xmltype-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/**
 *第一行是执行的sql语句
 *第二行是传入的参数
 *第三行是返回结果的行数
 */
补充


log-impl用于设置打印sql的日志实现,指定的值为org.apache.ibatis.logging.Log接口的其中的一个实现类
在这里以StdOutImpl为例

publicclassStdOutImplimplementsLog{publicStdOutImpl(String clazz){// Do Nothing}@OverridepublicbooleanisDebugEnabled(){returntrue;}@OverridepublicbooleanisTraceEnabled(){returntrue;}@Overridepublicvoiderror(String s){System.err.println(s);}@Overridepublicvoiddebug(String s){System.out.println(s);}@Overridepublicvoidtrace(String s){System.out.println(s);}@Overridepublicvoidwarn(String s){System.out.println(s);}}

方法里面的输出,是用的System.out/error.println方法,所以如果配置为org.apache.ibatis.logging.stdout.StdOutImpl就只会在控制台窗口打印,不会记录到日志文件。
如果需要保存打印SQL到文件就不能设置为StdOutImpl,可以设置为Slf4jImpl。部分代码如下,使用的是Log的方法,保存到文件中。

@Overridepublicvoiderror(String s){
    log.error(s);}@Overridepublicvoiddebug(String s){
    log.debug(s);}@Overridepublicvoidtrace(String s){
    log.trace(s);}@Overridepublicvoidwarn(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的方式打印sqllogging:level:com.xxx.mapper: debug # 包路径为mapper文件包路径org.springframework: warn
      org.apache.ibatis.logging: debug
    file:path/name:#加不加这个配置就看你的resources目录下是否有logbackxxxx之类的xml文件,有的话会自动加载这个,识别里面的logpath。如果外部的这个日志配置和xml文件中具有相同的配置的话,哪个优先级更高一点?

此时,如果有相应的log配置文件,比如叫logback-spring.xml。
配置文件中部分内容如下:

<!--开发环境:打印控制台--><springProfilename="dev,prod,test"><loggername="com.example.plan"level="info"/></springProfile><rootlevel="info"><appender-refref="CONSOLE"/><appender-refref="INFO_FILE"/><appender-refref="WARNING_FILE"/><appender-refref="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.xmlspring:profiles:active: dev

3、总结

sql其它日志信息简述配置只打印到控制台不输出只打印sql到控制台仅需log-impl配置为StdOutImpl只打印到控制台输出其它日志信息保存到文件,sql只打印到控制台,不保存到文件中log-impl配置为StdOutImpl,再加上logging的配置(logging.config指定配置文件/ resources目录下有符合spring boot自动化配置文件名的logbackxxx.xml/ yml中logging.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

可以直观的感受到前面日志的时间格式的变化。

标签: spring boot sql mybatis

本文转载自: https://blog.csdn.net/qq_45722699/article/details/126782288
版权归原作者 Congee_porridge 所有, 如有侵权,请联系我们删除。

“SpringBoot打印系统执行的sql语句以及日志的配置”的评论:

还没有评论