0


springboot JPA日志打印

1、在springboot整合jpa后再配置文件applcation.yml的时候show-sql要设置为true

spring:
  profiles:
    active: dev
    #JPA(java持久层API)配置
  jpa:
    #指定数据库管理系统
    database: MYSQL
    #显示或者不记录每个sql查询
    show-sql: true
    hibernate:
      ddl-auto: update
      #命名策略
      naming:
        strategy: org.hibernate.cfg.ImprovedNamingStrategy
      database-platform: org.hibernate.dialect.MySQL5InnoDBDialect

这时去执行请求,后台就会打印sql,但是不会现实参数,如果需要现在参数需要再在application.yml中添加下面的配置

logging:
  level:
    org.springframework.security:- debug
      - info
    org.springframework.web: error
    org.hibernate.SQL: debug
    org.hibernate.engine.QueryParameters: debug
    org.hibernate.engine.query.HQLQueryPlan: debug
    org.hibernate.type.descriptor.sql.BasicBinder: trace

这时去执行请求,后台就会将参数和sql都打印出来,但是sql和参数是分开的。
下面我们让sql和参数合并在一起打印出来
1、第一步添加依赖

<!--监控sql日志--><dependency><groupId>org.bgee.log4jdbc-log4j2</groupId><artifactId>log4jdbc-log4j2-jdbc4.1</artifactId><version>1.16</version></dependency>

2、创建log4jdbc.log4j2.properties 配置文件,并添加以下内容

# If you use SLF4J. First, you need to tell log4jdbc-log4j2 that you want to use the SLF4J logger
log4jdbc.spylogdelegator.name=net.sf.log4jdbc.log.slf4j.Slf4jSpyLogDelegator

3、修改application.yml中的mysql连接信息
在这里插入图片描述
4、在resources 里新建 logback.xml文件

<?xml version="1.0" encoding="UTF-8"?><configurationscan="true"><!-- 从application.yml 中注入变量  --><!-- <springProperty scope="context" name="LOG_PATH" source="log.home"/> --><!-- <springProperty scope="context" name="APPDIR" source="spring.application.name"/> --><propertyname="LOG_PATH"value="./logs"/><propertyname="APPDIR"value="graceLogs"/><appendername="STDOUT"class="ch.qos.logback.core.ConsoleAppender"><encoder><pattern>1-%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger - %msg%n</pattern><charset>GBK</charset></encoder></appender><!-- error级别日志文件输出,按日期时间滚动记录输出 --><appendername="FILEERROR"class="ch.qos.logback.core.rolling.RollingFileAppender"><file>${LOG_PATH}/${APPDIR}/log_error.log</file><rollingPolicyclass="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"><fileNamePattern>${LOG_PATH}/${APPDIR}/error/log-error-%d{yyyy-MM-dd}.%i.log</fileNamePattern><timeBasedFileNamingAndTriggeringPolicyclass="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP"><maxFileSize>500MB</maxFileSize></timeBasedFileNamingAndTriggeringPolicy></rollingPolicy><append>true</append><encoderclass="ch.qos.logback.classic.encoder.PatternLayoutEncoder"><pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level %logger Line:%-3L - %msg%n</pattern><charset>utf-8</charset></encoder><filterclass="ch.qos.logback.classic.filter.LevelFilter"><level>error</level><onMatch>ACCEPT</onMatch><onMismatch>DENY</onMismatch></filter></appender><!-- warn级别日志文件输出,按日期时间滚动记录输出 --><appendername="FILEWARN"class="ch.qos.logback.core.rolling.RollingFileAppender"><file>${LOG_PATH}/${APPDIR}/log_warn.log</file><rollingPolicyclass="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"><fileNamePattern>${LOG_PATH}/${APPDIR}/warn/log-warn-%d{yyyy-MM-dd}.%i.log</fileNamePattern><timeBasedFileNamingAndTriggeringPolicyclass="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP"><maxFileSize>2MB</maxFileSize></timeBasedFileNamingAndTriggeringPolicy></rollingPolicy><append>true</append><encoderclass="ch.qos.logback.classic.encoder.PatternLayoutEncoder"><pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level %logger Line:%-3L - %msg%n</pattern><charset>utf-8</charset></encoder><filterclass="ch.qos.logback.classic.filter.LevelFilter"><level>warn</level><onMatch>ACCEPT</onMatch><onMismatch>DENY</onMismatch></filter></appender><!-- info级别日志文件输出,按日期时间滚动记录输出 --><appendername="FILEINFO"class="ch.qos.logback.core.rolling.RollingFileAppender"><file>${LOG_PATH}/${APPDIR}/log_info.log</file><rollingPolicyclass="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"><fileNamePattern>${LOG_PATH}/${APPDIR}/info/log-info-%d{yyyy-MM-dd}.%i.log</fileNamePattern><timeBasedFileNamingAndTriggeringPolicyclass="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP"><maxFileSize>2MB</maxFileSize></timeBasedFileNamingAndTriggeringPolicy></rollingPolicy><append>true</append><encoderclass="ch.qos.logback.classic.encoder.PatternLayoutEncoder"><pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level %logger Line:%-3L - %msg%n</pattern><charset>utf-8</charset></encoder><filterclass="ch.qos.logback.classic.filter.LevelFilter"><level>info</level><onMatch>ACCEPT</onMatch><onMismatch>DENY</onMismatch></filter></appender><appendername="CONSOLE"class="ch.qos.logback.core.ConsoleAppender"><encoder><pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level %logger Line:%-3L - %msg%n</pattern></encoder></appender><!--设置为OFF,即屏蔽; 留下sqltiming作为INFO级别输出--><loggername="jdbc.connection"level="OFF"/><loggername="jdbc.resultset"level="OFF"/><loggername="jdbc.resultsettable"level="OFF"/><loggername="jdbc.audit"level="OFF"/><loggername="jdbc.sqltiming"level="INFO"/><loggername="jdbc.sqlonly"level="OFF"/><!--设置日志打印级别为INFO--><rootlevel="INFO"><appender-refref="CONSOLE"/><appender-refref="FILEINFO"/><appender-refref="FILEWARN"/><appender-refref="FILEERROR"/></root></configuration>

好了现在发送请求,在控制台上看到答应的sql了


本文转载自: https://blog.csdn.net/weixin_44740485/article/details/130224451
版权归原作者 可乐丿不加冰 所有, 如有侵权,请联系我们删除。

“springboot JPA日志打印”的评论:

还没有评论