org.slf4j.MDC.get()方法的使用及代码示例

x33g5p2x  于2022-01-24 转载在 其他  
字(6.3k)|赞(0)|评价(0)|浏览(612)

本文整理了Java中org.slf4j.MDC.get()方法的一些代码示例,展示了MDC.get()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。MDC.get()方法的具体详情如下:
包路径:org.slf4j.MDC
类名称:MDC
方法名:get

MDC.get介绍

[英]Get the diagnostic context identified by the key parameter. The key parameter cannot be null.

This method delegates all work to the MDC of the underlying logging system.
[中]获取由key参数标识的诊断上下文。key参数不能为空。
此方法将所有工作委托给底层日志系统的MDC。

代码示例

代码示例来源:origin: ch.qos.logback/logback-classic

@Override
public FilterReply decide(Marker marker, Logger logger, Level level, String format, Object[] params, Throwable t) {
  if (MDCKey == null) {
    return FilterReply.NEUTRAL;
  }
  String value = MDC.get(MDCKey);
  if (this.value.equals(value)) {
    return onMatch;
  }
  return onMismatch;
}

代码示例来源:origin: stagemonitor/stagemonitor

@Test
public void testMdc() throws Exception {
  Stagemonitor.reset(new MeasurementSession("MDCSpanEventListenerTest", "testHost", "testInstance"));
  when(corePlugin.getMeasurementSession())
      .thenReturn(new MeasurementSession("MDCSpanEventListenerTest", "testHost", "testInstance"));
  mdcSpanInterceptor.onStart(spanWrapper);
  assertNotNull(MDC.get("spanId"));
  assertNotNull(MDC.get("traceId"));
  assertNull(MDC.get("parentId"));
  mdcSpanInterceptor.onFinish(spanWrapper, null, 0);
  assertThat(MDC.getCopyOfContextMap()).isEmpty();
}

代码示例来源:origin: org.slf4j/log4j-over-slf4j

public static int getDepth() {
  int i = 0;
  while (true) {
    String val = MDC.get(PREFIX + i);
    if (val != null) {
      i++;
    } else {
      break;
    }
  }
  return i;
}

代码示例来源:origin: scouter-project/scouter

public CommonResultView(int status, int resultCode, String message, T result) {
  this.status = status;
  this.resultCode = resultCode;
  this.message = message;
  this.result = result;
  this.requestId = MDC.get(LoggingInitServletFilter.requestId);
}

代码示例来源:origin: wildfly/wildfly

public Object getMdc(final String key) {
  return MDC.get(key);
}

代码示例来源:origin: scouter-project/scouter

public CommonResultView(int resultCode, String message, T result) {
  this.resultCode = resultCode;
  this.message = message;
  this.result = result;
  this.requestId = MDC.get(LoggingInitServletFilter.requestId);
}

代码示例来源:origin: org.slf4j/log4j-over-slf4j

public static Object get(String key) {
  return org.slf4j.MDC.get(key);
}

代码示例来源:origin: openzipkin/brave

@Override protected String get(String key) {
 return MDC.get(key);
}

代码示例来源:origin: wildfly/wildfly

public Object putMdc(final String key, final Object value) {
  try {
    return MDC.get(key);
  } finally {
    if (value == null) {
      MDC.remove(key);
    } else {
      MDC.put(key, String.valueOf(value));
    }
  }
}

代码示例来源:origin: org.slf4j/log4j-over-slf4j

public static String pop() {
  int next = getDepth();
  if (next == 0) {
    return "";
  }
  int last = next - 1;
  String key = PREFIX + last;
  String val = MDC.get(key);
  MDC.remove(key);
  return val;
}

代码示例来源:origin: org.slf4j/log4j-over-slf4j

public static String peek() {
  int next = getDepth();
  if (next == 0) {
    return "";
  }
  int last = next - 1;
  String key = PREFIX + last;
  String val = MDC.get(key);
  return val;
}

代码示例来源:origin: ch.qos.logback/logback-classic

