首页 > 解决方案 > Hibernate 查询日志记录 - 从实体名称记录

问题描述

我通过在我的 application.properties 中设置以下配置在 Hibernate 中使用查询日志记录

logging.level.org.springframework.transaction.interceptor=DEBUG
logging.level.org.hibernate.SQL=DEBUG
logging.level.org.hibernate.type.descriptor.sql.BasicBinder=DEBUG

这很好用并记录查询。我需要通过在开头添加一个表名来增强此日志记录,我从哪个表中选择数据。例如,我记录了以下查询:

2021-06-16 13:11:18.008 DEBUG 6544 --- [io-8080-exec-10] org.hibernate.SQL : select count(program0_.id) as col_0_0_ from public.program program0_ where 1=1
2021-06-16 13:11:18.008 DEBUG 6544 --- [io-8080-exec-10] org.hibernate.SQL : select count(user_0 .id) as col_0_0_ from public.user user_0 where 1=1

这些查询很简单,很容易确定是从表programuser. 但是在大查询的情况下,开头的表名会简化日志的检查。

是否有任何选项如何在 Hibernate 中进行设置?

标签: javaspringhibernatespring-data-jpaspring-data

解决方案


  1. Hibernate 没有这样的选项。

  2. 评论已经描述了需求定义不明确,因为您可以在任何 SQL 语句中拥有任意数量的表,包括 0,因此通用解决方案相当棘手。

  3. 但是你可以自己做这样的事情。

    例如,如果您使用 Logback,您可能会注册一个解析消息的自定义布局,如果它找到 SQL 语句,它会从中提取表。

    或者,您可以使用您用来收集和整合日志的工具链对日志进行后处理


推荐阅读