Integrate log4j2 kafka appender
This commit is contained in:
@@ -17,6 +17,16 @@
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.apache.logging.log4j</groupId>
|
||||
<artifactId>log4j-layout-template-json</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.apache.kafka</groupId>
|
||||
<artifactId>kafka-clients</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.openjdk.jol</groupId>
|
||||
<artifactId>jol-core</artifactId>
|
||||
|
||||
@@ -17,24 +17,29 @@
|
||||
|
||||
package we.flume.clients.log4j2appender;
|
||||
|
||||
import org.apache.logging.log4j.ThreadContext;
|
||||
import we.constants.CommonConstants;
|
||||
import we.util.Consts;
|
||||
|
||||
public enum LogService {
|
||||
|
||||
BIZ_ID, HANDLE_STGY, APP;
|
||||
|
||||
public static void cleanBizId() {
|
||||
setBizId(null);
|
||||
// setBizId(null);
|
||||
ThreadContext.remove(Consts.TRACE_ID);
|
||||
}
|
||||
|
||||
public static Object getBizId() {
|
||||
return ThreadContext.get(Constants.BIZ_ID);
|
||||
// return ThreadContext.get(Constants.BIZ_ID);
|
||||
return ThreadContext.get(Consts.TRACE_ID);
|
||||
}
|
||||
|
||||
public static void setBizId(Object bizId) {
|
||||
ThreadContext.set(Constants.BIZ_ID, bizId);
|
||||
// ThreadContext.set(Constants.BIZ_ID, bizId);
|
||||
if (bizId != null) {
|
||||
org.apache.logging.log4j.ThreadContext.put(CommonConstants.TRACE_ID, String.valueOf(bizId));
|
||||
// org.apache.logging.log4j.ThreadContext.put(CommonConstants.TRACE_ID, String.valueOf(bizId));
|
||||
ThreadContext.put(Consts.TRACE_ID, String.valueOf(bizId));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -45,7 +50,7 @@ public enum LogService {
|
||||
public static String toESaKF(String topic) {
|
||||
return Constants.AND + topic;
|
||||
}
|
||||
|
||||
|
||||
public static class Constants {
|
||||
static final String BIZ_ID = "bizId";
|
||||
static final char AND = '&';
|
||||
|
||||
@@ -1,90 +1,90 @@
|
||||
/*
|
||||
* Copyright (C) 2020 the original author or authors.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package we.flume.clients.log4j2appender;
|
||||
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/** for internal use */
|
||||
public abstract class ThreadContext {
|
||||
|
||||
private static ThreadLocal<Map<String, Object>> tl = new ThreadLocal<>();
|
||||
private static final int mapCap = 32;
|
||||
|
||||
private static final String sb = "sb";
|
||||
private static final int sbCap = 256;
|
||||
|
||||
public static StringBuilder getStringBuilder() {
|
||||
return getStringBuilder(true);
|
||||
}
|
||||
|
||||
public static StringBuilder getStringBuilder(boolean clean) {
|
||||
Map<String, Object> m = getMap();
|
||||
StringBuilder b = (StringBuilder) m.get(sb);
|
||||
if (b == null) {
|
||||
b = new StringBuilder(sbCap);
|
||||
m.put(sb, b);
|
||||
} else {
|
||||
if (clean) {
|
||||
b.delete(0, b.length());
|
||||
}
|
||||
}
|
||||
return b;
|
||||
}
|
||||
|
||||
public static SimpleDateFormat getSimpleDateFormat(String pattern) {
|
||||
Map<String, Object> m = getMap();
|
||||
SimpleDateFormat sdf = (SimpleDateFormat) m.get(pattern);
|
||||
if (sdf == null) {
|
||||
sdf = new SimpleDateFormat(pattern);
|
||||
m.put(pattern, sdf);
|
||||
}
|
||||
return sdf;
|
||||
}
|
||||
|
||||
public static Object get(String key, Class<?> clz) {
|
||||
Object obj = get(key);
|
||||
if (obj == null) {
|
||||
try {
|
||||
obj = clz.newInstance();
|
||||
set(key, obj);
|
||||
} catch (InstantiationException | IllegalAccessException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
return obj;
|
||||
}
|
||||
|
||||
private static Map<String, Object> getMap() {
|
||||
Map<String, Object> m = tl.get();
|
||||
if (m == null) {
|
||||
m = new HashMap<>(mapCap);
|
||||
tl.set(m);
|
||||
}
|
||||
return m;
|
||||
}
|
||||
|
||||
public static Object get(String key) {
|
||||
return getMap().get(key);
|
||||
}
|
||||
|
||||
public static void set(String key, Object obj) {
|
||||
getMap().put(key, obj);
|
||||
}
|
||||
}
|
||||
///*
|
||||
// * Copyright (C) 2020 the original author or authors.
|
||||
// *
|
||||
// * This program is free software: you can redistribute it and/or modify
|
||||
// * it under the terms of the GNU General Public License as published by
|
||||
// * the Free Software Foundation, either version 3 of the License, or
|
||||
// * any later version.
|
||||
// *
|
||||
// * This program is distributed in the hope that it will be useful,
|
||||
// * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// * GNU General Public License for more details.
|
||||
// *
|
||||
// * You should have received a copy of the GNU General Public License
|
||||
// * along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
// */
|
||||
//
|
||||
//package we.flume.clients.log4j2appender;
|
||||
//
|
||||
//import java.text.SimpleDateFormat;
|
||||
//import java.util.HashMap;
|
||||
//import java.util.Map;
|
||||
//
|
||||
///** for internal use */
|
||||
//public abstract class ThreadContext {
|
||||
//
|
||||
// private static ThreadLocal<Map<String, Object>> tl = new ThreadLocal<>();
|
||||
// private static final int mapCap = 32;
|
||||
//
|
||||
// private static final String sb = "sb";
|
||||
// private static final int sbCap = 256;
|
||||
//
|
||||
// public static StringBuilder getStringBuilder() {
|
||||
// return getStringBuilder(true);
|
||||
// }
|
||||
//
|
||||
// public static StringBuilder getStringBuilder(boolean clean) {
|
||||
// Map<String, Object> m = getMap();
|
||||
// StringBuilder b = (StringBuilder) m.get(sb);
|
||||
// if (b == null) {
|
||||
// b = new StringBuilder(sbCap);
|
||||
// m.put(sb, b);
|
||||
// } else {
|
||||
// if (clean) {
|
||||
// b.delete(0, b.length());
|
||||
// }
|
||||
// }
|
||||
// return b;
|
||||
// }
|
||||
//
|
||||
// public static SimpleDateFormat getSimpleDateFormat(String pattern) {
|
||||
// Map<String, Object> m = getMap();
|
||||
// SimpleDateFormat sdf = (SimpleDateFormat) m.get(pattern);
|
||||
// if (sdf == null) {
|
||||
// sdf = new SimpleDateFormat(pattern);
|
||||
// m.put(pattern, sdf);
|
||||
// }
|
||||
// return sdf;
|
||||
// }
|
||||
//
|
||||
// public static Object get(String key, Class<?> clz) {
|
||||
// Object obj = get(key);
|
||||
// if (obj == null) {
|
||||
// try {
|
||||
// obj = clz.newInstance();
|
||||
// set(key, obj);
|
||||
// } catch (InstantiationException | IllegalAccessException e) {
|
||||
// throw new RuntimeException(e);
|
||||
// }
|
||||
// }
|
||||
// return obj;
|
||||
// }
|
||||
//
|
||||
// private static Map<String, Object> getMap() {
|
||||
// Map<String, Object> m = tl.get();
|
||||
// if (m == null) {
|
||||
// m = new HashMap<>(mapCap);
|
||||
// tl.set(m);
|
||||
// }
|
||||
// return m;
|
||||
// }
|
||||
//
|
||||
// public static Object get(String key) {
|
||||
// return getMap().get(key);
|
||||
// }
|
||||
//
|
||||
// public static void set(String key, Object obj) {
|
||||
// getMap().put(key, obj);
|
||||
// }
|
||||
//}
|
||||
|
||||
@@ -107,8 +107,8 @@ public final class Consts {
|
||||
public static final int GB = 1024 * MB;
|
||||
}
|
||||
|
||||
public static final String HTTP_SERVER = "http_server";
|
||||
public static final String HTTP_CLIENT = "http_client";
|
||||
public static final String HTTP_SERVER = "httpServer";
|
||||
public static final String HTTP_CLIENT = "httpClient";
|
||||
public static final String MYSQL = "mysql";
|
||||
public static final String REDIS = "redis";
|
||||
public static final String CODIS = "codis";
|
||||
@@ -118,5 +118,5 @@ public final class Consts {
|
||||
public static final String SCHED = "sched";
|
||||
public static final String R2DBC = "r2dbc";
|
||||
|
||||
public static final String TRACE_ID = "id^";
|
||||
public static final String TRACE_ID = "traceId";
|
||||
}
|
||||
|
||||
@@ -17,18 +17,11 @@
|
||||
|
||||
package we.util;
|
||||
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import io.netty.buffer.ByteBufAllocator;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.core.io.buffer.DataBuffer;
|
||||
import org.springframework.core.io.buffer.NettyDataBuffer;
|
||||
import org.springframework.core.io.buffer.NettyDataBufferFactory;
|
||||
import org.springframework.core.io.buffer.PooledDataBuffer;
|
||||
import org.springframework.lang.Nullable;
|
||||
import we.flume.clients.log4j2appender.LogService;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
/**
|
||||
@@ -37,8 +30,6 @@ import java.nio.charset.StandardCharsets;
|
||||
|
||||
public abstract class NettyDataBufferUtils extends org.springframework.core.io.buffer.DataBufferUtils {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(NettyDataBufferUtils.class);
|
||||
|
||||
private static NettyDataBufferFactory dataBufferFactory = new NettyDataBufferFactory(ByteBufAllocator.DEFAULT);
|
||||
|
||||
public static final DataBuffer EMPTY_DATA_BUFFER = from(new byte[0]);
|
||||
@@ -54,14 +45,6 @@ public abstract class NettyDataBufferUtils extends org.springframework.core.io.b
|
||||
return (NettyDataBuffer) dataBufferFactory.wrap(bytes);
|
||||
}
|
||||
|
||||
/*public static NettyDataBuffer from(ByteBuffer byteBuffer) {
|
||||
return dataBufferFactory.wrap(byteBuffer);
|
||||
}
|
||||
|
||||
public static NettyDataBuffer from(ByteBuf byteBuf) {
|
||||
return dataBufferFactory.wrap(byteBuf);
|
||||
}*/
|
||||
|
||||
public static byte[] copyBytes(DataBuffer dataBuffer) {
|
||||
byte[] bytes = new byte[dataBuffer.readableByteCount()];
|
||||
dataBuffer.read(bytes);
|
||||
@@ -71,25 +54,4 @@ public abstract class NettyDataBufferUtils extends org.springframework.core.io.b
|
||||
public static DataBuffer copy2heap(DataBuffer dataBuffer) {
|
||||
return from(copyBytes(dataBuffer));
|
||||
}
|
||||
|
||||
/*public static boolean release(@Nullable String traceId, @Nullable DataBuffer dataBuffer) {
|
||||
if (dataBuffer instanceof PooledDataBuffer) {
|
||||
PooledDataBuffer pooledDataBuffer = (PooledDataBuffer) dataBuffer;
|
||||
if (pooledDataBuffer.isAllocated()) {
|
||||
if (pooledDataBuffer instanceof NettyDataBuffer) {
|
||||
NettyDataBuffer ndb = (NettyDataBuffer) pooledDataBuffer;
|
||||
ByteBuf nativeBuffer = ndb.getNativeBuffer();
|
||||
int refCnt = nativeBuffer.refCnt();
|
||||
if (refCnt < 1) {
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug(nativeBuffer + " ref cnt is " + refCnt, LogService.BIZ_ID, traceId);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return pooledDataBuffer.release();
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}*/
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user