Java 关闭休眠日志记录控制台输出

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

Turning off hibernate logging console output

javahibernatelogging

提问by Jared

I'm using hibernate 3 and want to stop it from dumping all the startup messages to the console. I tried commenting out the stdout lines in log4j.properties but no luck. I've pasted my log file below. Also I'm using eclipse with the standard project structure and have a copy of log4j.properties in both the root of the project folder and the bin folder.

我正在使用 hibernate 3 并希望阻止它将所有启动消息转储到控制台。我尝试注释掉 log4j.properties 中的 stdout 行,但没有运气。我在下面粘贴了我的日志文件。此外,我将 eclipse 与标准项目结构一起使用,并且在项目文件夹的根目录和 bin 文件夹中都有 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

### direct messages to file hibernate.log ###
log4j.appender.file=org.apache.log4j.FileAppender
log4j.appender.file.File=hibernate.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n

### set log levels - for more verbose logging change 'info' to 'debug' ###

log4j.rootLogger=warn, stdout

#log4j.logger.org.hibernate=info
log4j.logger.org.hibernate=debug

### log HQL query parser activity
#log4j.logger.org.hibernate.hql.ast.AST=debug

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

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

### log schema export/update ###
log4j.logger.org.hibernate.tool.hbm2ddl=debug

### log HQL parse trees
#log4j.logger.org.hibernate.hql=debug

### log cache activity ###
#log4j.logger.org.hibernate.cache=debug

### log transaction activity
#log4j.logger.org.hibernate.transaction=debug

### log JDBC resource acquisition
#log4j.logger.org.hibernate.jdbc=debug

### enable the following line if you want to track down connection ###
### leakages when using DriverManagerConnectionProvider ###
#log4j.logger.org.hibernate.connection.DriverManagerConnectionProvider=trac5

采纳答案by Juha Syrj?l?

Try to set more reasonable logging level. Setting logging level to infomeans that only log event at infoor higher level (warn, errorand fatal) are logged, that is debuglogging events are ignored.

尝试设置更合理的日志级别。将日志级别设置为info意味着仅记录处于info或更高级别 ( warn,errorfatal) 的debug日志事件,即忽略日志事件。

log4j.logger.org.hibernate=info

or in XML versionof log4j config file:

或在log4j 配置文件的XML 版本中:

<logger name="org.hibernate">
  <level value="info"/> 
</logger>

See also log4j manual.

另请参阅log4j 手册

回答by rresino

You can disabled the many of the outputs of hibernate setting this props of hibernate (hb configuration) a false:

您可以禁用休眠的许多输出,将休眠的这个道具(hb 配置)设置为 false:

hibernate.show_sql
hibernate.generate_statistics
hibernate.use_sql_comments

But if you want to disable all console info you must to set the logger level a NONE of FATAL of class org.hibernatelike Juha say.

但是如果你想禁用所有的控制台信息,你必须org.hibernate像 Juha 说的那样将记录器级别设置为 NONE of FATAL 。

回答by user370677

I changed the "debug" to "info" and it worked. Here is what I did:

我将“调试”更改为“信息”并且它起作用了。这是我所做的:

Before:

前:

log4j.rootLogger=debug, stdout, R

After:

后:

log4j.rootLogger=info, stdout, R 

回答by Liqun Chen

I finally figured out, it's because the Hibernate is using slf4j log facade now, to bridge to log4j, you need to put log4j and slf4j-log4j12 jars to your lib and then the log4j properties will take control Hibernate logs.

我终于想通了,这是因为 Hibernate 现在正在使用 slf4j 日志外观,要桥接到 log4j,您需要将 log4j 和 slf4j-log4j12 jar 放入您的库,然后 log4j 属性将控制 Hibernate 日志。

My pom.xml setting looks as below:

我的 pom.xml 设置如下所示:

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

    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
        <version>1.6.4</version>
    </dependency>

回答by Systfile

