我可以禁止 c3p0 记录 System.err 吗?

发布于 2024-11-26 11:34:52 字数 4183 浏览 1 评论 0原文

根据c3p0文档,您可以手动指定日志应该去哪里,是否通过JDK 1.4日志记录,Log4j,或通过 System.out。我正在运行 SLF4J,因此我在应用程序中包含了 org.slf4j.jul-to-slf4j 并调用 SLF4JBridgeHandler.install() 来强制所有 Java util 日志记录通过SLF4J。此外,我在 c3p0.properties 文件中包含了以下属性:

com.mchange.v2.log.MLog = com.mchange.v2.log.jdk14logging.Jdk14MLog

根据文档,这将强制 c3p0 记录到 JDK 1.4 日志记录,而 JDK 1.4 日志记录又会将内容记录到 SLF4J。这确实有点作用,但我仍然看到一些日志命中 System.err

示例 1:

17:24:32.648 [main] INFO  com.mchange.v2.log.MLog - MLog clients using java 1.4+ standard logging.
Jul 27, 2011 5:24:32 PM com.mchange.v2.log.MLog <clinit>
INFO: MLog clients using java 1.4+ standard logging.
Jul 27, 2011 5:24:32 PM com.mchange.v2.c3p0.C3P0Registry banner
INFO: Initializing c3p0-0.9.1.2 [built 21-May-2007 15:04:56; debug? true; trace: 10]
17:24:32.754 [main] INFO  com.mchange.v2.c3p0.C3P0Registry - Initializing c3p0-0.9.1.2 [built 21-May-2007 15:04:56; debug? true; trace: 10]

上面的第 1 行和第 6 行写入 SLF4J,其他写入 System.err< /代码>。

示例 2:

Jul 27, 2011 5:24:33 PM com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource getPoolManager
INFO: Initializing c3p0 pool... com.mchange.v2.c3p0.ComboPooledDataSource [ acquireIncrement -> 3, acquireRetryAttempts -> 30, acquireRetryDelay -> 1000, autoCommitOnClose -> false, automaticTestTable -> null, breakAfterAcquireFailure -> false, checkoutTimeout -> 0, connectionCustomizerClassName -> null, connectionTesterClassName -> com.mchange.v2.c3p0.impl.DefaultConnectionTester, dataSourceName -> z8kflt8huk1hupkhyfms|13b33a0e, debugUnreturnedConnectionStackTraces -> false, description -> null, driverClass -> org.sqlite.JDBC, factoryClassLocation -> null, forceIgnoreUnresolvedTransactions -> false, identityToken -> z8kflt8huk1hupkhyfms|13b33a0e, idleConnectionTestPeriod -> 0, initialPoolSize -> 3, jdbcUrl -> jdbc:sqlite:/tmp/floodstream.db, maxAdministrativeTaskTime -> 0, maxConnectionAge -> 0, maxIdleTime -> 1800, maxIdleTimeExcessConnections -> 0, maxPoolSize -> 10, maxStatements -> 0, maxStatementsPerConnection -> 0, minPoolSize -> 3, numHelperThreads -> 3, numThreadsAwaitingCheckoutDefaultUser -> 0, preferredTestQuery -> null, properties -> {user=******, password=******}, propertyCycle -> 0, testConnectionOnCheckin -> false, testConnectionOnCheckout -> false, unreturnedConnectionTimeout -> 0, usesTraditionalReflectiveProxies -> false ]
17:24:33.603 [main] INFO  com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource - Initializing c3p0 pool... com.mchange.v2.c3p0.ComboPooledDataSource [ acquireIncrement -> 3, acquireRetryAttempts -> 30, acquireRetryDelay -> 1000, autoCommitOnClose -> false, automaticTestTable -> null, breakAfterAcquireFailure -> false, checkoutTimeout -> 0, connectionCustomizerClassName -> null, connectionTesterClassName -> com.mchange.v2.c3p0.impl.DefaultConnectionTester, dataSourceName -> z8kflt8huk1hupkhyfms|13b33a0e, debugUnreturnedConnectionStackTraces -> false, description -> null, driverClass -> org.sqlite.JDBC, factoryClassLocation -> null, forceIgnoreUnresolvedTransactions -> false, identityToken -> z8kflt8huk1hupkhyfms|13b33a0e, idleConnectionTestPeriod -> 0, initialPoolSize -> 3, jdbcUrl -> jdbc:sqlite:/tmp/floodstream.db, maxAdministrativeTaskTime -> 0, maxConnectionAge -> 0, maxIdleTime -> 1800, maxIdleTimeExcessConnections -> 0, maxPoolSize -> 10, maxStatements -> 0, maxStatementsPerConnection -> 0, minPoolSize -> 3, numHelperThreads -> 3, numThreadsAwaitingCheckoutDefaultUser -> 0, preferredTestQuery -> null, properties -> {user=******, password=******}, propertyCycle -> 0, testConnectionOnCheckin -> false, testConnectionOnCheckout -> false, unreturnedConnectionTimeout -> 0, usesTraditionalReflectiveProxies -> false ]

