Search in sources :

Example 11 with PropertiesUtil

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

the class GarbageFreeSortedArrayThreadContextMap 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)

Example 12 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 13 with PropertiesUtil

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

the class ThreadContextMapFactory method initPrivate.

/**
 * Initializes static variables based on system properties. Normally called when this class is initialized by the VM
 * and when Log4j is reconfigured.
 */
private static void initPrivate() {
    final PropertiesUtil properties = PropertiesUtil.getProperties();
    ThreadContextMapName = properties.getStringProperty(THREAD_CONTEXT_KEY);
    GcFreeThreadContextKey = properties.getBooleanProperty(GC_FREE_THREAD_CONTEXT_KEY);
}
Also used : PropertiesUtil(org.apache.logging.log4j.util.PropertiesUtil)

Example 14 with PropertiesUtil

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

the class CopyOnWriteSortedArrayThreadContextMap method init.

/**
 * Initializes static variables based on system properties. Normally called when this class is initialized by the VM
 * and when Log4j is reconfigured.
 */
static void init() {
    final PropertiesUtil properties = PropertiesUtil.getProperties();
    initialCapacity = properties.getIntegerProperty(PROPERTY_NAME_INITIAL_CAPACITY, DEFAULT_INITIAL_CAPACITY);
    inheritableMap = properties.getBooleanProperty(INHERITABLE_MAP);
}
Also used : PropertiesUtil(org.apache.logging.log4j.util.PropertiesUtil)

Example 15 with PropertiesUtil

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

the class GarbageFreeSortedArrayThreadContextMap method init.

/**
 * Initializes static variables based on system properties. Normally called when this class is initialized by the VM
 * and when Log4j is reconfigured.
 */
static void init() {
    final PropertiesUtil properties = PropertiesUtil.getProperties();
    initialCapacity = properties.getIntegerProperty(PROPERTY_NAME_INITIAL_CAPACITY, DEFAULT_INITIAL_CAPACITY);
    inheritableMap = properties.getBooleanProperty(INHERITABLE_MAP);
}
Also used : PropertiesUtil(org.apache.logging.log4j.util.PropertiesUtil)

Aggregations

PropertiesUtil (org.apache.logging.log4j.util.PropertiesUtil)22 ArrayList (java.util.ArrayList)4 UnsupportedEncodingException (java.io.UnsupportedEncodingException)3 Properties (java.util.Properties)3 Level (org.apache.logging.log4j.Level)3 CloseShieldOutputStream (org.apache.logging.log4j.core.util.CloseShieldOutputStream)3 FileNotFoundException (java.io.FileNotFoundException)2 FileOutputStream (java.io.FileOutputStream)2 IOException (java.io.IOException)2 OutputStream (java.io.OutputStream)2 PrintStream (java.io.PrintStream)2 URISyntaxException (java.net.URISyntaxException)2 DefaultThreadContextStack (org.apache.logging.log4j.spi.DefaultThreadContextStack)2 NoOpThreadContextMap (org.apache.logging.log4j.spi.NoOpThreadContextMap)2 ReadOnlyThreadContextMap (org.apache.logging.log4j.spi.ReadOnlyThreadContextMap)2 ReadOnlyStringMap (org.apache.logging.log4j.util.ReadOnlyStringMap)2 HttpURLConnection (java.net.HttpURLConnection)1 ProtocolException (java.net.ProtocolException)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1