休眠-在异常日志sql和所有参数期间 [英] Hibernate - during an exception log sql and all parameters

查看:80
本文介绍了休眠-在异常日志sql和所有参数期间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当引发异常时,如何使休眠状态完全记录所有详细信息? 我想看到完整的(不被截断的)sql和完整的(不被截断的)参数.

How do I get hibernate to fully log all details when an exception is thrown? I would like to see the full (not truncated) sql and the full (not truncated) parameters.

即Hibernate抛出:

ie Hibernate is throwing:

Caused by: org.hibernate.exception.DataException: could not execute statement
long stack trace
Caused by: java.sql.SQLDataException: Incorrect string value:
Query is: blah blah blah ... 

我不想看到任何"..."那里.我想查看 full sql和 full 参数列表.

I don't want to see any "..." there. I want to see the full sql and the full parameter list.

这是一个未经检查的异常.完全记录此(以及任何其他休眠异常)所需的配置是什么?

This is an unchecked exception. What is the configuration needed for this (and any other hibernate exceptions) to be fully logged?

因为这是生产代码,所以我不想将jdbc驱动程序更改为net.sf.log4jdbc.DriverSpy

As this is production code I would prefer not changing the jdbc driver to net.sf.log4jdbc.DriverSpy

(为清楚起见,请保留截断的sql +参数)

(Left out the truncated sql + parameters for clarity)

推荐答案

您可以使用 p6spy

<dependency>
  <groupId>p6spy</groupId>
  <artifactId>p6spy</artifactId>
  <version>3.9.0</version>
</dependency>

这个想法是使用p6spy代理包装用于Hibernate的数据源,该代理会在配置时记录所有内容.

The idea is wrapping the data source you use for Hibernate with p6spy proxy which logs everything as you configure it.

如果使用Hikari池,则可能看起来像以下代码:

It may look like the following code if you use Hikari pool:

DataSource ds = new P6DataSource(new HikariDataSource(dsConfig));

p6spy的配置位于spy.properties文件中,该文件应该在您的类路径的根目录(在jar文件的根目录中)中找到.

The configuration of p6spy lays in spy.properties file which should be found in the root of your classpath (in the root of the jar file).

以下是spy.properties的示例:

# suppress inspection "UnusedProperty" for whole file
#appender=com.p6spy.engine.spy.appender.StdoutLogger
appender=com.p6spy.engine.spy.appender.Slf4JLogger
excludecategories=info,debug,result,resultset
logMessageFormat=com.p6spy.engine.spy.appender.CustomLineFormat
customLogMessageFormat=%(category) | connection %(connectionId) | \u001b[33m%(sqlSingleLine)\u001b[0m | %(executionTime) ms

如您所见,可以配置p6spy如何使用选定的附加程序写入日志.

As you can see, it is possible to configure how p6spy writes logs with the chosen appender.

如果一切配置正确,您将在日志中看到SQL查询和相应的参数.

If everything configured well, you see SQL queries and corresponding parameters in the logs.

这篇关于休眠-在异常日志sql和所有参数期间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