我的spring boot日志当前如下所示。
{"@timestamp":"2018-08-07T14:49:21.244+01:00","@version":"1","message":"Starting Application on ipkiss bla bla)","logger_name":"logger name....","thread_name":"main","level":"INFO","level_value":20000}
使用如下所示的logback-spring.xml设置
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<encoder class="com.ipkiss.correlate.logback.CorrelationPatternLayoutEncoder">
<pattern>%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr(%5p) %clr(${PID:- }){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} id = %id %m%n%wEx</pattern>
</encoder>
</appender>
我的LayoutEncoder类看起来像这样
public class CorrelationPatternLayoutEncoder extends PatternLayoutEncoder {
public CorrelationPatternLayoutEncoder() {
}
@Override
public void start() {
PatternLayout patternLayout = new PatternLayout();
patternLayout.getDefaultConverterMap().put("id", CorrelationConverter.class.getName());
patternLayout.setContext(context);
patternLayout.setPattern(getPattern());
patternLayout.setOutputPatternAsHeader(outputPatternAsHeader);
patternLayout.start();
this.layout = patternLayout;
this.started = true;
}
}
我要达到的目的是将id添加到日志中,我无法使logstach附加我的id,我尝试根据文档尝试自定义字段,但无法使其正常工作, 有什么想法可以实现这一目标吗?
这就是我要结束的事情
{"id":"3a7ccd34-d66a-4fcc-a12e-763a395a496c","@timestamp":"2018-08-07T14:49:21.244+01:00","@version":"1","message":"Starting Application on ipkiss bla bla)","logger_name":"logger name....","thread_name":"main","level":"INFO","level_value":20000}
在日志末尾添加或ID。
答案 0 :(得分:3)
默认情况下,映射诊断上下文(MDC)(org.slf4j.MDC)中的每个条目将在LoggingEvent中显示为字段。
简而言之,如果您将ID条目添加到MDC中,它将自动包含在所有日志中。
要将您的ID添加到MDC,请执行以下操作:
MDC.put("id", uuid);
由于MDC是线程局部变量,因此您必须在请求使用完毕后将其清除
MDC.remove("id")
在Web应用程序中,通常在Servlet过滤器中完成MDC中的值的添加和清除。
public class IdFilter implements Filter{
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
MDC.put("id", UUID.randomUUID().toString());
try {
filterChain.doFilter(servletRequest, servletResponse);
} catch (Throwable throwable) {
MDC.remove("id");
}
}
}
答案 1 :(得分:0)
您可以通过创建自定义转换说明符来添加自定义日志字段。 首先,创建一个扩展ClassicConverter类的自定义转换器类。
real:
[2, 0, 0;
0, -2, 0;
0, 0, 0]
r:
[2, 0, 0;
0, 2, 0;
0, 0, 0]
theta:
[0, 0, 0;
0, 3.141592741012573, 0;
0, 0, 0]
new_r:
[1.013959479790043, 6.933081232643711e-07, 6.933081232643711e-07;
6.933081232643711e-07, 1.013959479790043, 6.933081232643711e-07;
6.933081232643711e-07, 6.933081232643711e-07, 6.933081232644603e-07]
new_theta:
[0, 0, 0;
0, 0.06283185482025147, 0;
0, 0, 0]
new_re:
[1.013959479790043, 6.933081232643711e-07, 6.933081232643711e-07;
6.933081232643711e-07, 1.011958843888384, 6.933081232643711e-07;
6.933081232643711e-07, 6.933081232643711e-07, 6.933081232644603e-07]
new_im:
[0, 0, 0;
0, 0.06366705860016499, 0;
0, 0, 0]
ClassicConverter 对象负责从ILoggingEvent实例中提取信息并生成String。
因此,只需在您的logback配置文件中写入信息,以使logback了解新Converter。为此,您只需要在配置文件中声明新的转换字,如下所示:
import ch.qos.logback.classic.pattern.ClassicConverter;
import ch.qos.logback.classic.spi.ILoggingEvent;
import java.util.UUID;
public class LogUniqueIdConverter extends ClassicConverter {
@Override
public String convert(ILoggingEvent event) {
return String.valueOf(UUID.randomUUID());
}
}