feat: 2.6.0
1. 修复工作流实时日志问题 !43 【轻量级 PR】:Logback、Log4j、Log4j2 打印异常堆栈信息 * fix: 2.6.0:
This commit is contained in:
parent
03306652b1
commit
7b38398a6e
@ -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;
|
||||
|
@ -11,7 +11,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.slf4j.MDC;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
@ -31,9 +31,12 @@ public class EasyRetryLogbackAppender<E> extends UnsynchronizedAppenderBase<E> {
|
||||
protected void append(E eventObject) {
|
||||
|
||||
// Not job context
|
||||
if (!(eventObject instanceof LoggingEvent) || Objects.isNull(ThreadLocalLogUtil.getContext()) || Objects.isNull(MDC.get(LogFieldConstant.MDC_REMOTE))) {
|
||||
if (!(eventObject instanceof LoggingEvent)
|
||||
|| Objects.isNull(ThreadLocalLogUtil.getContext())
|
||||
|| Objects.isNull(MDC.get(LogFieldConstant.MDC_REMOTE))) {
|
||||
return;
|
||||
}
|
||||
|
||||
MDC.remove(LogFieldConstant.MDC_REMOTE);
|
||||
LogContentDTO logContentDTO = new LogContentDTO();
|
||||
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
@ -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;
|
||||
|
||||
/**
|
||||
* <a href="http://logging.apache.org/log4j/1.2/index.html">Apache Log4J</a> log.<br>
|
||||
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -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<String, String> 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<String, String> map = new LinkedHashMap<>();
|
||||
map.put(LogFieldConstant.MDC_REMOTE, remote.toString());
|
||||
MDC.setContextMap(map);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -22,10 +22,7 @@ import com.google.common.collect.Lists;
|
||||
import org.slf4j.MDC;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.*;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.stream.Collectors;
|
||||
@ -52,7 +49,6 @@ public class EasyRetryServerLogbackAppender<E> extends UnsynchronizedAppenderBas
|
||||
LoggingEvent event = (LoggingEvent) eventObject;
|
||||
|
||||
LogContentDTO logContentDTO = new LogContentDTO();
|
||||
logContentDTO.addTimeStamp(event.getTimeStamp());
|
||||
logContentDTO.addLevelField(event.getLevel().levelStr);
|
||||
logContentDTO.addThreadField(event.getThreadName());
|
||||
logContentDTO.addLocationField(getLocationField(event));
|
||||
@ -72,6 +68,7 @@ public class EasyRetryServerLogbackAppender<E> extends UnsynchronizedAppenderBas
|
||||
logMetaDTO = JsonUtil.parseObject(extractedData, LogMetaDTO.class);
|
||||
String message = event.getFormattedMessage().replaceFirst(patternString, StrUtil.EMPTY);
|
||||
logContentDTO.addMessageField(message);
|
||||
logContentDTO.addTimeStamp(Optional.ofNullable(logMetaDTO.getTimestamp()).orElse(event.getTimeStamp()));
|
||||
break;
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user