Skip to content

Instantly share code, notes, and snippets.

@bohrqiu
Created May 2, 2013 17:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bohrqiu/5503811 to your computer and use it in GitHub Desktop.
Save bohrqiu/5503811 to your computer and use it in GitHub Desktop.
druid slf4j日志集成
diff --git a/src/main/java/com/alibaba/druid/support/logging/LogFactory.java b/src/main/java/com/alibaba/druid/support/logging/LogFactory.java
index dbdc21a..fe6f467 100644
--- a/src/main/java/com/alibaba/druid/support/logging/LogFactory.java
+++ b/src/main/java/com/alibaba/druid/support/logging/LogFactory.java
@@ -23,9 +23,7 @@
private static Constructor logConstructor;
static {
- // TODO add slf4j logging
-
- // 浼樺厛閫夋嫨log4j,鑰岄潪Apache Common Logging. 鍥犱负鍚庤?鏃犳硶璁剧疆鐪熷疄Log璋冪敤鑰呯殑淇℃伅
+ tryImplementation("org.slf4j.Logger", "com.alibaba.druid.support.logging.SLF4JImpl");
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",
@@ -57,7 +55,7 @@
logConstructor = null;
}
} catch (Throwable t) {
- t.printStackTrace();
+// t.printStackTrace();
}
}
diff --git a/src/main/java/com/alibaba/druid/support/logging/SLF4JImpl.java b/src/main/java/com/alibaba/druid/support/logging/SLF4JImpl.java
new file mode 100644
index 0000000..bd4e3b9
--- /dev/null
+++ b/src/main/java/com/alibaba/druid/support/logging/SLF4JImpl.java
@@ -0,0 +1,105 @@
+package com.alibaba.druid.support.logging;
+
+import org.slf4j.Logger;
+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 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++;
+ }
+
+ @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;
+ }
+
+}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment