近期的项目里使用到了c3p0,在创建ComboPooledDataSource类时会输出一堆日志,而且竟然是用System.err.println输出的,实在是令人不爽。
一堆日志
2013-10-22 14:53:56 com.mchange.v2.log.MLog
信息: MLog clients using java 1.4+ standard logging.
2013-10-22 14:53:56 com.mchange.v2.c3p0.C3P0Registry banner
信息: Initializing c3p0-0.9.2-pre8 [built 19-December-2012 15:00:02 +0000; debug? true; trace: 10]
2013-10-22 14:53:56 com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource getPoolManager
信息: Initializing c3p0 pool... com.mchange.v2.c3p0.ComboPooledDataSource [ acquireIncrement -> 3, acquireRetryAttempts -> 30, acquireRetryDelay -> 1000, autoCommitOnClose -> false, automaticTestTable -> null, breakAfterAcquireFailure -> true, checkoutTimeout -> 0, connectionCustomizerClassName -> null, connectionTesterClassName -> com.mchange.v2.c3p0.impl.DefaultConnectionTester, dataSourceName -> 1hge0yq8x1puwc6y1d8sp6v|e70e30, debugUnreturnedConnectionStackTraces -> false, description -> null, driverClass -> com.mysql.jdbc.Driver, factoryClassLocation -> null, forceIgnoreUnresolvedTransactions -> false, identityToken -> 1hge0yq8x1puwc6y1d8sp6v|e70e30, idleConnectionTestPeriod -> 20, initialPoolSize -> 5, jdbcUrl -> jdbc:mysql://127.0.0.1:3306/douniu?useUnicode=true&characterEncoding=utf8, maxAdministrativeTaskTime -> 0, maxConnectionAge -> 0, maxIdleTime -> 0, maxIdleTimeExcessConnections -> 0, maxPoolSize -> 100, maxStatements -> 200, maxStatementsPerConnection -> 0, minPoolSize -> 1, numHelperThreads -> 5, preferredTestQuery -> null, properties -> {user=******, password=******}, propertyCycle -> 0, statementCacheNumDeferredCloseThreads -> 0, testConnectionOnCheckin -> true, testConnectionOnCheckout -> true, unreturnedConnectionTimeout -> 60, userOverrides -> {}, usesTraditionalReflectiveProxies -> false ]
与之奋战了一下午,终于把他搞掉了,记录过程如下:
1.反编译mchange-commons-java-0.2.3.3.jar发现其中一句是在com.mchange.v2.log.MLog内
logger = getLogger(MLog.class);
String loggerDesc = mlog.getClass().getName();
if ("com.mchange.v2.log.jdk14logging.Jdk14MLog".equals(loggerDesc))
loggerDesc = "java 1.4+ standard";
else if ("com.mchange.v2.log.log4j.Log4jMLog".equals(loggerDesc)) {
loggerDesc = "log4j";
}
if (logger.isLoggable(MLevel.INFO)) {
logger.log(MLevel.INFO, "MLog clients using " + loggerDesc + " logging."); //这一句在前面的日志里出现过
}
public synchronized MLogger getMLogger(String name) {
name = name.intern();
MLogger out = (MLogger)this.namedLoggerMap.get(name);
if (out == null)
{
Logger lg = Logger.getLogger(name);
out = new Jdk14MLogger(lg);
this.namedLoggerMap.put(name, out);
}
return out;
}
public synchronized MLogger getMLogger(Class cl) {
return getLogger(cl.getName());
}
public synchronized MLogger getMLogger()
{
if (this.global == null)
this.global = new Jdk14MLogger(LogManager.getLogManager().getLogger("global"));
return this.global;
}
这里的Jdk14MLogger是com.mchange.v2.log.jdk14logging.Jdk14MLog的成员类,在此找到了第一步中调用的isLoggable方法
public boolean isLoggable(MLevel l) {
return this.logger.isLoggable(level(l));
}
private static Level level(MLevel lvl) {
return (Level)lvl.asJdk14Level(); //Level是java.util.logging.Level
}
3.由asJdk14Level方法追查到com.mchange.v2.log.MLevel
Object level;
public Object asJdk14Level() {
return this.level;
}
4.知道了以上信息之后即可以对其进行修改了,由于level变量限包内访问,所以要在自己的项目里面创建同名包之后再创建类如下:
package com.mchange.v2.log;
import java.util.logging.Level;
import com.mchange.v2.log.MLevel;
/**
* 为减少日志而添加的类,不要修改包名
*/
public class ModifyLogLevel {
public static void modifyInfoLevel(Level level) {
MLevel.INFO.level=level;
}
}
5.在主方法调用一下此方法后就看不到烦人的日志了
public class MainServer {
static {
ModifyLogLevel.modifyInfoLevel(Level.ALL);
}
}
结:在网上没找到好的方法,所以才折腾了一个如此纠结的方法。如果您有更好的办法,恳请提点。
参考资料:
http://jingyan.baidu.com/article/642c9d34d00d8c644a46f79c.html
http://lavasoft.blog.51cto.com/62575/184492/