类org.apache.logging.log4j.core.ContextDataInjector源码实例Demo

下面列出了怎么用org.apache.logging.log4j.core.ContextDataInjector的API类实例代码及写法,或者点击链接到github查看源代码。

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();
}
 
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()));
    }
}
 
/**
 * Returns a new {@code ContextDataInjector} instance based on the value of system property
 * {@code log4j2.ContextDataInjector}. If no value was specified this factory method returns one of the
 * {@code ContextDataInjector} classes defined in {@link ThreadContextDataInjector} which is most appropriate for
 * the ThreadContext implementation.
 * <p>
 * <b>Note:</b> It is no longer recommended that users provide a custom implementation of the ContextDataInjector.
 * Instead, provide a {@code ContextDataProvider}.
 * </p>
 * <p>
 * Users may use this system property to specify the fully qualified class name of a class that implements the
 * {@code ContextDataInjector} interface.
 * </p><p>
 * When providing a custom {@code ContextDataInjector}, be aware that this method may be invoked multiple times by
 * the various components in Log4j that need access to context data.
 * This includes the object(s) that populate log events, but also various lookups and filters that look at
 * context data to determine whether an event should be logged.
 * </p>
 *
 * @return a ContextDataInjector that populates the {@code ReadOnlyStringMap} of all {@code LogEvent} objects
 * @see LogEvent#getContextData()
 * @see ContextDataInjector
 */
public static ContextDataInjector createInjector() {
    final String className = PropertiesUtil.getProperties().getStringProperty("log4j2.ContextDataInjector");
    if (className == null) {
        return createDefaultInjector();
    }
    try {
        final Class<? extends ContextDataInjector> cls = Loader.loadClass(className).asSubclass(
                ContextDataInjector.class);
        return cls.newInstance();
    } catch (final Exception dynamicFailed) {
        final ContextDataInjector result = createDefaultInjector();
        StatusLogger.getLogger().warn(
                "Could not create ContextDataInjector for '{}', using default {}: {}",
                className, result.getClass().getName(), dynamicFailed);
        return result;
    }
}
 
 类所在包
 类方法
 同包方法