Search in sources :

Example 1 with PropertiesUtil

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

the class ThreadContext method init.

/**
 * <em>Consider private, used for testing.</em>
 */
static void init() {
    ThreadContextMapFactory.init();
    contextMap = null;
    final PropertiesUtil managerProps = PropertiesUtil.getProperties();
    final boolean disableAll = managerProps.getBooleanProperty(DISABLE_ALL);
    useStack = !(managerProps.getBooleanProperty(DISABLE_STACK) || disableAll);
    final boolean useMap = !(managerProps.getBooleanProperty(DISABLE_MAP) || disableAll);
    contextStack = new DefaultThreadContextStack(useStack);
    if (!useMap) {
        contextMap = new NoOpThreadContextMap();
    } else {
        contextMap = ThreadContextMapFactory.createThreadContextMap();
    }
    if (contextMap instanceof ReadOnlyThreadContextMap) {
        readOnlyContextMap = (ReadOnlyThreadContextMap) contextMap;
    } else {
        readOnlyContextMap = null;
    }
}
Also used : DefaultThreadContextStack(org.apache.logging.log4j.spi.DefaultThreadContextStack) PropertiesUtil(org.apache.logging.log4j.util.PropertiesUtil) NoOpThreadContextMap(org.apache.logging.log4j.spi.NoOpThreadContextMap) ReadOnlyThreadContextMap(org.apache.logging.log4j.spi.ReadOnlyThreadContextMap)

Example 2 with PropertiesUtil

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

the class ConfigurationFactory method getInstance.

/**
 * Returns the ConfigurationFactory.
 * @return the ConfigurationFactory.
 */
public static ConfigurationFactory getInstance() {
    // noinspection DoubleCheckedLocking
    if (factories == null) {
        LOCK.lock();
        try {
            if (factories == null) {
                final List<ConfigurationFactory> list = new ArrayList<>();
                final PropertiesUtil props = PropertiesUtil.getProperties();
                final String factoryClass = props.getStringProperty(CONFIGURATION_FACTORY_PROPERTY);
                if (factoryClass != null) {
                    addFactory(list, factoryClass);
                }
                final PluginManager manager = new PluginManager(CATEGORY);
                manager.collectPlugins();
                final Map<String, PluginType<?>> plugins = manager.getPlugins();
                final List<Class<? extends ConfigurationFactory>> ordered = new ArrayList<>(plugins.size());
                for (final PluginType<?> type : plugins.values()) {
                    try {
                        ordered.add(type.getPluginClass().asSubclass(ConfigurationFactory.class));
                    } catch (final Exception ex) {
                        LOGGER.warn("Unable to add class {}", type.getPluginClass(), ex);
                    }
                }
                Collections.sort(ordered, OrderComparator.getInstance());
                for (final Class<? extends ConfigurationFactory> clazz : ordered) {
                    addFactory(list, clazz);
                }
                // see above comments about double-checked locking
                // noinspection NonThreadSafeLazyInitialization
                factories = Collections.unmodifiableList(list);
                authorizationProvider = authorizationProvider(props);
            }
        } finally {
            LOCK.unlock();
        }
    }
    LOGGER.debug("Using configurationFactory {}", configFactory);
    return configFactory;
}
Also used : ArrayList(java.util.ArrayList) PropertiesUtil(org.apache.logging.log4j.util.PropertiesUtil) PluginType(org.apache.logging.log4j.plugins.util.PluginType) URISyntaxException(java.net.URISyntaxException) FileNotFoundException(java.io.FileNotFoundException) PluginManager(org.apache.logging.log4j.plugins.util.PluginManager)

Example 3 with PropertiesUtil

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

the class AsyncQueueFullPolicyFactory method createDiscardingAsyncQueueFullPolicy.

private static AsyncQueueFullPolicy createDiscardingAsyncQueueFullPolicy() {
    final PropertiesUtil util = PropertiesUtil.getProperties();
    final String level = util.getStringProperty(PROPERTY_NAME_DISCARDING_THRESHOLD_LEVEL, Level.INFO.name());
    final Level thresholdLevel = Level.toLevel(level, Level.INFO);
    LOGGER.debug("Creating custom DiscardingAsyncQueueFullPolicy(discardThreshold:{})", thresholdLevel);
    return new DiscardingAsyncQueueFullPolicy(thresholdLevel);
}
Also used : PropertiesUtil(org.apache.logging.log4j.util.PropertiesUtil) Level(org.apache.logging.log4j.Level)

