diff --git a/easy-retry-client/easy-retry-client-common/src/main/java/com/aizuda/easy/retry/client/common/appender/EasyRetryLog4j2Appender.java b/easy-retry-client/easy-retry-client-common/src/main/java/com/aizuda/easy/retry/client/common/appender/EasyRetryLog4j2Appender.java
index e3d5b9fa..06c5e3c8 100644
--- a/easy-retry-client/easy-retry-client-common/src/main/java/com/aizuda/easy/retry/client/common/appender/EasyRetryLog4j2Appender.java
+++ b/easy-retry-client/easy-retry-client-common/src/main/java/com/aizuda/easy/retry/client/common/appender/EasyRetryLog4j2Appender.java
@@ -5,6 +5,7 @@ import com.aizuda.easy.retry.client.common.report.AsyncReportLog;
import com.aizuda.easy.retry.client.common.util.ThreadLocalLogUtil;
import com.aizuda.easy.retry.common.core.constant.LogFieldConstant;
import com.aizuda.easy.retry.common.core.context.SpringContext;
+import org.apache.log4j.MDC;
import org.apache.logging.log4j.core.Filter;
import org.apache.logging.log4j.core.Layout;
import org.apache.logging.log4j.core.LogEvent;
@@ -15,7 +16,6 @@ import org.apache.logging.log4j.core.config.plugins.PluginElement;
import org.apache.logging.log4j.core.config.plugins.PluginFactory;
import org.apache.logging.log4j.core.util.Booleans;
import org.apache.logging.log4j.core.util.Throwables;
-import org.slf4j.MDC;
import java.io.Serializable;
import java.util.Objects;
diff --git a/easy-retry-common/easy-retry-common-log/src/main/java/com/aizuda/easy/retry/common/log/LogFactory.java b/easy-retry-common/easy-retry-common-log/src/main/java/com/aizuda/easy/retry/common/log/LogFactory.java
index 2cc1660a..aa87e257 100644
--- a/easy-retry-common/easy-retry-common-log/src/main/java/com/aizuda/easy/retry/common/log/LogFactory.java
+++ b/easy-retry-common/easy-retry-common-log/src/main/java/com/aizuda/easy/retry/common/log/LogFactory.java
@@ -181,4 +181,16 @@ public abstract class LogFactory {
return (null != url) ? new JdkLogFactory() : new ConsoleLogFactory();
}
// ------------------------------------------------------------------------- Static end
+
+ public static final Throwable extractThrowable(Object... arguments) {
+ if (arguments == null || arguments.length == 0) {
+ return null;
+ }
+
+ final Object lastEntry = arguments[arguments.length - 1];
+ if (lastEntry instanceof Throwable) {
+ return (Throwable) lastEntry;
+ }
+ return null;
+ }
}
diff --git a/easy-retry-common/easy-retry-common-log/src/main/java/com/aizuda/easy/retry/common/log/dialect/log4j/Log4jLog.java b/easy-retry-common/easy-retry-common-log/src/main/java/com/aizuda/easy/retry/common/log/dialect/log4j/Log4jLog.java
index f3e08f3c..19ca8166 100644
--- a/easy-retry-common/easy-retry-common-log/src/main/java/com/aizuda/easy/retry/common/log/dialect/log4j/Log4jLog.java
+++ b/easy-retry-common/easy-retry-common-log/src/main/java/com/aizuda/easy/retry/common/log/dialect/log4j/Log4jLog.java
@@ -1,9 +1,12 @@
package com.aizuda.easy.retry.common.log.dialect.log4j;
import cn.hutool.core.util.StrUtil;
+import com.aizuda.easy.retry.common.core.constant.LogFieldConstant;
import com.aizuda.easy.retry.common.log.AbstractLog;
+import com.aizuda.easy.retry.common.log.LogFactory;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
+import org.apache.log4j.MDC;
/**
* Apache Log4J log.
@@ -113,6 +116,12 @@ public class Log4jLog extends AbstractLog {
}
if (logger.isEnabledFor(log4jLevel)) {
+ if (remote) {
+ MDC.put(LogFieldConstant.MDC_REMOTE, remote.toString());
+ }
+ if (t == null) {
+ t = LogFactory.extractThrowable(arguments);
+ }
logger.log(fqcn, log4jLevel, StrUtil.format(format, arguments), t);
}
}
diff --git a/easy-retry-common/easy-retry-common-log/src/main/java/com/aizuda/easy/retry/common/log/dialect/log4j2/Log4j2Log.java b/easy-retry-common/easy-retry-common-log/src/main/java/com/aizuda/easy/retry/common/log/dialect/log4j2/Log4j2Log.java
index bc56e1fa..73f5a0a1 100644
--- a/easy-retry-common/easy-retry-common-log/src/main/java/com/aizuda/easy/retry/common/log/dialect/log4j2/Log4j2Log.java
+++ b/easy-retry-common/easy-retry-common-log/src/main/java/com/aizuda/easy/retry/common/log/dialect/log4j2/Log4j2Log.java
@@ -1,7 +1,10 @@
package com.aizuda.easy.retry.common.log.dialect.log4j2;
import cn.hutool.core.util.StrUtil;
+import com.aizuda.easy.retry.common.core.constant.LogFieldConstant;
import com.aizuda.easy.retry.common.log.AbstractLog;
+import com.aizuda.easy.retry.common.log.LogFactory;
+import org.apache.log4j.MDC;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
@@ -113,7 +116,7 @@ public class Log4j2Log extends AbstractLog {
default:
throw new Error(StrUtil.format("Can not identify level: {}", level));
}
- logIfEnabled(fqcn, log4j2Level, t, format, arguments);
+ logIfEnabled(fqcn, log4j2Level, t, format, remote, arguments);
}
// ------------------------------------------------------------------------- Private method
@@ -128,12 +131,20 @@ public class Log4j2Log extends AbstractLog {
* @param msgTemplate 消息模板
* @param arguments 参数
*/
- private void logIfEnabled(String fqcn, Level level, Throwable t, String msgTemplate, Object... arguments) {
+ private void logIfEnabled(String fqcn, Level level, Throwable t, String msgTemplate, Boolean remote, Object... arguments) {
if (this.logger.isEnabled(level)) {
+
+ if (remote) {
+ MDC.put(LogFieldConstant.MDC_REMOTE, remote.toString());
+ }
if (this.logger instanceof AbstractLogger) {
((AbstractLogger) this.logger).logIfEnabled(fqcn, level, null, StrUtil.format(msgTemplate, arguments), t);
} else {
// FQCN无效
+ //this.logger.log(level, StrUtil.format(msgTemplate, arguments), t);
+ if (t == null) {
+ t = LogFactory.extractThrowable(arguments);
+ }
this.logger.log(level, StrUtil.format(msgTemplate, arguments), t);
}
}
diff --git a/easy-retry-common/easy-retry-common-log/src/main/java/com/aizuda/easy/retry/common/log/dialect/slf4j/Slf4jLog.java b/easy-retry-common/easy-retry-common-log/src/main/java/com/aizuda/easy/retry/common/log/dialect/slf4j/Slf4jLog.java
index cf9cda1a..4f8138de 100644
--- a/easy-retry-common/easy-retry-common-log/src/main/java/com/aizuda/easy/retry/common/log/dialect/slf4j/Slf4jLog.java
+++ b/easy-retry-common/easy-retry-common-log/src/main/java/com/aizuda/easy/retry/common/log/dialect/slf4j/Slf4jLog.java
@@ -56,10 +56,15 @@ public class Slf4jLog extends AbstractLog {
@Override
public void trace(String fqcn, Throwable t, String format, Boolean remote, Object... arguments) {
if (isTraceEnabled()) {
+ setContextMap(remote);
if (this.isLocationAwareLogger) {
- locationAwareLog((LocationAwareLogger) this.logger, fqcn, LocationAwareLogger.TRACE_INT, t, format, remote, arguments);
+ locationAwareLog((LocationAwareLogger) this.logger, fqcn, LocationAwareLogger.TRACE_INT, t, format, arguments);
} else {
- logger.trace(StrUtil.format(format, arguments), t);
+ if (Objects.nonNull(t)) {
+ this.logger.trace(StrUtil.format(format, arguments), t);
+ } else {
+ this.logger.trace(format, arguments);
+ }
}
}
}
@@ -73,10 +78,15 @@ public class Slf4jLog extends AbstractLog {
@Override
public void debug(String fqcn, Throwable t, String format, Boolean remote, Object... arguments) {
if (isDebugEnabled()) {
+ setContextMap(remote);
if (this.isLocationAwareLogger) {
- locationAwareLog((LocationAwareLogger) this.logger, fqcn, LocationAwareLogger.DEBUG_INT, t, format, remote, arguments);
+ locationAwareLog((LocationAwareLogger) this.logger, fqcn, LocationAwareLogger.DEBUG_INT, t, format, arguments);
} else {
- logger.debug(StrUtil.format(format, arguments), t);
+ if (Objects.nonNull(t)) {
+ this.logger.debug(StrUtil.format(format, arguments), t);
+ } else {
+ this.logger.debug(format, arguments);
+ }
}
}
}
@@ -90,10 +100,15 @@ public class Slf4jLog extends AbstractLog {
@Override
public void info(String fqcn, Throwable t, String format, Boolean remote, Object... arguments) {
if (isInfoEnabled()) {
+ setContextMap(remote);
if (this.isLocationAwareLogger) {
- locationAwareLog((LocationAwareLogger) this.logger, fqcn, LocationAwareLogger.INFO_INT, t, format, remote, arguments);
+ locationAwareLog((LocationAwareLogger) this.logger, fqcn, LocationAwareLogger.INFO_INT, t, format, arguments);
} else {
- logger.info(StrUtil.format(format, arguments), t);
+ if (Objects.nonNull(t)) {
+ this.logger.info(StrUtil.format(format, arguments), t);
+ } else {
+ this.logger.info(format, arguments);
+ }
}
}
}
@@ -107,10 +122,15 @@ public class Slf4jLog extends AbstractLog {
@Override
public void warn(String fqcn, Throwable t, String format, Boolean remote, Object... arguments) {
if (isWarnEnabled()) {
+ setContextMap(remote);
if (this.isLocationAwareLogger) {
- locationAwareLog((LocationAwareLogger) this.logger, fqcn, LocationAwareLogger.WARN_INT, t, format, remote, arguments);
+ locationAwareLog((LocationAwareLogger) this.logger, fqcn, LocationAwareLogger.WARN_INT, t, format, arguments);
} else {
- logger.warn(StrUtil.format(format, arguments), t);
+ if (Objects.nonNull(t)) {
+ this.logger.warn(StrUtil.format(format, arguments), t);
+ } else {
+ this.logger.warn(format, arguments);
+ }
}
}
}
@@ -124,15 +144,15 @@ public class Slf4jLog extends AbstractLog {
@Override
public void error(String fqcn, Throwable t, String format, Boolean remote, Object... arguments) {
if (isErrorEnabled()) {
+ setContextMap(remote);
if (this.isLocationAwareLogger) {
- locationAwareLog((LocationAwareLogger) this.logger, fqcn, LocationAwareLogger.ERROR_INT, t, format, remote, arguments);
+ locationAwareLog((LocationAwareLogger) this.logger, fqcn, LocationAwareLogger.ERROR_INT, t, format, arguments);
} else {
if (Objects.nonNull(t)) {
- logger.error(format, t);
+ this.logger.error(StrUtil.format(format, arguments), t);
} else {
- logger.error(format, arguments);
+ this.logger.error(format, arguments);
}
-
}
}
}
@@ -176,14 +196,9 @@ public class Slf4jLog extends AbstractLog {
* @param msgTemplate 消息模板
* @param arguments 参数
*/
- private void locationAwareLog(LocationAwareLogger logger, String fqcn, int level_int, Throwable t, String msgTemplate, Boolean remote, Object[] arguments) {
+ private void locationAwareLog(LocationAwareLogger logger, String fqcn, int level_int, Throwable t, String msgTemplate, Object[] arguments) {
// ((LocationAwareLogger)this.logger).log(null, fqcn, level_int, msgTemplate, arguments, t);
// 由于slf4j-log4j12中此方法的实现存在bug,故在此拼接参数
- if (remote) {
- Map map = new LinkedHashMap<>();
- map.put(LogFieldConstant.MDC_REMOTE, remote.toString());
- MDC.getMDCAdapter().setContextMap(map);
- }
logger.log(null, fqcn, level_int, msgTemplate, arguments, t);
}
@@ -196,4 +211,17 @@ public class Slf4jLog extends AbstractLog {
private static Logger getSlf4jLogger(Class> clazz) {
return (null == clazz) ? LoggerFactory.getLogger(StrUtil.EMPTY) : LoggerFactory.getLogger(clazz);
}
+
+ /**
+ * 设置MDC上下文
+ *
+ * @param remote
+ */
+ private void setContextMap(Boolean remote) {
+ if (remote) {
+ Map map = new LinkedHashMap<>();
+ map.put(LogFieldConstant.MDC_REMOTE, remote.toString());
+ MDC.setContextMap(map);
+ }
+ }
}