上面的前两行被记录到 System.err,这根本没有任何意义,因为它也按照预期记录到 SLF4J。

有没有办法让我禁用从 c3p0 记录到 System.err

According to the c3p0 documentation, you can manually specify where logs should go, whether through JDK 1.4 logging, Log4j, or through System.out. I'm running SLF4J, so I've included org.slf4j.jul-to-slf4j and call SLF4JBridgeHandler.install() in my application to force all Java util logging to go through SLF4J. Additionally, I've included the following property in my c3p0.properties file:

com.mchange.v2.log.MLog = com.mchange.v2.log.jdk14logging.Jdk14MLog

This, according to the documentation, will force c3p0 to log to JDK 1.4 logging, which in turn will log things out to SLF4J. This does work somewhat, but I'm still seeing some logs hit System.err:

Example 1:

17:24:32.648 [main] INFO  com.mchange.v2.log.MLog - MLog clients using java 1.4+ standard logging.
Jul 27, 2011 5:24:32 PM com.mchange.v2.log.MLog <clinit>
INFO: MLog clients using java 1.4+ standard logging.
Jul 27, 2011 5:24:32 PM com.mchange.v2.c3p0.C3P0Registry banner
INFO: Initializing c3p0-0.9.1.2 [built 21-May-2007 15:04:56; debug? true; trace: 10]
17:24:32.754 [main] INFO  com.mchange.v2.c3p0.C3P0Registry - Initializing c3p0-0.9.1.2 [built 21-May-2007 15:04:56; debug? true; trace: 10]

Lines 1 and 6 above are written to SLF4J, the others are written to System.err.

Example 2:

Jul 27, 2011 5:24:33 PM com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource getPoolManager
INFO: Initializing c3p0 pool... com.mchange.v2.c3p0.ComboPooledDataSource [ acquireIncrement -> 3, acquireRetryAttempts -> 30, acquireRetryDelay -> 1000, autoCommitOnClose -> false, automaticTestTable -> null, breakAfterAcquireFailure -> false, checkoutTimeout -> 0, connectionCustomizerClassName -> null, connectionTesterClassName -> com.mchange.v2.c3p0.impl.DefaultConnectionTester, dataSourceName -> z8kflt8huk1hupkhyfms|13b33a0e, debugUnreturnedConnectionStackTraces -> false, description -> null, driverClass -> org.sqlite.JDBC, factoryClassLocation -> null, forceIgnoreUnresolvedTransactions -> false, identityToken -> z8kflt8huk1hupkhyfms|13b33a0e, idleConnectionTestPeriod -> 0, initialPoolSize -> 3, jdbcUrl -> jdbc:sqlite:/tmp/floodstream.db, maxAdministrativeTaskTime -> 0, maxConnectionAge -> 0, maxIdleTime -> 1800, maxIdleTimeExcessConnections -> 0, maxPoolSize -> 10, maxStatements -> 0, maxStatementsPerConnection -> 0, minPoolSize -> 3, numHelperThreads -> 3, numThreadsAwaitingCheckoutDefaultUser -> 0, preferredTestQuery -> null, properties -> {user=******, password=******}, propertyCycle -> 0, testConnectionOnCheckin -> false, testConnectionOnCheckout -> false, unreturnedConnectionTimeout -> 0, usesTraditionalReflectiveProxies -> false ]
17:24:33.603 [main] INFO  com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource - Initializing c3p0 pool... com.mchange.v2.c3p0.ComboPooledDataSource [ acquireIncrement -> 3, acquireRetryAttempts -> 30, acquireRetryDelay -> 1000, autoCommitOnClose -> false, automaticTestTable -> null, breakAfterAcquireFailure -> false, checkoutTimeout -> 0, connectionCustomizerClassName -> null, connectionTesterClassName -> com.mchange.v2.c3p0.impl.DefaultConnectionTester, dataSourceName -> z8kflt8huk1hupkhyfms|13b33a0e, debugUnreturnedConnectionStackTraces -> false, description -> null, driverClass -> org.sqlite.JDBC, factoryClassLocation -> null, forceIgnoreUnresolvedTransactions -> false, identityToken -> z8kflt8huk1hupkhyfms|13b33a0e, idleConnectionTestPeriod -> 0, initialPoolSize -> 3, jdbcUrl -> jdbc:sqlite:/tmp/floodstream.db, maxAdministrativeTaskTime -> 0, maxConnectionAge -> 0, maxIdleTime -> 1800, maxIdleTimeExcessConnections -> 0, maxPoolSize -> 10, maxStatements -> 0, maxStatementsPerConnection -> 0, minPoolSize -> 3, numHelperThreads -> 3, numThreadsAwaitingCheckoutDefaultUser -> 0, preferredTestQuery -> null, properties -> {user=******, password=******}, propertyCycle -> 0, testConnectionOnCheckin -> false, testConnectionOnCheckout -> false, unreturnedConnectionTimeout -> 0, usesTraditionalReflectiveProxies -> false ]

The first two lines above are logged to System.err, which doesn't make any sense at all, since it's also logging to SLF4J as intended.

Is there a way for me to disable logging to System.err from c3p0?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

