本文整理汇总了Java中org.apache.logging.log4j.ThreadContext.getContext方法的典型用法代码示例。如果您正苦于以下问题:Java ThreadContext.getContext方法的具体用法?Java ThreadContext.getContext怎么用?Java ThreadContext.getContext使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.logging.log4j.ThreadContext
的用法示例。
在下文中一共展示了ThreadContext.getContext方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testGetContextReturnsMutableCopy
import org.apache.logging.log4j.ThreadContext; //导入方法依赖的package包/类
public static void testGetContextReturnsMutableCopy() {
ThreadContext.clearMap();
final Map<String, String> map1 = ThreadContext.getContext();
assertTrue(map1.isEmpty());
map1.put("K", "val"); // no error
assertEquals("val", map1.get("K"));
// adding to copy does not affect thread context map
assertTrue(ThreadContext.getContext().isEmpty());
ThreadContext.put("key", "val2");
final Map<String, String> map2 = ThreadContext.getContext();
assertEquals(1, map2.size());
assertEquals("val2", map2.get("key"));
map2.put("K", "val"); // no error
assertEquals("val", map2.get("K"));
// first copy is not affected
assertNotSame(map1, map2);
assertEquals(1, map1.size());
}
示例2: LogContextThreadFactory
import org.apache.logging.log4j.ThreadContext; //导入方法依赖的package包/类
public LogContextThreadFactory(final String threadNamePrefix) {
super(threadNamePrefix, ThreadContext.getContext());
}
示例3: log
import org.apache.logging.log4j.ThreadContext; //导入方法依赖的package包/类
@Override
public void log(final Marker marker, final String fqcn, final Level level, final Message msg,
final Throwable throwable) {
final StringBuilder sb = new StringBuilder();
// Append date-time if so configured
if (showDateTime) {
final Date now = new Date();
String dateText;
synchronized (dateFormatter) {
dateText = dateFormatter.format(now);
}
sb.append(dateText);
sb.append(SPACE);
}
sb.append(level.toString());
sb.append(SPACE);
if (logName != null && logName.length() > 0) {
sb.append(logName);
sb.append(SPACE);
}
sb.append(msg.getFormattedMessage());
if (showContextMap) {
final Map<String, String> mdc = ThreadContext.getContext();
if (mdc.size() > 0) {
sb.append(SPACE);
sb.append(mdc.toString());
sb.append(SPACE);
}
}
final Object[] params = msg.getParameters();
Throwable t;
if (throwable == null && params != null && params[params.length - 1] instanceof Throwable) {
t = (Throwable) params[params.length - 1];
} else {
t = throwable;
}
if (t != null) {
sb.append(SPACE);
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
t.printStackTrace(new PrintStream(baos));
sb.append(baos.toString());
}
stream.println(sb.toString());
}
示例4: getCopyOfContextMap
import org.apache.logging.log4j.ThreadContext; //导入方法依赖的package包/类
@Override
public Map<String, String> getCopyOfContextMap() {
return ThreadContext.getContext();
}