本文整理汇总了Java中org.apache.logging.log4j.ThreadContext.getImmutableContext方法的典型用法代码示例。如果您正苦于以下问题:Java ThreadContext.getImmutableContext方法的具体用法?Java ThreadContext.getImmutableContext怎么用?Java ThreadContext.getImmutableContext使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.logging.log4j.ThreadContext
的用法示例。
在下文中一共展示了ThreadContext.getImmutableContext方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: createMap
import org.apache.logging.log4j.ThreadContext; //导入方法依赖的package包/类
private static Map<String, String> createMap(final List<Property> properties) {
final Map<String, String> contextMap = ThreadContext.getImmutableContext();
if (contextMap == null && (properties == null || properties.size() == 0)) {
return null;
}
if (properties == null || properties.size() == 0) {
return contextMap; // contextMap is not null
}
final Map<String, String> map = new HashMap<String, String>(contextMap);
for (final Property prop : properties) {
if (!map.containsKey(prop.getName())) {
map.put(prop.getName(), prop.getValue());
}
}
return Collections.unmodifiableMap(map);
}
示例2: perfTest
import org.apache.logging.log4j.ThreadContext; //导入方法依赖的package包/类
public static void perfTest() throws Exception {
ThreadContext.clearMap();
final Timer complete = new Timer("ThreadContextTest");
complete.start();
ThreadContext.put("Var1", "value 1");
ThreadContext.put("Var2", "value 2");
ThreadContext.put("Var3", "value 3");
ThreadContext.put("Var4", "value 4");
ThreadContext.put("Var5", "value 5");
ThreadContext.put("Var6", "value 6");
ThreadContext.put("Var7", "value 7");
ThreadContext.put("Var8", "value 8");
ThreadContext.put("Var9", "value 9");
ThreadContext.put("Var10", "value 10");
final int loopCount = 1000000;
final Timer timer = new Timer("ThreadContextCopy", loopCount);
timer.start();
for (int i = 0; i < loopCount; ++i) {
final Map<String, String> map = ThreadContext.getImmutableContext();
assertNotNull(map);
}
timer.stop();
complete.stop();
System.out.println(timer.toString());
System.out.println(complete.toString());
}
示例3: evalThreadContextToMessage
import org.apache.logging.log4j.ThreadContext; //导入方法依赖的package包/类
private void evalThreadContextToMessage(Message msg) {
Map<String, String> ctx = ThreadContext.getImmutableContext();
if (ctx != null) {
msg.putHeaderEntry(TASK_CONTEXT, ctx);
}
List<String> taskStack = ThreadContext.getImmutableStack().asList();
if (taskStack != null && !taskStack.isEmpty()) {
msg.putHeaderEntry(TASK_STACK, taskStack);
}
}
示例4: createMap
import org.apache.logging.log4j.ThreadContext; //导入方法依赖的package包/类
static Map<String, String> createMap(final List<Property> properties) {
final Map<String, String> contextMap = ThreadContext.getImmutableContext();
if (properties == null || properties.isEmpty()) {
return contextMap; // may be ThreadContext.EMPTY_MAP but not null
}
final Map<String, String> map = new HashMap<>(contextMap);
for (final Property prop : properties) {
if (!map.containsKey(prop.getName())) {
map.put(prop.getName(), prop.getValue());
}
}
return Collections.unmodifiableMap(map);
}
示例5: injectContextData
import org.apache.logging.log4j.ThreadContext; //导入方法依赖的package包/类
/**
* Puts key-value pairs from both the specified list of properties as well as the thread context into the
* specified reusable StringMap.
*
* @param props list of configuration properties, may be {@code null}
* @param ignore a {@code StringMap} instance from the log event
* @return a {@code StringMap} combining configuration properties with thread context data
*/
@Override
public StringMap injectContextData(final List<Property> props, final StringMap ignore) {
final Map<String, String> copy = ThreadContext.getImmutableContext();
// The DefaultThreadContextMap stores context data in a Map<String, String>.
// This is a copy-on-write data structure so we are sure ThreadContext changes will not affect our copy.
// If there are no configuration properties returning a thin wrapper around the copy
// is faster than copying the elements into the LogEvent's reusable StringMap.
if (props == null || props.isEmpty()) {
// this will replace the LogEvent's context data with the returned instance.
// NOTE: must mark as frozen or downstream components may attempt to modify (UnsupportedOperationEx)
return copy.isEmpty() ? ContextDataFactory.emptyFrozenContextData() : frozenStringMap(copy);
}
// If the list of Properties is non-empty we need to combine the properties and the ThreadContext
// data. Note that we cannot reuse the specified StringMap: some Loggers may have properties defined
// and others not, so the LogEvent's context data may have been replaced with an immutable copy from
// the ThreadContext - this will throw an UnsupportedOperationException if we try to modify it.
final StringMap result = new JdkMapAdapterStringMap(new HashMap<>(copy));
for (int i = 0; i < props.size(); i++) {
final Property prop = props.get(i);
if (!copy.containsKey(prop.getName())) {
result.putValue(prop.getName(), prop.getValue());
}
}
result.freeze();
return result;
}
示例6: rawContextData
import org.apache.logging.log4j.ThreadContext; //导入方法依赖的package包/类
@Override
public ReadOnlyStringMap rawContextData() {
final ReadOnlyThreadContextMap map = ThreadContext.getThreadContextMap();
if (map instanceof ReadOnlyStringMap) {
return (ReadOnlyStringMap) map;
}
// note: default ThreadContextMap is null
final Map<String, String> copy = ThreadContext.getImmutableContext();
return copy.isEmpty() ? ContextDataFactory.emptyFrozenContextData() : new JdkMapAdapterStringMap(copy);
}
示例7: logMessage
import org.apache.logging.log4j.ThreadContext; //导入方法依赖的package包/类
@Override
public void logMessage(final String fqcn, final Level mgsLevel, final Marker marker, 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(mgsLevel.toString());
sb.append(SPACE);
if (Strings.isNotEmpty(logName)) {
sb.append(logName);
sb.append(SPACE);
}
sb.append(msg.getFormattedMessage());
if (showContextMap) {
final Map<String, String> mdc = ThreadContext.getImmutableContext();
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.length > 0
&& params[params.length - 1] instanceof Throwable) {
t = (Throwable) params[params.length - 1];
} else {
t = throwable;
}
stream.println(sb.toString());
if (t != null) {
stream.print(SPACE);
t.printStackTrace(stream);
}
}
示例8: testGetImmutableContextReturnsImmutableMapIfNonEmpty
import org.apache.logging.log4j.ThreadContext; //导入方法依赖的package包/类
public static void testGetImmutableContextReturnsImmutableMapIfNonEmpty() {
ThreadContext.clearMap();
ThreadContext.put("key", "val");
final Map<String, String> immutable = ThreadContext.getImmutableContext();
immutable.put("otherkey", "otherval");
}
示例9: testGetImmutableContextReturnsImmutableMapIfEmpty
import org.apache.logging.log4j.ThreadContext; //导入方法依赖的package包/类
public static void testGetImmutableContextReturnsImmutableMapIfEmpty() {
ThreadContext.clearMap();
final Map<String, String> immutable = ThreadContext.getImmutableContext();
immutable.put("otherkey", "otherval");
}