org.apache.logging.log4j.core.ContextDataInjector Java Examples
The following examples show how to use
org.apache.logging.log4j.core.ContextDataInjector.
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: ContextDataInjectorFactory.java From logging-log4j2 with Apache License 2.0 | 5 votes |
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(); }
Example #2
Source File: ThreadContextDataInjectorTest.java From logging-log4j2 with Apache License 2.0 | 5 votes |
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())); } }
Example #3
Source File: ContextDataInjectorFactory.java From logging-log4j2 with Apache License 2.0 | 4 votes |
/** * 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; } }