前提
- 使用者需要知道如何配置 logback-spring.xml
解決方案
package io.wetoo.streaming.common.customlog;
import ch.qos.logback.classic.encoder.PatternLayoutEncoder;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.classic.spi.IThrowableProxy;
import ch.qos.logback.classic.spi.LoggerContextVO;
import ch.qos.logback.classic.spi.StackTraceElementProxy;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.ImmutableMap;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class ESEncoder extends PatternLayoutEncoder {
@Override
public byte[] encode(ILoggingEvent event) {
return convertEventWithError(event).getBytes();
}
private String convertEventWithError(ILoggingEvent event) {
try {
// 檢查-------------------------------------------------------
if (event == null)
return "";
if (StringUtils.isBlank(event.getMessage()))
return "";
// 獲取源Msg替換結(jié)果-------------------------------------------
String msg = printReplace(event.getMessage(), event.getArgumentArray());
// 嘗試獲取錯誤日志--------------------------------------------
IThrowableProxy err = event.getThrowableProxy();
StringBuilder errStr = null;
if (err != null) {
errStr = new StringBuilder();
errStr.append(err.getClassName());
errStr.append(" : ");
errStr.append(err.getMessage());
errStr.append(" \n ");
StackTraceElementProxy[] errTrack = err.getStackTraceElementProxyArray();
for (StackTraceElementProxy stackTraceElementProxy : errTrack) {
errStr.append(stackTraceElementProxy.getStackTraceElement());
errStr.append(" \n ");
}
errStr.delete(errStr.length() - 3, errStr.length());
}
// 構(gòu)建Json--------------------------------------------------
LinkedHashMap<String, String> result = new LinkedHashMap();
LoggerContextVO context = event.getLoggerContextVO();
result.put("serverName", context == null ? "default" : context.getName());
result.put("logTime", LocalDateTime.now().toString() + "Z");
result.put("level", event.getLevel().levelStr);
result.put("thread", event.getThreadName());
result.put("logger", event.getLoggerName());
result.put("msg", msg);
if (errStr != null && errStr.length() != 0)
result.put("err", errStr.toString());
// 返回--------------------------------------------------
return (new ObjectMapper()).writeValueAsString(result) + "\n";
} catch (Throwable e) {
try {
return (new ObjectMapper()).writeValueAsString(ImmutableMap.of("err", "ESEncoder日志工具錯誤:" + e.toString())) + "\n";
} catch (Throwable e2) {
return "{\"err\":\"ESEncoder日志工具錯誤\"}\n";
}
}
}
public static String printReplace(String first, Object... replaces) {
try {
int replaceLen = 0;
if (StringUtils.isBlank(first))
return first;
if (replaces == null || (replaceLen = replaces.length) == 0)
return first;
StringBuilder result = new StringBuilder();
int replaceIdx = 0;
int curCpIdx = 0;
Matcher m = Pattern.compile("\\{\\}").matcher(first);
boolean tailed = false;
while (m.find()) {
if (replaceIdx < replaceLen) {
result.append(first.substring(curCpIdx, m.start()));
result.append(replaces[replaceIdx] == null ? "null" : replaces[replaceIdx].toString());
curCpIdx = m.end();
} else {
result.append(first.substring(curCpIdx, first.length()));
tailed = true;
break;
}
replaceIdx++;
}
if (!tailed) result.append(first.substring(curCpIdx, first.length()));
return result.toString();
} catch (Throwable e) {
return first;
}
}
}
<encoder class="com.common.customlog.ESEncoder" >
<pattern> balabalabala </pattern>
</encoder>
<!-- json數(shù)據(jù) -->
<dependency>
<artifactId>jackson-core</artifactId>
<groupId>com.fasterxml.jackson.core</groupId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
</dependency>
<!-- 好用的Google工具庫 -->
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>18.0</version>
</dependency>
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者