Class PropertiesUtil

  • All Implemented Interfaces:
    PropertyEnvironment

    @InternalApi
    public class PropertiesUtil
    extends Object
    implements PropertyEnvironment
    Consider this class private.

    Provides utility methods for managing Properties instances as well as access to the global configuration system. Properties by default are loaded from the system properties, system environment, and a classpath resource file named . Additional properties can be loaded by implementing a custom PropertySource service and specifying it via a ServiceLoader file called META-INF/services/org.apache.logging.log4j.util.PropertySource with a list of fully qualified class names implementing that interface.

    See Also:
    PropertySource
    • Constructor Detail

      • PropertiesUtil

        public PropertiesUtil​(Properties props)
        Constructs a PropertiesUtil using a given Properties object as its source of defined properties.
        Parameters:
        props - the Properties to use by default
      • PropertiesUtil

        public PropertiesUtil​(Properties props,
                              boolean includeInvalid)
        Constructs a PropertiesUtil using a given Properties object as its source of defined properties.
        Parameters:
        props - the Properties to use by default
        includeInvalid - includes invalid properties.
      • PropertiesUtil

        public PropertiesUtil​(String propertiesFileName)
        Constructs a PropertiesUtil for a given properties file name on the classpath. The properties specified in this file are used by default. If a property is not defined in this file, then the equivalent system property is used.
        Parameters:
        propertiesFileName - the location of properties file to load
      • PropertiesUtil

        public PropertiesUtil​(String contextName,
                              String propertiesFileName)
        Constructs a PropertiesUtil for a given properties file name on the classpath. The properties specified in this file are used by default. If a property is not defined in this file, then the equivalent system property is used.
        Parameters:
        propertiesFileName - the location of properties file to load
      • PropertiesUtil

        public PropertiesUtil​(PropertySource source)
        Constructs a PropertiesUtil for a given property source as source of additional properties.
        Parameters:
        source - a property source
    • Method Detail

      • hasThreadProperties

        public static boolean hasThreadProperties()
      • setThreadProperties

        public static void setThreadProperties​(PropertiesUtil properties)
      • clearThreadProperties

        public static void clearThreadProperties()
      • getProperties

        public static PropertiesUtil getProperties()
        Returns the PropertiesUtil used by Log4j.
        Returns:
        the main Log4j PropertiesUtil instance.
      • getCharsetsResourceBundle

        public static ResourceBundle getCharsetsResourceBundle()
      • hasProperty

        public boolean hasProperty​(String name)
        Returns true if the specified property is defined, regardless of its value (it may not have a value).
        Specified by:
        hasProperty in interface PropertyEnvironment
        Parameters:
        name - the name of the property to verify
        Returns:
        true if the specified property is defined, regardless of its value
      • getDoubleProperty

        public double getDoubleProperty​(String name,
                                        double defaultValue)
        Gets the named property as a double.
        Parameters:
        name - the name of the property to look up
        defaultValue - the default value to use if the property is undefined
        Returns:
        the parsed double value of the property or defaultValue if it was undefined or could not be parsed.
      • getStringProperty

        public String getStringProperty​(String name)
        Gets the named property as a String.
        Specified by:
        getStringProperty in interface PropertyEnvironment
        Parameters:
        name - the name of the property to look up
        Returns:
        the String value of the property or null if undefined.
      • getSystemProperties

        public static Properties getSystemProperties()
        Return the system properties or an empty Properties object if an error occurs.
        Returns:
        The system properties.
      • reload

        public void reload()
        Reloads all properties. This is primarily useful for unit tests.
        Since:
        2.10.0
      • getContextProperties

        public static PropertiesUtil getContextProperties​(String contextName)
        Get the properties for a LoggerContext just by the name. This ALWAYS creates a new PropertiesUtil. Use locateProperties to obtain the current properties.
        Parameters:
        contextName - The context name.
        Returns:
        The PropertiesUtil that was created.
      • getContextProperties

        public static PropertiesUtil getContextProperties​(String contextName,
                                                          PropertiesUtil propertiesUtil)
        Get the properties for a LoggerContext just by the name. This method is used for testing.
        Parameters:
        contextName - The context name.
        Returns:
        The PropertiesUtil that was created.
      • getContextProperties

        public static PropertiesUtil getContextProperties​(ClassLoader classLoader,
                                                          String contextName)
        Get the properties associated with a LoggerContext that is associated with a ClassLoader. This ALWAYS creates a new PropertiesUtil. Use locateProperties to obtain the current properties.
        Parameters:
        classLoader - The ClassLoader.
        contextName - The context name.
        Returns:
        The PropertiesUtil created.
      • extractSubset

        public static Properties extractSubset​(Properties properties,
                                               String prefix)
        Extracts properties that start with or are equals to the specific prefix and returns them in a new Properties object with the prefix removed.
        Parameters:
        properties - The Properties to evaluate.
        prefix - The prefix to extract.
        Returns:
        The subset of properties.
      • partitionOnCommonPrefixes

        public static Map<String,​Properties> partitionOnCommonPrefixes​(Properties properties)
        Partitions a properties map based on common key prefixes up to the first period.
        Parameters:
        properties - properties to partition
        Returns:
        the partitioned properties where each key is the common prefix (minus the period) and the values are new property maps without the prefix and period in the key
        Since:
        2.6
      • partitionOnCommonPrefixes

        public static Map<String,​Properties> partitionOnCommonPrefixes​(Properties properties,
                                                                             boolean includeBaseKey)
        Partitions a properties map based on common key prefixes up to the first period.
        Parameters:
        properties - properties to partition
        includeBaseKey - when true if a key exists with no '.' the key will be included.
        Returns:
        the partitioned properties where each key is the common prefix (minus the period) and the values are new property maps without the prefix and period in the key
        Since:
        2.17.2