Sleuth+logback 设置traceid 及自定义信息方式

网友投稿 583 2022-10-10


Sleuth+logback 设置traceid 及自定义信息方式

Sleuth+logback 设置traceid及自定义信息

背景:

分布式系统中,如何快速定位某个用户的请求日志?

使用Sleuth生成的traceid可以跟踪某个请求,但是很多时候我们需要知道traceid 与某个用户的映射关系,方便定位某个用户的日志

方案:

@Component

@Order(TraceWebServletAutoConfiguration.TRACING_FILTER_ORDER + 1)

public class CustomHttpSpanExtractor extends GenericFilterBean {

private final Tracer tracer;

CustomHttpSpanExtractor(Tracer tracer) {

this.tracer = tracer;

}

@Overrihttp://de

public void doFilter(ServletRequest request, ServletResponse response,

FilterChain chain) throws IOException, ServletException {

Span currentSpan = this.tracer.currentSpan();

if (currentSpan == null) {

chain.doFilter(request, response);

return;

}

HttpServletRequest httpRequest = (HttpServletRequest) request;

try {

// mdc(httpRequest,currentSpan.context().traceIdString());

MDC.put(MdcConstant.USER_ID,"userid-12345");

} catch (Exception e) {

e.printStackTrace();

}

try {

chain.doFilter(request, response);

} finally {

MDC.clear();//must be,threadLocal

}

}}

logback 配置

%d{yyyy-MM-dd HH:mm:ss.SSS}[${applicationName},%X{X-B3-TraceId:-},%X{userId:-}] [%thread] %-5level %logger{50} - %msg%n

logback简单使用,自定义key

1.logback的pom依赖

net.logstash.logback

logstash-logback-encoder

4.11

2.配置文件: logback.xml

${springAppName:-}

{

"time": "%d{yyyy-MM-dd HH:mm:ss}",

"application":"tech_cloud",

"subapplication":"${springAppName:-}",

"level": "%level",

"trace": "%X{X-B3-TraceId:-}",

"span": "%X{X-B3-SpanId:-}",

"parent": "%X{X-B3-ParentSpanId:-}",

"classname": "%logger{100}",

"msg": "%message"

}

0

5120

%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{40} - %msg%n

3.自定义appender

package com.jlb.yts.common.util.aop;

import ch.qos.logback.classic.spi.ILoggingEvent;

import ch.qos.logback.classic.spi.IThrowableProxy;

import ch.qos.logback.classic.spi.LoggingEvent;

import ch.qos.logback.classic.spi.StackTraceElementProxy;

import ch.qos.logback.core.Layout;

import ch.qos.logback.core.UnsynchronizedAppenderBase;

import ch.qos.logback.core.filter.Filter;

import com.jlb.yts.common.util.redis.RedisHandle;

import com.jlb.yts.common.util.redis.ReidsConnect;

import net.logstash.logback.layout.LoggingEventCompositejsonLayout;

import java.lang.reflect.Field;

public class RedisAppender extends UnsynchronizedAppenderBase {

public static String service;

Layout layout;

public void setFilter(Filter filter) {

if(filter != null){

this.addFilter(filter);

}

}

String key = null;

public RedisAppender() {

layout = new LoggingEventCompositeJsonLayout();

}

private final static RedisHandle redisHandle = ReidsConnect.init("redis-log.properties","redis-log.properties");

@Override

protected void append(ILoggingEvent event) {

try {

LoggingEvent loggingEvent = (LoggingEvent) event;

String json = layout.doLayout(loggingEvent);

redisHandle.addList(key,json);

} catch (Exception e) {

e.printStackTrace();

} finally {

}

}

public String getKey() {

return key;

}

public void setKey(String key) {

String prefix = (System.getenv("APPLICATION_ENV") == null)?"dev":System.getenv("APPLICATION_ENV");

key = prefix + "-" + key;

this.key = key;

}

public Layout getLayout() {

return layout;

}

public void setLayout(Layout layout) {

this.layout = layout;

}

}

4.代码实例

//注意导入的包都是slf4j的包

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

public class LogbackTest {

private static final Logger LOGGER = LoggerFactory.getLogger(LogbackTest.class);

public static void main(String[] args) {

LOGGER.trace("logback的--trace日志-http://-输出了");

LOGGER.debug("logback的--debug日志--输出了");

LOGGER.info("logback的--info日志--输出了");

LOGGER.warn("logback的--warn日志--输出了");

LOGGER.error("logback的--error日志--输出了");

}

}

5.日志添加自定义KEY

import org.slf4j.MDC;

MDC.put(key, value);

logback.xml 配置

"key":"%X{key:-}"


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:checkpoint R80初始化安装和smartconsole安装(checkpoint 3ds)
下一篇:网站权重(如何提升网站权重)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~