本文整理了Java中org.apache.logging.log4j.ThreadContext.getThreadContextMap()
方法的一些代码示例,展示了ThreadContext.getThreadContextMap()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。ThreadContext.getThreadContextMap()
方法的具体详情如下:
包路径:org.apache.logging.log4j.ThreadContext
类名称:ThreadContext
方法名:getThreadContextMap
[英]Returns a read-only view of the internal data structure used to store thread context key-value pairs, or null if the internal data structure does not implement the ReadOnlyThreadContextMap interface.
The DefaultThreadContextMap implementation does not implement ReadOnlyThreadContextMap, so by default this method returns null.
[中]返回用于存储线程上下文键值对的内部数据结构的只读视图,如果内部数据结构未实现ReadOnlyThreadContextMap接口,则返回null。
DefaultThreadContextMap实现不实现ReadOnlyThreadContextMap,因此默认情况下,此方法返回null。
代码示例来源:origin: org.apache.logging.log4j/log4j-core
private static String contextMap() {
final ReadOnlyThreadContextMap impl = ThreadContext.getThreadContextMap();
return impl == null ? ContextImpl.WEBAPP.implClassSimpleName() : impl.getClass().getSimpleName();
}
代码示例来源:origin: org.apache.logging.log4j/log4j-core
private void testContextDataInjector() {
ReadOnlyThreadContextMap readOnlythreadContextMap = getThreadContextMap();
assertThat("thread context map class name",
(readOnlythreadContextMap == null) ? null : readOnlythreadContextMap.getClass().getName(),
is(equalTo(readOnlythreadContextMapClassName)));
ContextDataInjector contextDataInjector = createInjector();
StringMap stringMap = contextDataInjector.injectContextData(null, new SortedArrayStringMap());
assertThat("thread context map", ThreadContext.getContext(), allOf(hasEntry("foo", "bar"), not(hasKey("baz"))));
assertThat("context map", stringMap.toMap(), allOf(hasEntry("foo", "bar"), not(hasKey("baz"))));
if (!stringMap.isFrozen()) {
stringMap.clear();
assertThat("thread context map", ThreadContext.getContext(), allOf(hasEntry("foo", "bar"), not(hasKey("baz"))));
assertThat("context map", stringMap.toMap().entrySet(), is(empty()));
}
ThreadContext.put("foo", "bum");
ThreadContext.put("baz", "bam");
assertThat("thread context map", ThreadContext.getContext(), allOf(hasEntry("foo", "bum"), hasEntry("baz", "bam")));
if (stringMap.isFrozen()) {
assertThat("context map", stringMap.toMap(), allOf(hasEntry("foo", "bar"), not(hasKey("baz"))));
} else {
assertThat("context map", stringMap.toMap().entrySet(), is(empty()));
}
}
代码示例来源:origin: ops4j/org.ops4j.pax.logging
@Override
public ReadOnlyStringMap rawContextData() {
return ThreadContext.getThreadContextMap().getReadOnlyContextData();
}
}
代码示例来源:origin: ops4j/org.ops4j.pax.logging
@Override
public ReadOnlyStringMap rawContextData() {
return ThreadContext.getThreadContextMap().getReadOnlyContextData();
}
}
代码示例来源:origin: ops4j/org.ops4j.pax.logging
@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);
}
}
代码示例来源:origin: ops4j/org.ops4j.pax.logging
private static ContextDataInjector createDefaultInjector() {
final ReadOnlyThreadContextMap threadContextMap = ThreadContext.getThreadContextMap();
// note: map may be null (if legacy custom ThreadContextMap was installed by user)
if (threadContextMap instanceof DefaultThreadContextMap || threadContextMap == null) {
return new ThreadContextDataInjector.ForDefaultThreadContextMap(); // for non StringMap-based context maps
}
if (threadContextMap instanceof CopyOnWrite) {
return new ThreadContextDataInjector.ForCopyOnWriteThreadContextMap();
}
return new ThreadContextDataInjector.ForGarbageFreeThreadContextMap();
}
}
代码示例来源:origin: ops4j/org.ops4j.pax.logging
/**
* 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 reusable a {@code StringMap} instance that may be reused to avoid creating temporary objects
* @return a {@code StringMap} combining configuration properties with thread context data
*/
@Override
public StringMap injectContextData(final List<Property> props, final StringMap reusable) {
// When the ThreadContext is garbage-free, we must copy its key-value pairs into the specified reusable
// StringMap. We cannot return the ThreadContext's internal data structure because it may be modified later
// and such modifications should not be reflected in the log event.
copyProperties(props, reusable);
final ReadOnlyStringMap immutableCopy = ThreadContext.getThreadContextMap().getReadOnlyContextData();
reusable.putAll(immutableCopy);
return reusable;
}
代码示例来源:origin: ops4j/org.ops4j.pax.logging
/**
* If there are no configuration properties, this injector will return the thread context's internal data
* structure. Otherwise the configuration properties are combined with the thread context key-value pairs 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) {
// If there are no configuration properties we want to just return the ThreadContext's StringMap:
// it is a copy-on-write data structure so we are sure ThreadContext changes will not affect our copy.
final StringMap immutableCopy = ThreadContext.getThreadContextMap().getReadOnlyContextData();
if (props == null || props.isEmpty()) {
return immutableCopy; // this will replace the LogEvent's context data with the returned instance
}
// However, 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 = ContextDataFactory.createContextData(props.size() + immutableCopy.size());
copyProperties(props, result);
result.putAll(immutableCopy);
return result;
}
代码示例来源:origin: census-instrumentation/opencensus-java
static StringMap getContextAndTracingData() {
SpanContext spanContext = getCurrentSpanContext();
ReadOnlyThreadContextMap context = ThreadContext.getThreadContextMap();
SortedArrayStringMap stringMap;
if (context == null) {
stringMap = new SortedArrayStringMap(ThreadContext.getImmutableContext());
} else {
StringMap contextData = context.getReadOnlyContextData();
stringMap = new SortedArrayStringMap(contextData.size() + 3);
stringMap.putAll(contextData);
}
// TODO(sebright): Move the calls to TraceId.toLowerBase16() and SpanId.toLowerBase16() out of
// the critical path by wrapping the trace and span IDs in objects that call toLowerBase16() in
// their toString() methods, after there is a fix for
// https://github.com/census-instrumentation/opencensus-java/issues/1436.
stringMap.putValue(
OpenCensusTraceContextDataInjector.TRACE_ID_CONTEXT_KEY,
spanContext.getTraceId().toLowerBase16());
stringMap.putValue(
OpenCensusTraceContextDataInjector.SPAN_ID_CONTEXT_KEY,
spanContext.getSpanId().toLowerBase16());
stringMap.putValue(
OpenCensusTraceContextDataInjector.TRACE_SAMPLED_CONTEXT_KEY,
spanContext.getTraceOptions().isSampled() ? "true" : "false");
return stringMap;
}
内容来源于网络,如有侵权,请联系作者删除!