Java 无法阻止 Hibernate 将日志写入控制台(log4j.properties 可以)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2077377/
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-13 03:17:59  来源:igfitidea点击:

Can't stop Hibernate from writing log to console (log4j.properties is ok)

javahibernateorm

提问by dmitrynikolaev

I've already set

我已经设置了

<property name="show_sql">false</property>

and I have disabled all messages in log4j.properties

我已经禁用了 log4j.properties 中的所有消息

But Hibernate write to console with all queries & statements.

但是 Hibernate 将所有查询和语句写入控制台。

采纳答案by Pascal Thivent

Setting the hibernate.show_sqlto truetells hibernate to Write all SQL statements to console. This is an alternative to setting the log category org.hibernate.SQLto debug.

设置hibernate.show_sqltotrue告诉 hibernate 将所有 SQL 语句写入控制台。这是将日志类别设置org.hibernate.SQL为调试的替代方法。

So even if you set this property to false, make sure that you don't have the following category defined (or configured to use a console appender):

因此,即使您将此属性设置为false,请确保您没有定义以下类别(或配置为使用控制台附加程序):

log4j.logger.org.hibernate.SQL=DEBUG

Also, make sure that you don't set the hibernate.show_sqlprogrammatically to true when instancing your Configurationobject. Hunt something like this:

此外,请确保hibernate.show_sql在实例化Configuration对象时不要以编程方式将设置为 true 。狩猎这样的东西:

Configuration cfg = new Configuration().configure().
    .setProperty("hibernate.show_sql", "true");

Note that the setProperty(String propertyName, String value)takes as first parameter the full name of a configuration property i.e. hibernate.show_sql, not just show_sql.

请注意, 将setProperty(String propertyName, String value)配置属性的全名作为第一个参数,即hibernate.show_sql,不仅仅是show_sql.

回答by Droo

You adding something like this to log4j.properties?

您在 log4j.properties 中添加了类似的内容吗?

log4j.logger.org.hibernate = WARN

回答by dmitrynikolaev

It seems very strange, but when Use Eclipse with JUnit Test task, all output still goes to Console (in Eclipse Console window).

看起来很奇怪,但是当使用 Eclipse 和 JUnit 测试任务时,所有输出仍然到控制台(在 Eclipse 控制台窗口中)。

But when I use ant command for unit testing, nothing goes to console.

但是当我使用 ant 命令进行单元测试时,控制台没有任何反应。

回答by MadMad666

HI there, i figured out you can solve this also with this 2 lines in your log4j.properties file.

你好,我发现你也可以用 log4j.properties 文件中的这 2 行来解决这个问题。

log4j.logger.org.hibernate = WARN
log4j.logger.org.hibernate = ERROR

回答by cgalev

As mentioned before, you need first to set hibernate.show_sqlto false. This will prevent hibernate output to console or Tomcat stdout log.

如前所述,您首先需要设置hibernate.show_sqlfalse. 这将阻止休眠输出到控制台或 Tomcat 标准输出日志。

But in order to direct your sql output to another log use this code in log4j.xml file:

但是为了将您的 sql 输出定向到另一个日志,请在 log4j.xml 文件中使用以下代码:

<logger name="org.hibernate.SQL" additivity="false">
    <level value="DEBUG" />
    <appender-ref ref="hibernate"/>
</logger>

To keep the normal hibernate log level (WARN / ERROR) to console use this code in log4j.xml file:

要保持正常的休眠日志级别(警告/错误)到控制台,请在 log4j.xml 文件中使用以下代码:

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

回答by karan

Its simple Just Add dependency to your pom.xml file

它很简单,只需将依赖项添加到您的 pom.xml 文件中

<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>

And it will disable logging and add put in src/main/resources/log4j.properties file

它将禁用日志记录并在 src/main/resources/log4j.properties 文件中添加 put

log4j.rootLogger=OFF

Then you can use it anytime if you want to display log on console then On it or else OFF

然后你可以随时使用它,如果你想在控制台上显示登录然后在它或者关闭