For disabling Hibernate:selectmessage in log, it is possible to set the property into HibernateJpaVendorAdapter:

要禁用Hibernate:select日志中的消息,可以将属性设置为HibernateJpaVendorAdapter

<bean id="jpaVendorAdapter"
    class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
    <property name="showSql" value="false"/>
</bean> 

回答by acdcjunior

Executing:

执行:

java.util.logging.Logger.getLogger("org.hibernate").setLevel(Level.OFF);

before hibernate's initialization worked for me.

在休眠的初始化对我有用之前。



Note:the line above will turn everylogging off (Level.OFF). If you want to be less strict, you can use

注意:上面的行将关闭所有日志记录 ( Level.OFF)。如果你想不那么严格,你可以使用

java.util.logging.Logger.getLogger("org.hibernate").setLevel(Level.SEVERE);

that is silent enough. (Or check the java.util.logging.Levelclass for more levels).

这已经足够沉默了。(或查看java.util.logging.Level课程以了解更多级别)。

回答by user1050755

Important notice: the property (part of hibernate configuration, NOT part of logging framework config!)

重要提示:属性(休眠配置的一部分,而不是日志框架配置的一部分!)

hibernate.show_sql

controls the logging directlyto STDOUT bypassing any logging framework (which you can recognize by the missing output formatting of the messages). If you use a logging framework like log4j, you should alwaysset that property to falsebecause it gives you no benefit at all.

将日志记录直接控制到 STDOUT,绕过任何日志记录框架(您可以通过缺少消息的输出格式来识别)。如果您使用 log4j 之类的日志记录框架,则应始终将该属性设置为 false,因为它根本没有给您带来任何好处。

That circumstance irritated me quite a long time because I never really cared about it until I tried to write some benchmark regarding Hibernate.

这种情况激怒了我很长一段时间,因为我从来没有真正关心过它,直到我尝试编写一些关于 Hibernate 的基准测试。

回答by Matej Vargov?ík

For those who don't want elegant solutions, just a quick and dirty way to stop those messages, here is a solution that worked for me (I use Hibernate 4.3.6 and Eclipse and no answers provided above (or found on the internet) worked; neither log4j config files nor setting the logging level programatically)

对于那些不想要优雅的解决方案的人,只是一种快速而肮脏的方式来阻止这些消息,这是一个对我有用的解决方案(我使用 Hibernate 4.3.6 和 Eclipse,上面没有提供答案(或在互联网上找到)有效;既没有 log4j 配置文件,也没有以编程方式设置日志记录级别)

public static void main(String[] args) {
    //magical - do not touch
    @SuppressWarnings("unused")
    org.jboss.logging.Logger logger = org.jboss.logging.Logger.getLogger("org.hibernate");
    java.util.logging.Logger.getLogger("org.hibernate").setLevel(java.util.logging.Level.WARNING); //or whatever level you need

    ...
}

I used it in a tutorial program downloaded from this site

我在从该站点下载的教程程序中使用了它

回答by Balakrishna

Replace slf4j-jdk14-xxx.jar with slf4j-log4j12-xxx.jar. If you have both, delete slf4j-jdk14-xxx.jar. Found this solution at https://forum.hibernate.org/viewtopic.php?f=1&t=999623

将 slf4j-jdk14-xxx.jar 替换为 slf4j-log4j12-xxx.jar。如果两者都有,请删除 slf4j-jdk14-xxx.jar。在https://forum.hibernate.org/viewtopic.php?f=1&t=999623找到了这个解决方案

回答by Ramesh Gowtham

To get rid of logger output in console try this.

要摆脱控制台中的记录器输出,请尝试此操作。

ch.qos.logback.classic.LoggerContext.LoggerContext loggerContext = (LoggerContext) org.slf4j.LoggerFactory.LoggerFactory.getILoggerFactory();

loggerContext.stop();

These statements disabled all the console outputs from logger.

这些语句禁用了记录器的所有控制台输出。