public FilterReply decide(Marker marker, Logger logger, Level level, String s, Object[] objects, Throwable throwable) {
  String mdcValue = MDC.get(this.key);
  if (!isStarted()) {
    return FilterReply.NEUTRAL;

代码示例来源:origin: org.freemarker/freemarker

/**
 * Returns if Log4j-over-SLF4J is actually working. Sometimes the API classes are present, but there's no SLF4J
 * implementation around.
 */
public static final boolean test() {
  org.apache.log4j.MDC.put(MDC_KEY, "");
  try {
    return org.slf4j.MDC.get(MDC_KEY) != null;
  } finally {
    org.apache.log4j.MDC.remove(MDC_KEY);
  }
}

代码示例来源:origin: pentaho/pentaho-kettle

sbuf.append( MDC.get( PurgeUtilityLog.FILE_KEY ) );
 sbuf.append( MDC.get( PurgeUtilityLogger.CODE_LINE ) );

代码示例来源:origin: SonarSource/sonarqube

@Test
public void filter_put_id_in_MDC_and_remove_it_after_chain_has_executed() throws IOException, ServletException {
 String requestId = "request id";
 when(requestIdGenerator.generate()).thenReturn(requestId);
 doAnswer(invocation -> assertThat(MDC.get("HTTP_REQUEST_ID")).isEqualTo(requestId))
  .when(filterChain)
  .doFilter(servletRequest, servletResponse);
 underTest.doFilter(servletRequest, servletResponse, filterChain);
 assertThat(MDC.get("HTTP_REQUEST_ID")).isNull();
}

代码示例来源:origin: alibaba/yugong

public List<Record> translator(final DataSource sourceDs, final DataSource targetDs, final List<Record> records,
                final ExecutorTemplate template) {
  final List<Record> result = Collections.synchronizedList(new ArrayList<Record>());
  final String spiltKey = MDC.get(YuGongConstants.MDC_TABLE_SHIT_KEY);
  for (final Record record : records) {
    if (template != null) {

代码示例来源:origin: SonarSource/sonarqube

@Test
public void initForTask_stores_task_uuid_in_MDC() {
 String uuid = "ce_task_uuid";
 underTest.initForTask(createCeTask(uuid));
 assertThat(MDC.get(MDC_CE_TASK_UUID)).isEqualTo(uuid);
}

代码示例来源:origin: SonarSource/sonarqube

@Test
public void clearForTask_removes_task_uuid_from_MDC() {
 MDC.put(MDC_CE_TASK_UUID, "some_value");
 underTest.clearForTask();
 assertThat(MDC.get(MDC_CE_TASK_UUID)).isNull();
}

代码示例来源:origin: SonarSource/sonarqube

@Test
public void filter_put_id_in_MDC_and_remove_it_after_chain_throws_exception() throws IOException, ServletException {
 RuntimeException exception = new RuntimeException("Simulating chain failing");
 String requestId = "request id";
 when(requestIdGenerator.generate()).thenReturn(requestId);
 doAnswer(invocation -> {
  assertThat(MDC.get("HTTP_REQUEST_ID")).isEqualTo(requestId);
  throw exception;
 })
  .when(filterChain)
  .doFilter(servletRequest, servletResponse);
 try {
  underTest.doFilter(servletRequest, servletResponse, filterChain);
  fail("A runtime exception should have been raised");
 } catch (RuntimeException e) {
  assertThat(e).isEqualTo(exception);
 } finally {
  assertThat(MDC.get("HTTP_REQUEST_ID")).isNull();
 }
}

代码示例来源:origin: stagemonitor/stagemonitor

@Test
public void testMdcStagemonitorNotStarted() throws Exception {
  final MeasurementSession measurementSession = new MeasurementSession("MDCSpanEventListenerTest", "testHost", null);
  Stagemonitor.reset(measurementSession);
  when(corePlugin.getMeasurementSession()).thenReturn(measurementSession);
  mdcSpanInterceptor.onStart(spanWrapper);
  assertEquals("testHost", MDC.get("host"));
  assertEquals("MDCSpanEventListenerTest", MDC.get("application"));
  assertNull(MDC.get("instance"));
  assertNull(MDC.get("spanId"));
  assertNull(MDC.get("traceId"));
  assertNull(MDC.get("parentId"));
  mdcSpanInterceptor.onFinish(spanWrapper, null, 0);
}

相关文章