Java 如何从 Hibernate 获取更多调试消息?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1380898/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 11:24:22  来源:igfitidea点击:

How do I get more debug messages from Hibernate?

javahibernatedebuggingjbosslog4j

提问by Matt Ball

I haven't been able to get any more console output (to help with debugging) from Hibernate despite setting a few properties in the hibernate.cfg.xmlfile. For example, adding the line <property name="show_sql">true</property>did not, in fact, show SQL statements in the console.

尽管在hibernate.cfg.xml文件中设置了一些属性,但我无法从 Hibernate 获得更多控制台输出(以帮助调试)。例如,添加该行<property name="show_sql">true</property>实际上并没有在控制台中显示 SQL 语句。

I've also tried playing around with the contents of the log4j.propertiesfile - like setting log4j.logger.org.hibernate=debug- with no luck. What am I missing?

我也试过玩弄log4j.properties文件的内容——比如设置log4j.logger.org.hibernate=debug——但没有运气。我错过了什么?



Edit:the contents of the hibernate-service.xml file are

编辑:hibernate-service.xml 文件的内容是

<server> 
    <mbean code="org.jboss.hibernate.jmx.Hibernate" 
       name="jboss.har:service=Hibernate_SMS"> 
        <attribute name="DatasourceName">java:/SMS_DS</attribute> 
        <attribute name="Dialect">org.hibernate.dialect.HSQLDialect</attribute> 
        <attribute name="SessionFactoryName">java:/hibernate/SessionFactory</attribute> 
        <attribute name="CacheProviderClass">org.hibernate.cache.HashtableCacheProvider</attribute>
        <attribute name="ShowSqlEnabled">true</attribute>
    </mbean> 
</server>

I'm not 100% sure if this actually has any effect, though. That XML file is in the Eclipse project that handles my database stuff, but doesn't seem to be in the JBoss deploy directory.

不过,我不能 100% 确定这是否真的有任何影响。该 XML 文件位于处理我的数据库内容的 Eclipse 项目中,但似乎不在 JBoss 部署目录中。



Edit 2:This is definitely deployed as a HAR. That said, I'm pretty sure that I need hibernate.cfg.xml- I recall having problems when a mapping document was omitted as an entry in that file. I think the HAR is generated using ant - there's a target for it in the build.xml file:

编辑 2:这绝对是作为 HAR 部署的。也就是说,我很确定我需要hibernate.cfg.xml- 我记得当映射文档作为该文件中的条目被省略时遇到了问题。我认为 HAR 是使用 ant 生成的 - 在 build.xml 文件中有一个目标:

<target name="har" depends="prepare" description="Builds the Hibernate HAR file">
    <mkdir dir="${class.root}" />
    <mkdir dir="${distribution.dir}" />

    <jar destfile="${distribution.dir}/${har.name}">                    
        <!-- include the hbm.xml files  -->
        <fileset dir="${class.root}">
            <include name="**/*.hbm.xml"/>
            <include name="com/[redacted]/sms/data/dto/*.class"/>
            <include name="com/[redacted]/sms/data/dto/base/*.class"/>
        </fileset>

        <!-- include jboss-service.xml -->
        <metainf dir="${hibernate.dir}">
            <include name="hibernate-service.xml"/>
        </metainf>
    </jar>
</target>

but when I do the ant build with incorrectly-formed xml in the hibernate-service.xml file, it doesn't fail. UpdateEven deleting the file entirely doesn't cause a build to fail. Any ideas here? End Update

但是当我在 hibernate-service.xml 文件中使用格式不正确的 xml 进行 ant 构建时,它不会失败。更新即使完全删除文件也不会导致构建失败。这里有什么想法吗?结束更新

