本文整理了Java中org.slf4j.MDC
类的一些代码示例,展示了MDC
类的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。MDC
类的具体详情如下:
包路径:org.slf4j.MDC
类名称:MDC
[英]This class hides and serves as a substitute for the underlying logging system's MDC implementation.
If the underlying logging system offers MDC functionality, then SLF4J's MDC, i.e. this class, will delegate to the underlying system's MDC. Note that at this time, only two logging systems, namely log4j and logback, offer MDC functionality. For java.util.logging which does not support MDC, BasicMDCAdapter will be used. For other systems, i.e. slf4j-simple and slf4j-nop, NOPMDCAdapter will be used.
Thus, as a SLF4J user, you can take advantage of MDC in the presence of log4j, logback, or java.util.logging, but without forcing these systems as dependencies upon your users.
For more information on MDC please see the chapter on MDC in the logback manual.
Please note that all methods in this class are static.
[中]此类隐藏并充当底层日志记录系统的MDC实现的替代品。
如果底层日志系统提供MDC功能,那么SLF4J的MDC(即此类)将委托给底层系统的MDC。注意,目前只有两个日志系统,即log4j和logback,提供了MDC功能。对于java。util。不支持MDC的日志记录,将使用BasicMDCAdapter。对于其他系统,即slf4j simple和slf4j nop,将使用NOPMDCAdapter。
因此,作为SLF4J用户,您可以在log4j、logback或java存在的情况下利用MDC。util。登录,但不强制这些系统依赖于您的用户。
有关MDC的更多信息,请参阅logback手册中的{$0$}。
请注意,此类中的所有方法都是静态的。
代码示例来源:origin: ch.qos.logback/logback-classic
void insertIntoMDC(ServletRequest request) {
MDC.put(ClassicConstants.REQUEST_REMOTE_HOST_MDC_KEY, request.getRemoteHost());
if (request instanceof HttpServletRequest) {
HttpServletRequest httpServletRequest = (HttpServletRequest) request;
MDC.put(ClassicConstants.REQUEST_REQUEST_URI, httpServletRequest.getRequestURI());
StringBuffer requestURL = httpServletRequest.getRequestURL();
if (requestURL != null) {
MDC.put(ClassicConstants.REQUEST_REQUEST_URL, requestURL.toString());
}
MDC.put(ClassicConstants.REQUEST_METHOD, httpServletRequest.getMethod());
MDC.put(ClassicConstants.REQUEST_QUERY_STRING, httpServletRequest.getQueryString());
MDC.put(ClassicConstants.REQUEST_USER_AGENT_MDC_KEY, httpServletRequest.getHeader("User-Agent"));
MDC.put(ClassicConstants.REQUEST_X_FORWARDED_FOR, httpServletRequest.getHeader("X-Forwarded-For"));
}
}
代码示例来源:origin: spring-cloud/spring-cloud-sleuth
static void replace(String key, @Nullable String value) {
if (value != null) {
MDC.put(key, value);
}
else {
MDC.remove(key);
}
}
代码示例来源:origin: apache/hive
private void handleLogOnQueryCompletion(String queryIdString, String dagIdString) {
if (routeBasedLoggingEnabled) {
// Inform the routing purgePolicy.
// Send out a fake log message at the ERROR level with the MDC for this query setup. With an
// LLAP custom appender this message will not be logged.
MDC.put("dagId", dagIdString);
MDC.put("queryId", queryIdString);
try {
LOG.error(QUERY_COMPLETE_MARKER, "Ignore this. Log line to interact with logger." +
" Query complete: " + queryIdString + ", " +
dagIdString);
} finally {
MDC.clear();
}
}
}
代码示例来源: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: apache/incubator-gobblin
@Override
public void run() {
Map<String, String> originalContext = MDC.getCopyOfContextMap();
if (context != null) {
MDC.setContextMap(context);
}
try {
this.runnable.run();
} finally {
if (originalContext != null) {
MDC.setContextMap(originalContext);
} else {
MDC.clear();
}
}
}
}
代码示例来源:origin: ch.qos.logback/logback-classic
void clearMDC() {
MDC.remove(ClassicConstants.REQUEST_REMOTE_HOST_MDC_KEY);
MDC.remove(ClassicConstants.REQUEST_REQUEST_URI);
MDC.remove(ClassicConstants.REQUEST_QUERY_STRING);
// removing possibly inexistent item is OK
MDC.remove(ClassicConstants.REQUEST_REQUEST_URL);
MDC.remove(ClassicConstants.REQUEST_METHOD);
MDC.remove(ClassicConstants.REQUEST_USER_AGENT_MDC_KEY);
MDC.remove(ClassicConstants.REQUEST_X_FORWARDED_FOR);
}
代码示例来源:origin: apache/incubator-druid
@Override
public void logNativeQuery(RequestLogLine requestLogLine) throws IOException
final Map mdc = MDC.getCopyOfContextMap();
try {
final Query query = requestLogLine.getQuery();
MDC.put("queryId", query.getId());
MDC.put("sqlQueryId", StringUtils.nullToEmptyNonDruidDataString(query.getSqlQueryId()));
MDC.put("dataSource", findInnerDatasource(query).toString());
MDC.put("queryType", query.getType());
MDC.put("isNested", String.valueOf(!(query.getDataSource() instanceof TableDataSource)));
MDC.put("hasFilters", Boolean.toString(query.hasFilters()));
MDC.put("remoteAddr", requestLogLine.getRemoteAddr());
MDC.put("duration", query.getDuration().toString());
MDC.put("descending", Boolean.toString(query.isDescending()));
if (setContextMDC) {
final Iterable<Map.Entry<String, Object>> entries = query.getContext() == null
: query.getContext().entrySet();
for (Map.Entry<String, Object> entry : entries) {
MDC.put(entry.getKey(), entry.getValue() == null ? "NULL" : entry.getValue().toString());
if (setMDC) {
if (mdc != null) {
MDC.setContextMap(mdc);
} else {
MDC.clear();
代码示例来源:origin: rhwayfun/spring-boot-learning-examples
@RequestMapping("/mdc/{id}")
@ResponseBody
public String mdcController(@PathVariable String id, HttpServletRequest request) {
MDC.put("traceId", request.getPathInfo());
threadPool.execute(wrap(new Runnable() {
@Override
public void run() {
logger.info("This is an mdc context controller");
}
}, MDC.getCopyOfContextMap()));
return request.getPathInfo();
}
代码示例来源: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: 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: 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: Activiti/Activiti
public static void clear() {
MDC.clear();
}
}
代码示例来源:origin: rhwayfun/spring-boot-learning-examples
@Override
public void run() {
try {
MDC.setContextMap(map);
wrapped.run();
} finally {
MDC.clear();
}
}
}
代码示例来源:origin: apache/incubator-gobblin
public MDCPropagatingCallable(Callable<T> callable) {
this.callable = callable;
this.context = MDC.getCopyOfContextMap();
}
代码示例来源: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.apache.solr/solr-solrj
public void logDebugWithMdc(Logger logger, String msg) {
Map previousMdcContext = MDC.getCopyOfContextMap();
MDC.setContextMap(mdcContext);
try {
logger.debug(msg);
} finally{
MDC.setContextMap(previousMdcContext);
}
}
代码示例来源:origin: debezium/debezium
/**
* Restore this logging context.
*/
public void restore() {
MDC.setContextMap(context);
}
}
代码示例来源:origin: redisson/redisson
public void close() {
MDC.remove(this.key);
}
}
代码示例来源:origin: apache/incubator-gobblin
@Override
public T call() throws Exception {
T answer;
Map<String, String> originalContext = MDC.getCopyOfContextMap();
if (context != null) {
MDC.setContextMap(context);
}
try {
answer = this.callable.call();
} finally {
if (originalContext != null) {
MDC.setContextMap(originalContext);
} else {
MDC.clear();
}
}
return answer;
}
}
代码示例来源:origin: rhwayfun/spring-boot-learning-examples
@RequestMapping("/test/{id}")
@ResponseBody
public String test(@PathVariable String id, HttpServletRequest request) {
MDC.put("traceId", request.getRequestURI());
threadPool.execute(wrap(new Runnable() {
@Override
public void run() {
logger.info("线程池任务执行--------2");
}
}, MDC.getCopyOfContextMap()));
return request.getRequestURI();
}
内容来源于网络,如有侵权,请联系作者删除!