Example 4 with PropertiesUtil

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

the class ConsoleAppender method getOutputStream.

private static OutputStream getOutputStream(final boolean follow, final boolean direct, final Target target) {
    final String enc = Charset.defaultCharset().name();
    OutputStream outputStream;
    try {
        // @formatter:off
        outputStream = target == Target.SYSTEM_OUT ? direct ? new FileOutputStream(FileDescriptor.out) : (follow ? new PrintStream(new SystemOutStream(), true, enc) : System.out) : direct ? new FileOutputStream(FileDescriptor.err) : (follow ? new PrintStream(new SystemErrStream(), true, enc) : System.err);
        // @formatter:on
        outputStream = new CloseShieldOutputStream(outputStream);
    } catch (final UnsupportedEncodingException ex) {
        // should never happen
        throw new IllegalStateException("Unsupported default encoding " + enc, ex);
    }
    final PropertiesUtil propsUtil = PropertiesUtil.getProperties();
    if (!propsUtil.isOsWindows() || propsUtil.getBooleanProperty("log4j.skipJansi", true) || direct) {
        return outputStream;
    }
    try {
        // We type the parameter as a wildcard to avoid a hard reference to Jansi.
        final Class<?> clazz = Loader.loadClass(JANSI_CLASS);
        final Constructor<?> constructor = clazz.getConstructor(OutputStream.class);
        return new CloseShieldOutputStream((OutputStream) constructor.newInstance(outputStream));
    } catch (final ClassNotFoundException cnfe) {
        LOGGER.debug("Jansi is not installed, cannot find {}", JANSI_CLASS);
    } catch (final NoSuchMethodException nsme) {
        LOGGER.warn("{} is missing the proper constructor", JANSI_CLASS);
    } catch (final Exception ex) {
        LOGGER.warn("Unable to instantiate {} due to {}", JANSI_CLASS, clean(Throwables.getRootCause(ex).toString()).trim());
    }
    return outputStream;
}
Also used : PrintStream(java.io.PrintStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) CloseShieldOutputStream(org.apache.logging.log4j.core.util.CloseShieldOutputStream) UnsupportedEncodingException(java.io.UnsupportedEncodingException) PropertiesUtil(org.apache.logging.log4j.util.PropertiesUtil) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) FileOutputStream(java.io.FileOutputStream) CloseShieldOutputStream(org.apache.logging.log4j.core.util.CloseShieldOutputStream)

Example 5 with PropertiesUtil

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

the class Log4j2CloudConfigLoggingSystem method getStandardConfigLocations.

@Override
protected String[] getStandardConfigLocations() {
    String[] locations = super.getStandardConfigLocations();
    PropertiesUtil props = new PropertiesUtil(new Properties());
    String location = props.getStringProperty(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY);
    if (location != null) {
        List<String> list = new ArrayList<>(Arrays.asList(super.getStandardConfigLocations()));
        list.add(location);
        locations = list.toArray(new String[0]);
    }
    return locations;
}
Also used : ArrayList(java.util.ArrayList) PropertiesUtil(org.apache.logging.log4j.util.PropertiesUtil) Properties(java.util.Properties)

Aggregations

PropertiesUtil (org.apache.logging.log4j.util.PropertiesUtil)12 ArrayList (java.util.ArrayList)2 ReadOnlyStringMap (org.apache.logging.log4j.util.ReadOnlyStringMap)2 FileNotFoundException (java.io.FileNotFoundException)1 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 HttpURLConnection (java.net.HttpURLConnection)1 ProtocolException (java.net.ProtocolException)1 URISyntaxException (java.net.URISyntaxException)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Properties (java.util.Properties)1 HttpsURLConnection (javax.net.ssl.HttpsURLConnection)1 Level (org.apache.logging.log4j.Level)1 AuthorizationProvider (org.apache.logging.log4j.core.util.AuthorizationProvider)1 BasicAuthorizationProvider (org.apache.logging.log4j.core.util.BasicAuthorizationProvider)1 CloseShieldOutputStream (org.apache.logging.log4j.core.util.CloseShieldOutputStream)1