java 关闭 Hibernate c3p0 的日志记录

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

Turning off logging for Hibernate c3p0

javahibernateloggingc3p0

提问by Alex Abdugafarov

I'm using Hibernate's c3p0 connection pooling and standard Java 1.4 java.util.logging. Upon startup, my app sets up it's logging properties (including formatter and log levels) in staticblock. Every time I start my app, I see the following:

我正在使用 Hibernate 的 c3p0 连接池和标准 Java 1.4 java.util.logging。启动时,我的应用程序在static块中设置它的日志记录属性(包括格式化程序和日志级别)。每次我启动我的应用程序时,我都会看到以下内容:

2011-04-16 17-43-51 [com.mchange.v2.log.MLog] INFO: {MLog.<clinit>) MLog clients using java 1.4+ standard logging.
2011-04-16 17-43-51 [com.mchange.v2.c3p0.C3P0Registry] INFO: {C3P0Registry.banner) Initializing c3p0-0.9.1 [built 16-January-2007 14:46:42; debug? true; trace: 10]
2011-04-16 17-43-51 [com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource] INFO: {AbstractPoolBackedDataSource.getPoolManager)
...

I've tried

我试过了

Logger.getLogger("com.mchange").setLevel(Level.WARNING);
com.mchange.v2.log.MLog.getLogger().setLevel(MLevel.WARNING);
System.setProperty("com.mchange.v2.log.FallbackMLog.DEFAULT_CUTOFF_LEVEL", "WARNING");

but only way to prevent it that I found for now is

但我现在发现的唯一防止它的方法是

Logger.getLogger("").setLevel(Level.WARNING);

which affects everything - not a good side effect. Google didn't help. Could anyone help please?

这会影响一切 - 不是一个好的副作用。谷歌没有帮助。有人可以帮忙吗?

回答by Alex Abdugafarov

The way I found is to set the system property

我发现的方法是设置系统属性

System.setProperty("com.mchange.v2.log.MLog", "com.mchange.v2.log.FallbackMLog");

in addition to

此外

System.setProperty("com.mchange.v2.log.FallbackMLog.DEFAULT_CUTOFF_LEVEL", "WARNING");

I thought, that absence of any other logging system wil make that optional, but it seems, that I was wrong.

我想,没有任何其他日志系统会使它成为可选的,但似乎我错了。

P.S.

聚苯乙烯

Damn those wheel-reinvented custom logging implementations, like the one used by c3p0...

该死的那些轮子重新发明的自定义日志实现,就像 c3p0 使用的那样......

回答by Bohdan Levchenko

The way I found for achieving this

我发现实现这一目标的方式

Create in your classpath a file called mchange-log.propertiesand put into it properties suggested by Frozen Spider.

在您的类路径中创建一个名为mchange-log.properties的文件,并将 Frozen Spider 建议的属性放入其中。

com.mchange.v2.log.MLog=com.mchange.v2.log.FallbackMLog
com.mchange.v2.log.FallbackMLog.DEFAULT_CUTOFF_LEVEL=WARNING

Thats work fine even when you are not able to set system properties directly.

即使您无法直接设置系统属性,这也能正常工作。

回答by hooknc

Do you not want to see any c3p0 logging?

您不想看到任何 c3p0 日志记录吗?

If so try:

如果是这样,请尝试:

Logger.getLogger("com.mchange.v2.c3p0").setLevel(Level.WARNING);

OR, if you don't even want to see the first line of the log:

或者,如果您甚至不想看到日志的第一行:

Logger.getLogger("com.mchange.v2").setLevel(Level.WARNING);

回答by TomEE

It appears that c3p0 logging defaults to DEBUG. That can result in a lot of noise.

似乎 c3p0 日志记录默认为 DEBUG。这会导致很多噪音。

By adding a line like this to log4j.properties, you are telling the logger not to bother you with c3p0 messages - unless it's something important:

通过在 log4j.properties 中添加这样的一行,您是在告诉记录器不要用 c3p0 消息来打扰您——除非它很重要:

log4j.logger.com.mchange.v2=WARN

回答by JRSofty

This is probably really late, but according to the c3p0 project website it is possible to configure the logging inside the mchange-log.propertiesso that you can capture the information using slf4j or log4j (and thus also with Logback).

这可能真的很晚了,但是根据 c3p0 项目网站,可以在其中配置日志记录,mchange-log.properties以便您可以使用 slf4j 或 log4j(因此也可以使用 Logback)捕获信息。

The link http://www.mchange.com/projects/c3p0/#configuring_loggingprovides this information that in your mchange-log.propertiesfile set the property com.mchange.v2.log.MLogto equal com.mchange.v2.log.slf4j.Slf4jMLogthen in your logback.xmlyou can provide a logger like this:

链接http://www.mchange.com/projects/c3p0/#configuring_logging提供了此信息,在您的mchange-log.properties文件中将属性设置com.mchange.v2.log.MLog为相等,com.mchange.v2.log.slf4j.Slf4jMLog然后logback.xml您可以提供这样的记录器:

<logger name="com.mchange" level="warn" additivity="false">
    <appender-ref ref="c3p0-log" />
</logger>

Note: you will need to create a logback appender called c3p0-log before you can use this exact piece of code.

注意:在使用这段代码之前,您需要创建一个名为 c3p0-log 的 logback appender。

回答by Dirk

This only happens on older c3p0 version. So it might also be worth checking if you can just update to a newer version.

这仅发生在较旧的 c3p0 版本上。因此,如果您可以更新到较新的版本,也可能值得检查。

回答by Anantha Sharma

create a file called log4j.properties in your root classpath set the following in there,

在根类路径中创建一个名为 log4j.properties 的文件,在其中设置以下内容,

# Configure the name of the file for the LOGGER appender
log4j.appender.LOGGER=org.apache.log4j.ConsoleAppender
log4j.appender.LOGGER.layout=org.apache.log4j.PatternLayout
log4j.appender.LOGGER.layout.ConversionPattern=%d{MM-dd@HH:mm:ss} %-5p (%13F:%L) %3x - %m%n
log4j.appender.LOGGER.append=false

# this line logs everything from hibernate package at info level, you can refine this to include only some pachages like log4j.logger.org.hibernate.hql etc.,
log4j.logger.org.hibernate=INFO, LOGGER

log4j.logger.org.jboss.cache=INFO, LOGGER

this is a much better way of implementing the logging because if you set the logging strategy programmatically, then the config sometimes might not take effect at all (like in your case).. if you use the log4j.properties file ,the config is applied at application startup & everything works smoothly.

这是实现日志记录的更好方法,因为如果您以编程方式设置日志记录策略,那么配置有时可能根本不会生效(就像您的情况一样)..如果您使用 log4j.properties 文件,则应用配置在应用程序启动时一切正常。