mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
bug fixed Logging error on osgi
This commit is contained in:
@@ -43,7 +43,7 @@ import com.alibaba.druid.support.logging.LogFactory;
|
||||
|
||||
public class MockDriver implements Driver, MockDriverMBean {
|
||||
|
||||
private final static Log LOG = LogFactory.getLog(MockDriver.class);
|
||||
private static Log LOG;
|
||||
|
||||
public final static MockExecuteHandler DEFAULT_HANDLER = new MySqlMockExecuteHandlerImpl();
|
||||
|
||||
@@ -75,6 +75,14 @@ public class MockDriver implements Driver, MockDriverMBean {
|
||||
return logExecuteQueryEnable;
|
||||
}
|
||||
|
||||
private static Log getLog() {
|
||||
if (LOG == null) {
|
||||
LOG = LogFactory.getLog(MockDriver.class);
|
||||
}
|
||||
|
||||
return LOG;
|
||||
}
|
||||
|
||||
public void setLogExecuteQueryEnable(boolean logExecuteQueryEnable) {
|
||||
this.logExecuteQueryEnable = logExecuteQueryEnable;
|
||||
}
|
||||
@@ -119,8 +127,8 @@ public class MockDriver implements Driver, MockDriverMBean {
|
||||
|
||||
connections.remove(conn);
|
||||
|
||||
if (LOG.isDebugEnabled()) {
|
||||
LOG.debug("conn-" + conn.getId() + " close");
|
||||
if (getLog().isDebugEnabled()) {
|
||||
getLog().debug("conn-" + conn.getId() + " close");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -136,12 +144,12 @@ public class MockDriver implements Driver, MockDriverMBean {
|
||||
mbeanServer.registerMBean(instance, objectName);
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
LOG.error("register druid-driver mbean error", ex);
|
||||
getLog().error("register druid-driver mbean error", ex);
|
||||
}
|
||||
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
LOG.error("registerDriver error", e);
|
||||
getLog().error("registerDriver error", e);
|
||||
}
|
||||
|
||||
return false;
|
||||
@@ -175,8 +183,8 @@ public class MockDriver implements Driver, MockDriverMBean {
|
||||
|
||||
MockConnection conn = createMockConnection(this, url, info);
|
||||
|
||||
if (LOG.isDebugEnabled()) {
|
||||
LOG.debug("connect, url " + url + ", id " + conn.getId());
|
||||
if (getLog().isDebugEnabled()) {
|
||||
getLog().debug("connect, url " + url + ", id " + conn.getId());
|
||||
}
|
||||
|
||||
if (url == null) {
|
||||
@@ -234,14 +242,14 @@ public class MockDriver implements Driver, MockDriverMBean {
|
||||
public boolean jdbcCompliant() {
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
public MockResultSet createMockResultSet(MockStatementBase stmt) {
|
||||
return new MockResultSet(stmt);
|
||||
}
|
||||
|
||||
public ResultSet executeQuery(MockStatementBase stmt, String sql) throws SQLException {
|
||||
if (logExecuteQueryEnable && LOG.isDebugEnabled()) {
|
||||
LOG.debug("executeQuery " + sql);
|
||||
if (logExecuteQueryEnable && getLog().isDebugEnabled()) {
|
||||
getLog().debug("executeQuery " + sql);
|
||||
}
|
||||
|
||||
MockConnection conn = stmt.getConnection();
|
||||
|
||||
@@ -24,24 +24,43 @@ public class LogFactory {
|
||||
|
||||
static {
|
||||
// TODO add slf4j logging
|
||||
|
||||
//优先选择log4j,而非Apache Common Logging. 因为后者无法设置真实Log调用者的信息
|
||||
|
||||
// 优先选择log4j,而非Apache Common Logging. 因为后者无法设置真实Log调用者的信息
|
||||
tryImplementation("org.apache.log4j.Logger", "com.alibaba.druid.support.logging.Log4jImpl");
|
||||
tryImplementation("java.util.logging.Logger", "com.alibaba.druid.support.logging.Jdk14LoggingImpl");
|
||||
tryImplementation("org.apache.commons.logging.LogFactory",
|
||||
"com.alibaba.druid.support.logging.JakartaCommonsLoggingImpl");
|
||||
|
||||
if (logConstructor == null) {
|
||||
try {
|
||||
logConstructor = NoLoggingImpl.class.getConstructor(Class.class);
|
||||
} catch (Exception e) {
|
||||
throw new IllegalStateException(e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
tryImplementation("java.lang.Object", "com.alibaba.druid.support.logging.NoLoggingImpl");
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private static void tryImplementation(String testClassName, String implClassName) {
|
||||
if (logConstructor == null) {
|
||||
try {
|
||||
Resources.classForName(testClassName);
|
||||
Class implClass = Resources.classForName(implClassName);
|
||||
logConstructor = implClass.getConstructor(new Class[] { Class.class });
|
||||
} catch (Throwable t) {
|
||||
if (logConstructor != null) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
Resources.classForName(testClassName);
|
||||
Class implClass = Resources.classForName(implClassName);
|
||||
logConstructor = implClass.getConstructor(new Class[] { Class.class });
|
||||
{
|
||||
Class<?> declareClass = logConstructor.getDeclaringClass();
|
||||
if (!Log.class.isAssignableFrom(declareClass)) {
|
||||
logConstructor = null;
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
}
|
||||
} catch (Throwable t) {
|
||||
t.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -20,8 +20,10 @@ public class NoLoggingImpl implements Log {
|
||||
private int infoCount;
|
||||
private int errorCount;
|
||||
private int warnCount;
|
||||
private Class<?> clazz;
|
||||
|
||||
public NoLoggingImpl(Class<?> clazz){
|
||||
this.clazz = clazz;
|
||||
}
|
||||
|
||||
public boolean isDebugEnabled() {
|
||||
@@ -29,11 +31,18 @@ public class NoLoggingImpl implements Log {
|
||||
}
|
||||
|
||||
public void error(String s, Throwable e) {
|
||||
errorCount++;
|
||||
error(s);
|
||||
|
||||
if (e != null) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public void error(String s) {
|
||||
errorCount++;
|
||||
if (s != null) {
|
||||
System.err.println(clazz.getClass().getName() + " : " + s);
|
||||
}
|
||||
}
|
||||
|
||||
public void debug(String s) {
|
||||
|
||||
@@ -263,7 +263,7 @@ public final class Resources extends Object {
|
||||
in.close();
|
||||
return props;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Loads a class
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user