Search in sources :

Example 6 with PropertiesUtil

use of org.apache.logging.log4j.util.PropertiesUtil in project logging-log4j2 by apache.

the class ThreadContextMapFactory method createThreadContextMap.

public static ThreadContextMap createThreadContextMap() {
    final PropertiesUtil managerProps = PropertiesUtil.getProperties();
    final String threadContextMapName = managerProps.getStringProperty(THREAD_CONTEXT_KEY);
    final ClassLoader cl = ProviderUtil.findClassLoader();
    ThreadContextMap result = null;
    if (threadContextMapName != null) {
        try {
            final Class<?> clazz = cl.loadClass(threadContextMapName);
            if (ThreadContextMap.class.isAssignableFrom(clazz)) {
                result = (ThreadContextMap) clazz.newInstance();
            }
        } catch (final ClassNotFoundException cnfe) {
            LOGGER.error("Unable to locate configured ThreadContextMap {}", threadContextMapName);
        } catch (final Exception ex) {
            LOGGER.error("Unable to create configured ThreadContextMap {}", threadContextMapName, ex);
        }
    }
    if (result == null && ProviderUtil.hasProviders() && LogManager.getFactory() != null) {
        //LOG4J2-1658
        final String factoryClassName = LogManager.getFactory().getClass().getName();
        for (final Provider provider : ProviderUtil.getProviders()) {
            if (factoryClassName.equals(provider.getClassName())) {
                final Class<? extends ThreadContextMap> clazz = provider.loadThreadContextMap();
                if (clazz != null) {
                    try {
                        result = clazz.newInstance();
                        break;
                    } catch (final Exception e) {
                        LOGGER.error("Unable to locate or load configured ThreadContextMap {}", provider.getThreadContextMap(), e);
                        result = createDefaultThreadContextMap();
                    }
                }
            }
        }
    }
    if (result == null) {
        result = createDefaultThreadContextMap();
    }
    return result;
}
Also used : PropertiesUtil(org.apache.logging.log4j.util.PropertiesUtil)

Example 7 with PropertiesUtil

use of org.apache.logging.log4j.util.PropertiesUtil in project logging-log4j2 by apache.

the class CopyOnWriteSortedArrayThreadContextMap method createThreadLocalMap.

// LOG4J2-479: by default, use a plain ThreadLocal, only use InheritableThreadLocal if configured.
// (This method is package protected for JUnit tests.)
private ThreadLocal<StringMap> createThreadLocalMap() {
    final PropertiesUtil managerProps = PropertiesUtil.getProperties();
    final boolean inheritable = managerProps.getBooleanProperty(INHERITABLE_MAP);
    if (inheritable) {
        return new InheritableThreadLocal<StringMap>() {

            @Override
            protected StringMap childValue(final StringMap parentValue) {
                return parentValue != null ? createStringMap(parentValue) : null;
            }
        };
    }
    // if not inheritable, return plain ThreadLocal with null as initial value
    return new ThreadLocal<>();
}
Also used : SortedArrayStringMap(org.apache.logging.log4j.util.SortedArrayStringMap) StringMap(org.apache.logging.log4j.util.StringMap) ReadOnlyStringMap(org.apache.logging.log4j.util.ReadOnlyStringMap) PropertiesUtil(org.apache.logging.log4j.util.PropertiesUtil)

Aggregations

PropertiesUtil (org.apache.logging.log4j.util.PropertiesUtil)7 ReadOnlyStringMap (org.apache.logging.log4j.util.ReadOnlyStringMap)3 SortedArrayStringMap (org.apache.logging.log4j.util.SortedArrayStringMap)2 StringMap (org.apache.logging.log4j.util.StringMap)2 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 OutputStream (java.io.OutputStream)1 PrintStream (java.io.PrintStream)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Level (org.apache.logging.log4j.Level)1 CloseShieldOutputStream (org.apache.logging.log4j.core.util.CloseShieldOutputStream)1 DefaultThreadContextStack (org.apache.logging.log4j.spi.DefaultThreadContextStack)1 NoOpThreadContextMap (org.apache.logging.log4j.spi.NoOpThreadContextMap)1 ReadOnlyThreadContextMap (org.apache.logging.log4j.spi.ReadOnlyThreadContextMap)1