Java Code Examples for org.apache.logging.log4j.util.StringMap#putAll()

The following examples show how to use org.apache.logging.log4j.util.StringMap#putAll() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ThreadContextDataInjector.java    From logging-log4j2 with Apache License 2.0 6 votes vote down vote up
/**
 * 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.
    if (providers.size() == 1 && (props == null || props.isEmpty())) {
        // this will replace the LogEvent's context data with the returned instance
        return providers.get(0).supplyStringMap();
    }
    int count = props == null ? 0 : props.size();
    StringMap[] maps = new StringMap[providers.size()];
    for (int i = 0; i < providers.size(); ++i) {
        maps[i] = providers.get(i).supplyStringMap();
        count += maps[i].size();
    }
    // 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(count);
    copyProperties(props, result);
    for (StringMap map : maps) {
        result.putAll(map);
    }
    return result;
}
 
Example 2
Source File: ContextDataUtils.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
static StringMap injectContextData(@Nullable List<Property> properties, StringMap reusable) {
  if (properties == null || properties.isEmpty()) {
    return getContextAndTracingData();
  }
  // Context data has precedence over configuration properties.
  putProperties(properties, reusable);
  // TODO(sebright): The following line can be optimized. See
  //     https://github.com/census-instrumentation/opencensus-java/pull/1422/files#r216425494.
  reusable.putAll(getContextAndTracingData());
  return reusable;
}
 
Example 3
Source File: ThreadContextDataInjector.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
/**
 * 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);
    for (int i = 0; i < providers.size(); ++i) {
        reusable.putAll(providers.get(i).supplyStringMap());
    }
    return reusable;
}
 
Example 4
Source File: AsyncLogger.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
private static StringMap getContextData(final RingBufferLogEvent event) {
    StringMap contextData = (StringMap) event.getContextData();
    if (contextData.isFrozen()) {
        final StringMap temp = ContextDataFactory.createContextData();
        temp.putAll(contextData);
        return temp;
    }
    return contextData;
}
 
Example 5
Source File: Log4jLogEvent.java    From logging-log4j2 with Apache License 2.0 4 votes vote down vote up
private static StringMap memento(final ReadOnlyStringMap data) {
    final StringMap result = ContextDataFactory.createContextData();
    result.putAll(data);
    return result;
}
 
Example 6
Source File: ContextDataFactory.java    From logging-log4j2 with Apache License 2.0 4 votes vote down vote up
public static StringMap createContextData(final ReadOnlyStringMap readOnlyStringMap) {
    final StringMap contextData = createContextData(readOnlyStringMap.size());
    contextData.putAll(readOnlyStringMap);
    return contextData;
}