倾`听者〃 2024-12-03 11:34:52

我建议尝试一下 log4j 绑定,并配置 c3p0 来使用它。

java.util.logging 是我使用过的最令人困惑的事情之一。

I would suggest giving the log4j binding a try, and configuring c3p0 to use that.

java.util.logging is one of the more confusing things I've ever worked with.

愁以何悠 2024-12-03 11:34:52

添加以下依赖项可以解决部分问题,但不是全部。

<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>log4j-over-slf4j</artifactId>
    <version>1.7.2</version><!-- Or whatever you want/need... -->
</dependency>

除此之外,您还需要绕过此文件中的烦人逻辑:

https://github.com/jboss-logging/jboss-logging/blob/master/src/main/java/org/jboss/logging/LoggerProviders.java#L93

通过提供此空类:

org.apache.log4j.Hierarchy

关于这个问题的一些争论可以在这里找到:

https://issues.jboss.org/browse/JBLOGGING-65

我计划为此目的创建一个依赖于 log4j-over-slf4j 的单个类依赖项,但是它的工作方式是-位于一个项目中。现在,我可以从 slf4j-nop 切换到 slf4j-simple,再切换到 logback,并从我自己的代码和 Hibernate/C3P0 中获得预期结果。

Adding the following depedency takes care of some of the problem, however not all of it.

<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>log4j-over-slf4j</artifactId>
    <version>1.7.2</version><!-- Or whatever you want/need... -->
</dependency>

In addition to that, you need to bypass the irritating logic in this file:

https://github.com/jboss-logging/jboss-logging/blob/master/src/main/java/org/jboss/logging/LoggerProviders.java#L93

By providing this empty class:

org.apache.log4j.Hierarchy

Some debate on the issue can be found here:

https://issues.jboss.org/browse/JBLOGGING-65

I plan to create a single class dependency that depends on log4j-over-slf4j for this purpose, however it's working as-is in one project. I can now switch from slf4j-nop to slf4j-simple to logback and get the expected results, both from my own code, and from Hibernate/C3P0.

请恋爱 2024-12-03 11:34:52

自从编写了接受的答案(大约三年前)以来,mchange-commons-java 工件的 0.2.5 版本中添加了对 slf4j 登录到任何后端的直接支持,这就是 c3p0 文档使用

例如: 'com.mchange.v2.log.MLog = com.mchange.v2.log.slf4j.Slf4jMLog'

但是... c3p0 的 Maven Central (0.9.2.1) 中的最新稳定版本不使用该版本。您必须至少升级到 0.9.5-pre2 才能使用该配置。最新的未发布版本是 0.9.5-pre7

这将避免必须:

  1. 将您的整体日志记录后端更改为 log4j,或者
  2. 使用将日志记录路由到另一个答案中提供的桥的“双跳”。

这就是我们正在做的路由到 slf4j 和 logback 的事情。然而,在一个应用程序中,我们仍然需要使用 log4j 桥,直到发布更新 c3p0 版本的 hibernate 的 hibernate-c3p0 新版本为止。

我怀疑这是 FredCooke 的答案所引用的问题之一......但它可能会混淆两个问题。 Hibernate 将 jboss-logging 依赖项引入到类路径中,但是如果存在 logback,那么它已经智能地使用它了。然而,c3p0 需要额外配置 com.mchange.v2.log.MLog = com.mchange.v2.log.log4j.Log4jMLog ,以将其日志条目发送到 log4j,然后由 log4j 捕获'log4j =>; slf4j'桥。

Since the accepted answer was written (nearly three years ago), direct support for slf4j logging into any backend was added in version 0.2.5 of the mchange-commons-java artifact, which is what the c3p0 documentation uses.

eg: 'com.mchange.v2.log.MLog = com.mchange.v2.log.slf4j.Slf4jMLog'

However... The latest stable release in Maven Central (0.9.2.1) of c3p0 does not use that version. You would have to upgrade to at least 0.9.5-pre2 to use that configuration. The latest unreleased version is 0.9.5-pre7

This would avoid having to either:

  1. Change your overall logging backend to log4j, OR
  2. Use the 'double-hop' of routing your logging into the bridge provided in another answer.

This is what we are doing to route into slf4j and logback. In one application however we still need to use the log4j bridge until a new version of hibernate's hibernate-c3p0 is released that updates the c3p0 version.

I suspect this is one of the issues to which FredCooke's answer is referencing... but it may confusing two issues. Hibernate brings the jboss-logging dependency to the classpath, but that will intelligently use logback already if it is present. c3p0 however will need to be additionally configured with com.mchange.v2.log.MLog = com.mchange.v2.log.log4j.Log4jMLog to send its log entries into log4j which will then be caught by the 'log4j => slf4j' bridge.

无妨# 2024-12-03 11:34:52

如果您使用 log4j-over-slf4j< /a>,c3p0 的 MLog 将记录到“假”log4j,而该 log4j 又将是 slf4j,然后您可以使用您喜欢的任何提供程序slf4j 的输出。

编辑:当我使用它并提交这个答案时,这是有效的,但几个月后他们特别打破了这个。请参阅 FredCooke回答

If you use log4j-over-slf4j, c3p0's MLog will log to that "fake" log4j, which will in turn be slf4j, then you can use whichever provider you prefer for slf4j's output.

EDIT: This worked when I used it and submitted this answer, but they specifically broke this a couple months later. See the additional modification necessary in FredCooke's answer.

骑趴 2024-12-03 11:34:52

您可以使用 System.setErr() 使 System.err 指向另一个 PrintStream。它不会修改 C3PO,但日志记录输出将出现在您想要的位置。

You can make System.err point to another PrintStream with System.setErr(). It doesn't modify C3PO, but the logging output output will appear where you want it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文