mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
refactor logging
This commit is contained in:
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@@ -33,7 +33,9 @@ import com.alibaba.druid.support.logging.LogFactory;
|
||||
*/
|
||||
public class DruidDataSourceStatLoggerImpl extends DruidDataSourceStatLoggerAdapter {
|
||||
|
||||
private static Log LOG = LogFactory.getLog(DruidDataSourceStatLoggerImpl.class);
|
||||
private static Log LOG = LogFactory.getLog(DruidDataSourceStatLoggerImpl.class);
|
||||
|
||||
private Log logger = LOG;
|
||||
|
||||
public DruidDataSourceStatLoggerImpl(){
|
||||
this.configFromProperties(System.getProperties());
|
||||
@@ -44,12 +46,23 @@ public class DruidDataSourceStatLoggerImpl extends DruidDataSourceStatLoggerAdap
|
||||
*/
|
||||
@Override
|
||||
public void configFromProperties(Properties properties) {
|
||||
String property = properties.getProperty("druid.stat.loggerName");
|
||||
if (property != null && property.length() > 0) {
|
||||
setLoggerName(property);
|
||||
}
|
||||
}
|
||||
|
||||
public Log getLogger() {
|
||||
return logger;
|
||||
}
|
||||
|
||||
public void setLoggerName(String loggerName) {
|
||||
logger = LogFactory.getLog(loggerName);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void log(DruidDataSourceStatValue statValue) {
|
||||
if (LOG.isInfoEnabled()) {
|
||||
if (logger.isInfoEnabled()) {
|
||||
Map<String, Object> map = new LinkedHashMap<String, Object>();
|
||||
|
||||
map.put("url", statValue.url);
|
||||
@@ -205,7 +218,7 @@ public class DruidDataSourceStatLoggerImpl extends DruidDataSourceStatLoggerAdap
|
||||
|
||||
String text = JSONUtils.toJSONString(map);
|
||||
|
||||
LOG.info(text);
|
||||
logger.info(text);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,102 +1,102 @@
|
||||
/*
|
||||
* Copyright 1999-2011 Alibaba Group Holding Ltd.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.alibaba.druid.support.logging;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
public class JakartaCommonsLoggingImpl implements com.alibaba.druid.support.logging.Log {
|
||||
|
||||
private Log log;
|
||||
|
||||
private int errorCount;
|
||||
private int warnCount;
|
||||
private int infoCount;
|
||||
|
||||
public JakartaCommonsLoggingImpl(Class<?> clazz){
|
||||
log = LogFactory.getLog(clazz);
|
||||
}
|
||||
|
||||
public boolean isDebugEnabled() {
|
||||
return log.isDebugEnabled();
|
||||
}
|
||||
|
||||
public void error(String s, Throwable e) {
|
||||
log.error(s, e);
|
||||
errorCount++;
|
||||
}
|
||||
|
||||
public void error(String s) {
|
||||
log.error(s);
|
||||
errorCount++;
|
||||
}
|
||||
|
||||
public void debug(String s) {
|
||||
log.debug(s);
|
||||
}
|
||||
|
||||
public void debug(String s, Throwable e) {
|
||||
log.debug(s, e);
|
||||
}
|
||||
|
||||
public void warn(String s) {
|
||||
log.warn(s);
|
||||
warnCount++;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void warn(String s, Throwable e) {
|
||||
log.warn(s, e);
|
||||
warnCount++;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getWarnCount() {
|
||||
return warnCount;
|
||||
}
|
||||
|
||||
public int getErrorCount() {
|
||||
return errorCount;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void resetStat() {
|
||||
errorCount = 0;
|
||||
warnCount = 0;
|
||||
infoCount = 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isInfoEnabled() {
|
||||
return log.isInfoEnabled();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void info(String msg) {
|
||||
log.info(msg);
|
||||
infoCount++;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getInfoCount() {
|
||||
return infoCount;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isWarnEnabled() {
|
||||
return log.isWarnEnabled();
|
||||
}
|
||||
}
|
||||
/*
|
||||
* Copyright 1999-2011 Alibaba Group Holding Ltd.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.alibaba.druid.support.logging;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
public class JakartaCommonsLoggingImpl implements com.alibaba.druid.support.logging.Log {
|
||||
|
||||
private Log log;
|
||||
|
||||
private int errorCount;
|
||||
private int warnCount;
|
||||
private int infoCount;
|
||||
|
||||
public JakartaCommonsLoggingImpl(String loggerName){
|
||||
log = LogFactory.getLog(loggerName);
|
||||
}
|
||||
|
||||
public boolean isDebugEnabled() {
|
||||
return log.isDebugEnabled();
|
||||
}
|
||||
|
||||
public void error(String s, Throwable e) {
|
||||
log.error(s, e);
|
||||
errorCount++;
|
||||
}
|
||||
|
||||
public void error(String s) {
|
||||
log.error(s);
|
||||
errorCount++;
|
||||
}
|
||||
|
||||
public void debug(String s) {
|
||||
log.debug(s);
|
||||
}
|
||||
|
||||
public void debug(String s, Throwable e) {
|
||||
log.debug(s, e);
|
||||
}
|
||||
|
||||
public void warn(String s) {
|
||||
log.warn(s);
|
||||
warnCount++;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void warn(String s, Throwable e) {
|
||||
log.warn(s, e);
|
||||
warnCount++;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getWarnCount() {
|
||||
return warnCount;
|
||||
}
|
||||
|
||||
public int getErrorCount() {
|
||||
return errorCount;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void resetStat() {
|
||||
errorCount = 0;
|
||||
warnCount = 0;
|
||||
infoCount = 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isInfoEnabled() {
|
||||
return log.isInfoEnabled();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void info(String msg) {
|
||||
log.info(msg);
|
||||
infoCount++;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getInfoCount() {
|
||||
return infoCount;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isWarnEnabled() {
|
||||
return log.isWarnEnabled();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,105 +1,105 @@
|
||||
/*
|
||||
* Copyright 1999-2011 Alibaba Group Holding Ltd.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.alibaba.druid.support.logging;
|
||||
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
public class Jdk14LoggingImpl implements Log {
|
||||
|
||||
private Logger log;
|
||||
|
||||
private int errorCount;
|
||||
private int warnCount;
|
||||
private int infoCount;
|
||||
|
||||
private String clazzName;
|
||||
|
||||
public Jdk14LoggingImpl(Class<?> clazz){
|
||||
clazzName = clazz.getName();
|
||||
log = Logger.getLogger(clazzName);
|
||||
}
|
||||
|
||||
public boolean isDebugEnabled() {
|
||||
return log.isLoggable(Level.FINE);
|
||||
}
|
||||
|
||||
public void error(String s, Throwable e) {
|
||||
log.logp(Level.SEVERE,clazzName, Thread.currentThread().getStackTrace()[1].getMethodName(), s, e);
|
||||
errorCount++;
|
||||
}
|
||||
|
||||
public void error(String s) {
|
||||
log.logp(Level.SEVERE,clazzName, Thread.currentThread().getStackTrace()[1].getMethodName(), s);
|
||||
errorCount++;
|
||||
}
|
||||
|
||||
public void debug(String s) {
|
||||
log.logp(Level.FINE, clazzName, Thread.currentThread().getStackTrace()[1].getMethodName(), s);
|
||||
}
|
||||
|
||||
public void debug(String s, Throwable e) {
|
||||
log.logp(Level.FINE, clazzName, Thread.currentThread().getStackTrace()[1].getMethodName(), s, e);
|
||||
}
|
||||
|
||||
public void warn(String s) {
|
||||
log.logp(Level.WARNING, clazzName, Thread.currentThread().getStackTrace()[1].getMethodName(), s);
|
||||
warnCount++;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void warn(String s, Throwable e) {
|
||||
log.logp(Level.WARNING, clazzName, Thread.currentThread().getStackTrace()[1].getMethodName(), s, e);
|
||||
warnCount++;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getWarnCount() {
|
||||
return warnCount;
|
||||
}
|
||||
|
||||
public int getErrorCount() {
|
||||
return errorCount;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void resetStat() {
|
||||
errorCount = 0;
|
||||
warnCount = 0;
|
||||
infoCount = 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isInfoEnabled() {
|
||||
return log.isLoggable(Level.INFO);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void info(String msg) {
|
||||
log.logp(Level.INFO, clazzName, Thread.currentThread().getStackTrace()[1].getMethodName(), msg);
|
||||
infoCount++;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getInfoCount() {
|
||||
return infoCount;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isWarnEnabled() {
|
||||
return log.isLoggable(Level.WARNING);
|
||||
}
|
||||
}
|
||||
/*
|
||||
* Copyright 1999-2011 Alibaba Group Holding Ltd.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.alibaba.druid.support.logging;
|
||||
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
public class Jdk14LoggingImpl implements Log {
|
||||
|
||||
private Logger log;
|
||||
|
||||
private int errorCount;
|
||||
private int warnCount;
|
||||
private int infoCount;
|
||||
|
||||
private String loggerName;
|
||||
|
||||
public Jdk14LoggingImpl(String loggerName){
|
||||
this.loggerName = loggerName;
|
||||
log = Logger.getLogger(loggerName);
|
||||
}
|
||||
|
||||
public boolean isDebugEnabled() {
|
||||
return log.isLoggable(Level.FINE);
|
||||
}
|
||||
|
||||
public void error(String s, Throwable e) {
|
||||
log.logp(Level.SEVERE, loggerName, Thread.currentThread().getStackTrace()[1].getMethodName(), s, e);
|
||||
errorCount++;
|
||||
}
|
||||
|
||||
public void error(String s) {
|
||||
log.logp(Level.SEVERE, loggerName, Thread.currentThread().getStackTrace()[1].getMethodName(), s);
|
||||
errorCount++;
|
||||
}
|
||||
|
||||
public void debug(String s) {
|
||||
log.logp(Level.FINE, loggerName, Thread.currentThread().getStackTrace()[1].getMethodName(), s);
|
||||
}
|
||||
|
||||
public void debug(String s, Throwable e) {
|
||||
log.logp(Level.FINE, loggerName, Thread.currentThread().getStackTrace()[1].getMethodName(), s, e);
|
||||
}
|
||||
|
||||
public void warn(String s) {
|
||||
log.logp(Level.WARNING, loggerName, Thread.currentThread().getStackTrace()[1].getMethodName(), s);
|
||||
warnCount++;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void warn(String s, Throwable e) {
|
||||
log.logp(Level.WARNING, loggerName, Thread.currentThread().getStackTrace()[1].getMethodName(), s, e);
|
||||
warnCount++;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getWarnCount() {
|
||||
return warnCount;
|
||||
}
|
||||
|
||||
public int getErrorCount() {
|
||||
return errorCount;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void resetStat() {
|
||||
errorCount = 0;
|
||||
warnCount = 0;
|
||||
infoCount = 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isInfoEnabled() {
|
||||
return log.isLoggable(Level.INFO);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void info(String msg) {
|
||||
log.logp(Level.INFO, loggerName, Thread.currentThread().getStackTrace()[1].getMethodName(), msg);
|
||||
infoCount++;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getInfoCount() {
|
||||
return infoCount;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isWarnEnabled() {
|
||||
return log.isLoggable(Level.WARNING);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,101 +1,101 @@
|
||||
/*
|
||||
* Copyright 1999-2011 Alibaba Group Holding Ltd.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.alibaba.druid.support.logging;
|
||||
|
||||
import org.apache.log4j.Level;
|
||||
import org.apache.log4j.Logger;
|
||||
|
||||
public class Log4jImpl implements Log {
|
||||
|
||||
private static final String callerFQCN = Log4jImpl.class.getName();
|
||||
|
||||
private Logger log;
|
||||
|
||||
private int errorCount;
|
||||
private int warnCount;
|
||||
private int infoCount;
|
||||
|
||||
public Log4jImpl(Class<?> clazz){
|
||||
log = Logger.getLogger(clazz);
|
||||
}
|
||||
|
||||
public boolean isDebugEnabled() {
|
||||
return log.isDebugEnabled();
|
||||
}
|
||||
|
||||
public void error(String s, Throwable e) {
|
||||
errorCount++;
|
||||
log.log(callerFQCN, Level.ERROR, s, e);
|
||||
}
|
||||
|
||||
public void error(String s) {
|
||||
errorCount++;
|
||||
log.log(callerFQCN, Level.ERROR, s, null);
|
||||
}
|
||||
|
||||
public void debug(String s) {
|
||||
log.log(callerFQCN, Level.DEBUG, s, null);
|
||||
}
|
||||
|
||||
public void debug(String s, Throwable e) {
|
||||
log.log(callerFQCN, Level.DEBUG, s, e);
|
||||
}
|
||||
|
||||
public void warn(String s) {
|
||||
log.log(callerFQCN, Level.WARN, s, null);
|
||||
warnCount++;
|
||||
}
|
||||
|
||||
public void warn(String s, Throwable e) {
|
||||
log.log(callerFQCN, Level.WARN, s, e);
|
||||
warnCount++;
|
||||
}
|
||||
|
||||
public int getWarnCount() {
|
||||
return warnCount;
|
||||
}
|
||||
|
||||
public int getErrorCount() {
|
||||
return errorCount;
|
||||
}
|
||||
|
||||
public void resetStat() {
|
||||
errorCount = 0;
|
||||
warnCount = 0;
|
||||
infoCount = 0;
|
||||
}
|
||||
|
||||
public boolean isInfoEnabled() {
|
||||
return log.isInfoEnabled();
|
||||
}
|
||||
|
||||
public void info(String msg) {
|
||||
infoCount++;
|
||||
log.log(callerFQCN, Level.INFO, msg, null);
|
||||
}
|
||||
|
||||
public boolean isWarnEnabled() {
|
||||
return log.isEnabledFor(Level.WARN);
|
||||
}
|
||||
|
||||
public int getInfoCount() {
|
||||
return infoCount;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return log.toString();
|
||||
}
|
||||
}
|
||||
/*
|
||||
* Copyright 1999-2011 Alibaba Group Holding Ltd.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.alibaba.druid.support.logging;
|
||||
|
||||
import org.apache.log4j.Level;
|
||||
import org.apache.log4j.Logger;
|
||||
|
||||
public class Log4jImpl implements Log {
|
||||
|
||||
private static final String callerFQCN = Log4jImpl.class.getName();
|
||||
|
||||
private Logger log;
|
||||
|
||||
private int errorCount;
|
||||
private int warnCount;
|
||||
private int infoCount;
|
||||
|
||||
public Log4jImpl(String loggerName){
|
||||
log = Logger.getLogger(loggerName);
|
||||
}
|
||||
|
||||
public boolean isDebugEnabled() {
|
||||
return log.isDebugEnabled();
|
||||
}
|
||||
|
||||
public void error(String s, Throwable e) {
|
||||
errorCount++;
|
||||
log.log(callerFQCN, Level.ERROR, s, e);
|
||||
}
|
||||
|
||||
public void error(String s) {
|
||||
errorCount++;
|
||||
log.log(callerFQCN, Level.ERROR, s, null);
|
||||
}
|
||||
|
||||
public void debug(String s) {
|
||||
log.log(callerFQCN, Level.DEBUG, s, null);
|
||||
}
|
||||
|
||||
public void debug(String s, Throwable e) {
|
||||
log.log(callerFQCN, Level.DEBUG, s, e);
|
||||
}
|
||||
|
||||
public void warn(String s) {
|
||||
log.log(callerFQCN, Level.WARN, s, null);
|
||||
warnCount++;
|
||||
}
|
||||
|
||||
public void warn(String s, Throwable e) {
|
||||
log.log(callerFQCN, Level.WARN, s, e);
|
||||
warnCount++;
|
||||
}
|
||||
|
||||
public int getWarnCount() {
|
||||
return warnCount;
|
||||
}
|
||||
|
||||
public int getErrorCount() {
|
||||
return errorCount;
|
||||
}
|
||||
|
||||
public void resetStat() {
|
||||
errorCount = 0;
|
||||
warnCount = 0;
|
||||
infoCount = 0;
|
||||
}
|
||||
|
||||
public boolean isInfoEnabled() {
|
||||
return log.isInfoEnabled();
|
||||
}
|
||||
|
||||
public void info(String msg) {
|
||||
infoCount++;
|
||||
log.log(callerFQCN, Level.INFO, msg, null);
|
||||
}
|
||||
|
||||
public boolean isWarnEnabled() {
|
||||
return log.isEnabledFor(Level.WARN);
|
||||
}
|
||||
|
||||
public int getInfoCount() {
|
||||
return infoCount;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return log.toString();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,109 +1,101 @@
|
||||
/*
|
||||
* Copyright 1999-2011 Alibaba Group Holding Ltd.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.alibaba.druid.support.logging;
|
||||
|
||||
import java.lang.reflect.Constructor;
|
||||
|
||||
@SuppressWarnings("rawtypes")
|
||||
public class LogFactory {
|
||||
|
||||
private static Constructor logConstructor;
|
||||
|
||||
static {
|
||||
// TODO add slf4j logging
|
||||
|
||||
// 优先选择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.slf4j.Logger", "com.alibaba.druid.support.logging.SLF4JImpl");
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private static void tryImplementation(String testClassName, String implClassName) {
|
||||
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;
|
||||
}
|
||||
|
||||
try {
|
||||
logConstructor.newInstance(LogFactory.class);
|
||||
} catch (Throwable t) {
|
||||
logConstructor = null;
|
||||
}
|
||||
} catch (Throwable t) {
|
||||
// skip
|
||||
}
|
||||
}
|
||||
|
||||
public static Log getLog(Class aClass) {
|
||||
try {
|
||||
return (Log) logConstructor.newInstance(new Object[] { aClass });
|
||||
} catch (Throwable t) {
|
||||
throw new RuntimeException("Error creating logger for class " + aClass + ". Cause: " + t, t);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This method will switch the logging implementation to Log4J if Log4J is available on the classpath. This is
|
||||
* useful in situations where you want to use Log4J to log iBATIS activity but commons logging is on the classpath.
|
||||
* Note that this method is only effective for log classes obtained after calling this method. If you intend to use
|
||||
* this method you should call it before calling any other iBATIS method.
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public static synchronized void selectLog4JLogging() {
|
||||
try {
|
||||
Resources.classForName("org.apache.log4j.Logger");
|
||||
Class implClass = Resources.classForName("com.alibaba.druid.support.logging.Log4jImpl");
|
||||
logConstructor = implClass.getConstructor(new Class[] { Class.class });
|
||||
} catch (Throwable t) {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This method will switch the logging implementation to Java native logging if you are running in JRE 1.4 or above.
|
||||
* This is useful in situations where you want to use Java native logging to log iBATIS activity but commons logging
|
||||
* or Log4J is on the classpath. Note that this method is only effective for log classes obtained after calling this
|
||||
* method. If you intend to use this method you should call it before calling any other iBATIS method.
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public static synchronized void selectJavaLogging() {
|
||||
try {
|
||||
Resources.classForName("java.util.logging.Logger");
|
||||
Class implClass = Resources.classForName("com.alibaba.druid.support.logging.Jdk14LoggingImpl");
|
||||
logConstructor = implClass.getConstructor(new Class[] { Class.class });
|
||||
} catch (Throwable t) {
|
||||
}
|
||||
}
|
||||
}
|
||||
/*
|
||||
* Copyright 1999-2011 Alibaba Group Holding Ltd.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.alibaba.druid.support.logging;
|
||||
|
||||
import java.lang.reflect.Constructor;
|
||||
|
||||
@SuppressWarnings("rawtypes")
|
||||
public class LogFactory {
|
||||
|
||||
private static Constructor logConstructor;
|
||||
|
||||
static {
|
||||
// TODO add slf4j logging
|
||||
|
||||
// 优先选择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.slf4j.Logger", "com.alibaba.druid.support.logging.SLF4JImpl");
|
||||
tryImplementation("org.apache.commons.logging.LogFactory",
|
||||
"com.alibaba.druid.support.logging.JakartaCommonsLoggingImpl");
|
||||
|
||||
if (logConstructor == null) {
|
||||
try {
|
||||
logConstructor = NoLoggingImpl.class.getConstructor(String.class);
|
||||
} catch (Exception e) {
|
||||
throw new IllegalStateException(e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private static void tryImplementation(String testClassName, String implClassName) {
|
||||
if (logConstructor != null) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
Resources.classForName(testClassName);
|
||||
Class implClass = Resources.classForName(implClassName);
|
||||
logConstructor = implClass.getConstructor(new Class[] { String.class });
|
||||
|
||||
Class<?> declareClass = logConstructor.getDeclaringClass();
|
||||
if (!Log.class.isAssignableFrom(declareClass)) {
|
||||
logConstructor = null;
|
||||
}
|
||||
|
||||
try {
|
||||
logConstructor.newInstance(LogFactory.class.getName());
|
||||
} catch (Throwable t) {
|
||||
logConstructor = null;
|
||||
}
|
||||
} catch (Throwable t) {
|
||||
// skip
|
||||
}
|
||||
}
|
||||
|
||||
public static Log getLog(Class clazz) {
|
||||
return getLog(clazz.getName());
|
||||
}
|
||||
|
||||
public static Log getLog(String loggerName) {
|
||||
try {
|
||||
return (Log) logConstructor.newInstance(new Object[] { loggerName });
|
||||
} catch (Throwable t) {
|
||||
throw new RuntimeException("Error creating logger for logger '" + loggerName + "'. Cause: " + t, t);
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public static synchronized void selectLog4JLogging() {
|
||||
try {
|
||||
Resources.classForName("org.apache.log4j.Logger");
|
||||
Class implClass = Resources.classForName("com.alibaba.druid.support.logging.Log4jImpl");
|
||||
logConstructor = implClass.getConstructor(new Class[] { String.class });
|
||||
} catch (Throwable t) {
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public static synchronized void selectJavaLogging() {
|
||||
try {
|
||||
Resources.classForName("java.util.logging.Logger");
|
||||
Class implClass = Resources.classForName("com.alibaba.druid.support.logging.Jdk14LoggingImpl");
|
||||
logConstructor = implClass.getConstructor(new Class[] { String.class });
|
||||
} catch (Throwable t) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,97 +1,101 @@
|
||||
/*
|
||||
* Copyright 1999-2011 Alibaba Group Holding Ltd.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.alibaba.druid.support.logging;
|
||||
|
||||
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() {
|
||||
return false;
|
||||
}
|
||||
|
||||
public void error(String s, Throwable e) {
|
||||
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) {
|
||||
}
|
||||
|
||||
public void debug(String s, Throwable e) {
|
||||
}
|
||||
|
||||
public void warn(String s) {
|
||||
warnCount++;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void warn(String s, Throwable e) {
|
||||
warnCount++;
|
||||
}
|
||||
|
||||
public int getErrorCount() {
|
||||
return errorCount;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getWarnCount() {
|
||||
return warnCount;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void resetStat() {
|
||||
errorCount = 0;
|
||||
warnCount = 0;
|
||||
infoCount = 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isInfoEnabled() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void info(String s) {
|
||||
infoCount++;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isWarnEnabled() {
|
||||
return false;
|
||||
}
|
||||
|
||||
public int getInfoCount() {
|
||||
return infoCount;
|
||||
}
|
||||
}
|
||||
/*
|
||||
* Copyright 1999-2011 Alibaba Group Holding Ltd.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.alibaba.druid.support.logging;
|
||||
|
||||
public class NoLoggingImpl implements Log {
|
||||
|
||||
private int infoCount;
|
||||
private int errorCount;
|
||||
private int warnCount;
|
||||
private String loggerName;
|
||||
|
||||
public NoLoggingImpl(String loggerName){
|
||||
this.loggerName = loggerName;
|
||||
}
|
||||
|
||||
public String getLoggerName() {
|
||||
return this.loggerName;
|
||||
}
|
||||
|
||||
public boolean isDebugEnabled() {
|
||||
return false;
|
||||
}
|
||||
|
||||
public void error(String s, Throwable e) {
|
||||
error(s);
|
||||
|
||||
if (e != null) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public void error(String s) {
|
||||
errorCount++;
|
||||
if (s != null) {
|
||||
System.err.println(loggerName + " : " + s);
|
||||
}
|
||||
}
|
||||
|
||||
public void debug(String s) {
|
||||
}
|
||||
|
||||
public void debug(String s, Throwable e) {
|
||||
}
|
||||
|
||||
public void warn(String s) {
|
||||
warnCount++;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void warn(String s, Throwable e) {
|
||||
warnCount++;
|
||||
}
|
||||
|
||||
public int getErrorCount() {
|
||||
return errorCount;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getWarnCount() {
|
||||
return warnCount;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void resetStat() {
|
||||
errorCount = 0;
|
||||
warnCount = 0;
|
||||
infoCount = 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isInfoEnabled() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void info(String s) {
|
||||
infoCount++;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isWarnEnabled() {
|
||||
return false;
|
||||
}
|
||||
|
||||
public int getInfoCount() {
|
||||
return infoCount;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,102 +20,104 @@ import org.slf4j.LoggerFactory;
|
||||
import org.slf4j.spi.LocationAwareLogger;
|
||||
|
||||
public class SLF4JImpl implements Log {
|
||||
private static final String callerFQCN = SLF4JImpl.class.getName();
|
||||
private static final Logger testLogger = LoggerFactory.getLogger(SLF4JImpl.class);
|
||||
static {
|
||||
//if the logger is not a LocationAwareLogger instance, it can not get correct stack StackTraceElement
|
||||
//so ignore this implementation.
|
||||
if (!(testLogger instanceof LocationAwareLogger)) {
|
||||
throw new UnsupportedOperationException(testLogger.getClass()+" is not a suitable logger");
|
||||
}
|
||||
}
|
||||
private int errorCount;
|
||||
private int warnCount;
|
||||
private int infoCount;
|
||||
private LocationAwareLogger log;
|
||||
|
||||
public SLF4JImpl(Class<?> clazz) {
|
||||
Logger log = LoggerFactory.getLogger(clazz);
|
||||
if (log instanceof LocationAwareLogger) {
|
||||
this.log = (LocationAwareLogger) log;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isDebugEnabled() {
|
||||
return log.isDebugEnabled();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void error(String msg, Throwable e) {
|
||||
log.log(null, callerFQCN, LocationAwareLogger.ERROR_INT, msg, null, e);
|
||||
errorCount++;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void error(String msg) {
|
||||
log.log(null, callerFQCN, LocationAwareLogger.ERROR_INT, msg, null, null);
|
||||
errorCount++;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isInfoEnabled() {
|
||||
return log.isInfoEnabled();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void info(String msg) {
|
||||
infoCount++;
|
||||
log.log(null, callerFQCN, LocationAwareLogger.INFO_INT, msg, null, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void debug(String msg) {
|
||||
log.log(null, callerFQCN, LocationAwareLogger.DEBUG_INT, msg, null, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void debug(String msg, Throwable e) {
|
||||
log.log(null, callerFQCN, LocationAwareLogger.ERROR_INT, msg, null, e);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isWarnEnabled() {
|
||||
return log.isWarnEnabled();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void warn(String msg) {
|
||||
log.log(null, callerFQCN, LocationAwareLogger.WARN_INT, msg, null, null);
|
||||
warnCount++;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void warn(String msg, Throwable e) {
|
||||
log.log(null, callerFQCN, LocationAwareLogger.WARN_INT, msg, null, e);
|
||||
warnCount++;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getErrorCount() {
|
||||
return errorCount;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getWarnCount() {
|
||||
return warnCount;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getInfoCount() {
|
||||
return infoCount;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void resetStat() {
|
||||
errorCount = 0;
|
||||
warnCount = 0;
|
||||
infoCount = 0;
|
||||
}
|
||||
|
||||
|
||||
private static final String callerFQCN = SLF4JImpl.class.getName();
|
||||
private static final Logger testLogger = LoggerFactory.getLogger(SLF4JImpl.class);
|
||||
static {
|
||||
// if the logger is not a LocationAwareLogger instance, it can not get correct stack StackTraceElement
|
||||
// so ignore this implementation.
|
||||
if (!(testLogger instanceof LocationAwareLogger)) {
|
||||
throw new UnsupportedOperationException(testLogger.getClass() + " is not a suitable logger");
|
||||
}
|
||||
}
|
||||
private int errorCount;
|
||||
private int warnCount;
|
||||
private int infoCount;
|
||||
private LocationAwareLogger log;
|
||||
|
||||
public SLF4JImpl(Class<?> clazz){
|
||||
this.log = (LocationAwareLogger) LoggerFactory.getLogger(clazz);
|
||||
}
|
||||
|
||||
public SLF4JImpl(String loggerName){
|
||||
this.log = (LocationAwareLogger) LoggerFactory.getLogger(loggerName);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isDebugEnabled() {
|
||||
return log.isDebugEnabled();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void error(String msg, Throwable e) {
|
||||
log.log(null, callerFQCN, LocationAwareLogger.ERROR_INT, msg, null, e);
|
||||
errorCount++;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void error(String msg) {
|
||||
log.log(null, callerFQCN, LocationAwareLogger.ERROR_INT, msg, null, null);
|
||||
errorCount++;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isInfoEnabled() {
|
||||
return log.isInfoEnabled();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void info(String msg) {
|
||||
infoCount++;
|
||||
log.log(null, callerFQCN, LocationAwareLogger.INFO_INT, msg, null, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void debug(String msg) {
|
||||
log.log(null, callerFQCN, LocationAwareLogger.DEBUG_INT, msg, null, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void debug(String msg, Throwable e) {
|
||||
log.log(null, callerFQCN, LocationAwareLogger.ERROR_INT, msg, null, e);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isWarnEnabled() {
|
||||
return log.isWarnEnabled();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void warn(String msg) {
|
||||
log.log(null, callerFQCN, LocationAwareLogger.WARN_INT, msg, null, null);
|
||||
warnCount++;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void warn(String msg, Throwable e) {
|
||||
log.log(null, callerFQCN, LocationAwareLogger.WARN_INT, msg, null, e);
|
||||
warnCount++;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getErrorCount() {
|
||||
return errorCount;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getWarnCount() {
|
||||
return warnCount;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getInfoCount() {
|
||||
return infoCount;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void resetStat() {
|
||||
errorCount = 0;
|
||||
warnCount = 0;
|
||||
infoCount = 0;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+49
@@ -0,0 +1,49 @@
|
||||
package com.alibaba.druid.bvt.pool.dynamic;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.junit.Assert;
|
||||
|
||||
import com.alibaba.druid.pool.DruidDataSource;
|
||||
import com.alibaba.druid.pool.DruidDataSourceStatLogger;
|
||||
import com.alibaba.druid.pool.DruidDataSourceStatLoggerImpl;
|
||||
import com.alibaba.druid.support.logging.Log;
|
||||
|
||||
public class DruidDataSourceStatLoggerImplTest extends TestCase {
|
||||
|
||||
private DruidDataSource dataSource;
|
||||
|
||||
private Log statLog;
|
||||
|
||||
protected void setUp() throws Exception {
|
||||
Field logField = DruidDataSourceStatLoggerImpl.class.getDeclaredField("LOG");
|
||||
logField.setAccessible(true);
|
||||
statLog = (Log) logField.get(null);
|
||||
|
||||
statLog.resetStat();
|
||||
|
||||
dataSource = new DruidDataSource();
|
||||
dataSource.setUrl("jdbc:mock:xxx");
|
||||
dataSource.setTestOnBorrow(false);
|
||||
dataSource.setConnectionProperties("a=3;b=4");
|
||||
dataSource.setFilters("stat");
|
||||
dataSource.init();
|
||||
|
||||
}
|
||||
|
||||
protected void tearDown() throws Exception {
|
||||
dataSource.close();
|
||||
}
|
||||
|
||||
public void test_connectPropertiesChange() throws Exception {
|
||||
Assert.assertTrue(dataSource.isWrapperFor(DruidDataSourceStatLogger.class));
|
||||
Assert.assertTrue(dataSource.isWrapperFor(DruidDataSourceStatLoggerImpl.class));
|
||||
DruidDataSourceStatLoggerImpl loggerImpl = dataSource.unwrap(DruidDataSourceStatLoggerImpl.class);
|
||||
Assert.assertSame(statLog, loggerImpl.getLogger());
|
||||
|
||||
dataSource.setConnectionProperties("druid.stat.loggerName=xxx");
|
||||
Assert.assertNotSame(statLog, loggerImpl.getLogger());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user