It sounds like getting Hibernate to output SQL statements should (if everything's set up correctly) take care of it entirely - does that mean that the settings in log4j.properties won't make a difference here? - because that actually doeschange the output when running ant.

听起来让 Hibernate 输出 SQL 语句应该(如果一切设置正确)完全处理它 - 这是否意味着 log4j.properties 中的设置在这里不会产生影响?- 因为这实际上确实会在运行 ant 时改变输出。

Edit 3:After running into other weird issues with my data har, I deleted the har file entirely and rebuilt it using the harant build target. Suddenly everything's working! Thanks to chessplay for his awesome helpfulness. Can't say I know exactly what did it in the end, but I'd rather acknowledge his efforts than write and accept my own answer; My apologies if you're not a "he."

编辑 3:在我的数据har遇到其他奇怪的问题后,我完全删除了 har 文件并使用harant 构建目标重建它。突然间一切正常!感谢 chessplay 的出色帮助。不能说我确切地知道最后做了什么,但我宁愿承认他的努力也不愿写下并接受我自己的答案;如果你不是“他”,我很抱歉。

采纳答案by ChssPly76

The correct property name is hibernate.show_sqland it definitely works.

正确的属性名称是hibernate.show_sql并且它绝对有效。

Make sure that your hibernate.cfg.xmlis the right one and it is getting picked up; same goes for your log4j.propertiesfile. I know those seem like dumb suggestions, but they account for 98% of "missing logging output" problems.

确保您选择的hibernate.cfg.xml是正确的,并且正在被取走;您的log4j.properties文件也是如此。我知道那些看起来很愚蠢的建议,但它们占“缺少日志输出”问题的 98%。

Update: I'll update the answer rather than keep adding comments. You do need to make sure your hibernate-service.xmlis picked up by JBoss. The easy way to check is to add a syntax error to it (like omit a closing brace on some element) and see if JBoss blows up during restart :-) It should be packaged into harand deployed as part of your build process, so if you realize that it's notbeing picked up by JBoss, that's the place to look. I would get rid of conflicting settings in hibernate.cfg.xml(e.g. everything that you're specifying as mbean attributes should not be replicated in hibernate.cfg.xml). For that matter, do you even need hibernate.cfg.xml? If deployed as HAR, your mappings should be automatically scanned / picked up.

更新:我会更新答案而不是继续添加评论。您确实需要确保hibernate-service.xmlJBoss 接收了您的信息。检查的简单方法是向其中添加语法错误(例如在某些元素上省略右括号)并查看 JBoss 在重新启动期间是否爆炸:-) 它应该har作为构建过程的一部分打包和部署,因此如果您会意识到JBoss并没有发现它,而这正是需要查看的地方。我会摆脱冲突的设置hibernate.cfg.xml(例如,您指定为 mbean 属性的所有内容都不应在 中复制hibernate.cfg.xml)。就此而言,你甚至需要hibernate.cfg.xml吗?如果部署为HAR,则应自动扫描/提取您的映射。

回答by jwaddell

I just use the log4j.properties file to log Hibernate statements. For SQL statements, the log4j.logger.org.hibernate.SQLproperty needs to be set to debug, and for SQL parameters/returned values the log4j.logger.org.hibernate.typeproperty needs to be set to trace.

我只是使用 log4j.properties 文件来记录 Hibernate 语句。对于 SQL 语句,log4j.logger.org.hibernate.SQL属性需要设置为debug,对于 SQL 参数/返回值,log4j.logger.org.hibernate.type属性需要设置为trace

Here is the log4j.properties file I use:

这是我使用的 log4j.properties 文件:

### direct log messages to stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n

### Root logger
log4j.rootLogger=debug, stdout

### Main Hibernate
log4j.logger.org.hibernate=debug

### log just the SQL
log4j.logger.org.hibernate.SQL=debug

### log JDBC bind parameters
log4j.logger.org.hibernate.type=trace

回答by mafro

Also make shure you're using the correct log4j-configuration.

还要确保您使用的是正确的 log4j 配置。

When running in jboss (you are doing that, right?), you configure log4j-logging in $JBOSS_HOME/server/<config>/conf/jboss-log4j.xml.

在 jboss 中运行时(你正在这样做,对吗?),你配置 log4j-logging in $JBOSS_HOME/server/<config>/conf/jboss-log4j.xml

There are two default appenders; FILEwrites to log/server.logand CONSOLEto stdout(sometimes redirected log/console.log). Adjust threshold on the appender to DEBUGin the file or by setting the systemproperty jboss.server.log.threshold(depends on which version of jboss you are using).

有两个默认的appender;FILE写入log/server.logCONSOLEstdout(有时重定向log/console.log)。将 appender 上的阈值调整到DEBUG文件中或通过设置系统属性jboss.server.log.threshold(取决于您使用的 jboss 版本)。

You'll also need to adjust the logging priority of hibernate by adding a category:

您还需要通过添加类别来调整休眠的日志记录优先级:

<category name="org.hibernate">
  <priority value="DEBUG"/>
</category>