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

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

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

MDC.putCloseable介绍

[英]Put a diagnostic context value (the val parameter) as identified with the key parameter into the current thread's diagnostic context map. The key parameter cannot be null. The val parameter can be null only if the underlying implementation supports it.

This method delegates all work to the MDC of the underlying logging system.

This method return a Closeable object who can remove key when close is called.

Useful with Java 7 for example : try(MDC.MDCCloseable closeable = MDC.putCloseable(key, value)) { .... }
[中]将用key参数标识的诊断上下文值(val参数)放入当前线程的诊断上下文映射中。key参数不能为空。val参数只能在基础实现支持时为空。
此方法将所有工作委托给底层日志系统的MDC。
此方法返回一个Closeable对象,当调用close时,该对象可以删除key
适用于Java 7,例如:try(MDC.MDCCloseable closeable = MDC.putCloseable(key, value)) { .... }

代码示例

代码示例来源:origin: apache/incubator-gobblin

@Override
public TaskResult run() {
 log.info("Actual task {} started. [{} {}]", this.taskId, this.applicationName, this.instanceName);
 try (Closer closer = Closer.create()) {
  closer.register(MDC.putCloseable(ConfigurationKeys.JOB_NAME_KEY, this.jobName));
  closer.register(MDC.putCloseable(ConfigurationKeys.JOB_KEY_KEY, this.jobKey));
  this.task.run();
  log.info("Actual task {} completed.", this.taskId);
  return new TaskResult(TaskResult.Status.COMPLETED, "");
 } catch (InterruptedException ie) {
  Thread.currentThread().interrupt();
  log.error("Actual task {} interrupted.", this.taskId);
  return new TaskResult(TaskResult.Status.CANCELED, "");
 } catch (Throwable t) {
  log.error("Actual task {} failed due to {}", this.taskId, t.getMessage());
  return new TaskResult(TaskResult.Status.FAILED, Throwables.getStackTraceAsString(t));
 }
}

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

private void logValue(Server server, Query query, List<String> typeNames, Result result) throws IOException {
  Object value = result.getValue();
  String resultAsString = resultSerializer.serialize(server, query, result);
  if (resultAsString == null) {
    return;
  }
  Closer closer = Closer.create();
  try {
    closer.register(MDC.putCloseable("server", computeAlias(server)));
    closer.register(MDC.putCloseable("metric", KeyUtils.getKeyString(server, query, result, typeNames, null)));
    closer.register(MDC.putCloseable("value", value.toString()));
    if (result.getKeyAlias() != null) {
      closer.register(MDC.putCloseable("resultAlias", result.getKeyAlias()));
    }
    closer.register(MDC.putCloseable("attributeName", result.getAttributeName()));
    closer.register(MDC.putCloseable("key", KeyUtils.getValueKey(result)));
    closer.register(MDC.putCloseable("epoch", valueOf(result.getEpoch())));
    logger.info(resultAsString);
  } catch (Throwable t) {
    throw closer.rethrow(t);
  } finally {
    closer.close();
  }
}

代码示例来源:origin: DV8FromTheWorld/JDA

.map((entry) -> MDC.putCloseable(entry.getKey(), entry.getValue()))
.collect(Collectors.toSet());

代码示例来源:origin: spotify/styx

public static MDC.MDCCloseable safePutCloseable(String key, String value) {
  try {
   return MDC.putCloseable(key, value);
  } catch (Exception e) {
   log.error("Failed to put MDC", e);
   // Returning null here is ok as try-with-resources ignores null resources.
   return null;
  }
 }
}

代码示例来源:origin: apache/james-project

public Closeable build() {
  return new Closeables(
    buildContextMap()
      .entrySet()
      .stream()
      .map(entry -> MDC.putCloseable(entry.getKey(), entry.getValue()))
      .collect(Guavate.toImmutableList()));
}

代码示例来源:origin: org.apache.james/james-server-util

public Closeable build() {
  return new Closeables(
    buildContextMap()
      .entrySet()
      .stream()
      .map(entry -> MDC.putCloseable(entry.getKey(), entry.getValue()))
      .collect(Guavate.toImmutableList()));
}

代码示例来源:origin: org.apache.james/james-server-util-java8

public Closeable build() {
  return new Closeables(
    buildContextMap()
      .entrySet()
      .stream()
      .map(entry -> MDC.putCloseable(entry.getKey(), entry.getValue()))
      .collect(Guavate.toImmutableList()));
}

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

@Override
public void createGuideline(Project aProject, InputStream aIS, String aFileName)
  throws IOException
{
  String guidelinePath = dir.getAbsolutePath() + "/" + PROJECT_FOLDER + "/" + aProject.getId()
      + "/" + GUIDELINES_FOLDER + "/";
  FileUtils.forceMkdir(new File(guidelinePath));
  copyLarge(aIS, new FileOutputStream(new File(guidelinePath + aFileName)));
  try (MDC.MDCCloseable closable = MDC.putCloseable(Logging.KEY_PROJECT_ID,
      String.valueOf(aProject.getId()))) {
    log.info("Created guidelines file [{}] in project [{}]({})",
        aFileName, aProject.getName(), aProject.getId());
  }
}

代码示例来源:origin: de.tudarmstadt.ukp.clarin.webanno/webanno-project

@Override
public void removeGuideline(Project aProject, String aFileName)
  throws IOException
{
  FileUtils.forceDelete(new File(dir.getAbsolutePath() + "/" + PROJECT_FOLDER + "/"
      + aProject.getId() + "/" + GUIDELINES_FOLDER + "/" + aFileName));
  
  try (MDC.MDCCloseable closable = MDC.putCloseable(Logging.KEY_PROJECT_ID,
      String.valueOf(aProject.getId()))) {
    log.info("Removed guidelines file [{}] from project [{}]({})", aFileName,
        aProject.getName(), aProject.getId());
  }
}

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

@Override
public void removeGuideline(Project aProject, String aFileName)
  throws IOException
{
  FileUtils.forceDelete(new File(dir.getAbsolutePath() + "/" + PROJECT_FOLDER + "/"
      + aProject.getId() + "/" + GUIDELINES_FOLDER + "/" + aFileName));
  
  try (MDC.MDCCloseable closable = MDC.putCloseable(Logging.KEY_PROJECT_ID,
      String.valueOf(aProject.getId()))) {
    log.info("Removed guidelines file [{}] from project [{}]({})", aFileName,
        aProject.getName(), aProject.getId());
  }
}

代码示例来源:origin: Johnnei/JavaTorrent

public void handlePeer(SelectionKey key, Peer peer) {
  try (MDC.MDCCloseable ignored = MDC.putCloseable("context", peer.getIdAsString())) {
    BitTorrentSocket socket = peer.getBitTorrentSocket();
    if (key.isReadable()) {
      onDataAvailable(peer, socket);
    }
    if (key.isWritable()) {
      onDataRequested(peer, socket);
    }
  }
}

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

@Override
@Transactional
public void removeConstraintSet(ConstraintSet aSet)
{
  entityManager.remove(entityManager.merge(aSet));
  
  try (MDC.MDCCloseable closable = MDC.putCloseable(Logging.KEY_PROJECT_ID,
      String.valueOf(aSet.getProject().getId()))) {
    log.info("Removed constraints set [{}] in project [{}]({})",
        aSet.getName(), aSet.getProject().getName(), aSet.getProject().getId());
  }
}

代码示例来源:origin: de.tudarmstadt.ukp.clarin.webanno/webanno-constraints

@Override
@Transactional
public void removeConstraintSet(ConstraintSet aSet)
{
  entityManager.remove(entityManager.merge(aSet));
  
  try (MDC.MDCCloseable closable = MDC.putCloseable(Logging.KEY_PROJECT_ID,
      String.valueOf(aSet.getProject().getId()))) {
    log.info("Removed constraints set [{}] in project [{}]({})",
        aSet.getName(), aSet.getProject().getName(), aSet.getProject().getId());
  }
}

代码示例来源:origin: de.tudarmstadt.ukp.clarin.webanno/webanno-constraints

@Override
@Transactional
public void createConstraintSet(ConstraintSet aSet)
{
  entityManager.persist(aSet);
  
  try (MDC.MDCCloseable closable = MDC.putCloseable(Logging.KEY_PROJECT_ID,
      String.valueOf(aSet.getProject().getId()))) {
    log.info("Created constraints set [{}] in project [{}]({})",
        aSet.getName(), aSet.getProject().getName(), aSet.getProject().getId());
  }
}

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

@Override
@Transactional
public void createConstraintSet(ConstraintSet aSet)
{
  entityManager.persist(aSet);
  
  try (MDC.MDCCloseable closable = MDC.putCloseable(Logging.KEY_PROJECT_ID,
      String.valueOf(aSet.getProject().getId()))) {
    log.info("Created constraints set [{}] in project [{}]({})",
        aSet.getName(), aSet.getProject().getName(), aSet.getProject().getId());
  }
}

代码示例来源:origin: de.tudarmstadt.ukp.clarin.webanno/webanno-project

@Override
@Transactional
public void createProjectPermission(ProjectPermission aPermission)
{
  entityManager.persist(aPermission);
  
  try (MDC.MDCCloseable closable = MDC.putCloseable(Logging.KEY_PROJECT_ID,
      String.valueOf(aPermission.getProject().getId()))) {
    log.info("Created permission [{}] for user [{}] on project [{}]({})",
        aPermission.getLevel(), aPermission.getUser(),
        aPermission.getProject().getName(), aPermission.getProject().getId());
  }
}

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

@Override
@Transactional
public void createProjectPermission(ProjectPermission aPermission)
{
  entityManager.persist(aPermission);
  
  try (MDC.MDCCloseable closable = MDC.putCloseable(Logging.KEY_PROJECT_ID,
      String.valueOf(aPermission.getProject().getId()))) {
    log.info("Created permission [{}] for user [{}] on project [{}]({})",
        aPermission.getLevel(), aPermission.getUser(),
        aPermission.getProject().getName(), aPermission.getProject().getId());
  }
}

代码示例来源:origin: de.tudarmstadt.ukp.clarin.webanno/webanno-project

@Override
@Transactional
public void removeProjectPermission(ProjectPermission aPermission)
{
  entityManager.remove(aPermission);
  
  try (MDC.MDCCloseable closable = MDC.putCloseable(Logging.KEY_PROJECT_ID,
      String.valueOf(aPermission.getProject().getId()))) {
    log.info("Removed permission [{}] for user [{}] on project [{}]({})",
        aPermission.getLevel(), aPermission.getUser(),
        aPermission.getProject().getName(), aPermission.getProject().getId());
  }
}

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

@Override
@Transactional
public void removeProjectPermission(ProjectPermission aPermission)
{
  entityManager.remove(aPermission);
  
  try (MDC.MDCCloseable closable = MDC.putCloseable(Logging.KEY_PROJECT_ID,
      String.valueOf(aPermission.getProject().getId()))) {
    log.info("Removed permission [{}] for user [{}] on project [{}]({})",
        aPermission.getLevel(), aPermission.getUser(),
        aPermission.getProject().getName(), aPermission.getProject().getId());
  }
}

代码示例来源:origin: Netflix/spectator

@Test
 public void testValueInMdc() {
  String mdcName = "key";
  String expectedValue = "value";
  Tag expectedTag = new BasicTag(mdcName, expectedValue);
  TagFactory factory = new MdcTagFactory("key");

  try (MDC.MDCCloseable closeable = MDC.putCloseable(mdcName, expectedValue)) {
   Tag actualTag = factory.createTag();

   Assertions.assertEquals(expectedTag, actualTag);
  }

  // Make sure that the factory returns null after the MDC has been cleaned up.
  Assertions.assertNull(factory.createTag());
 }
}

相关文章