Package org.apache.logging.log4j.util
Class PropertiesUtil
java.lang.Object
org.apache.logging.log4j.util.PropertiesUtil
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 "log4j2.component.properties". 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:
-
Constructor Summary
ConstructorDescriptionPropertiesUtil
(String propertiesFileName) Constructs a PropertiesUtil for a given properties file name on the classpath.PropertiesUtil
(Properties props) Constructs a PropertiesUtil using a given Properties object as its source of defined properties. -
Method Summary
Modifier and TypeMethodDescriptionvoid
addPropertySource
(PropertySource propertySource) Allows aPropertySource
to be added afterPropertiesUtil
has been created.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.boolean
getBooleanProperty
(String name) Gets the named property as a boolean value.getBooleanProperty
(String[] prefixes, String key, Supplier<Boolean> supplier) Retrieves a property that may be prefixed by more than one string.boolean
getBooleanProperty
(String name, boolean defaultValue) Gets the named property as a boolean value.boolean
getBooleanProperty
(String name, boolean defaultValueIfAbsent, boolean defaultValueIfPresent) Gets the named property as a boolean value.getCharsetProperty
(String name) Gets the named property as a Charset value.getCharsetProperty
(String name, Charset defaultValue) Gets the named property as a Charset value.double
getDoubleProperty
(String name, double defaultValue) Gets the named property as a double.getDurationProperty
(String[] prefixes, String key, Supplier<Duration> supplier) Retrieves a property that may be prefixed by more than one string.getDurationProperty
(String name, Duration defaultValue) Retrieves a Duration where the String is of the format nnn[unit] where nnn represents an integer value and unit represents a time unit.getIntegerProperty
(String[] prefixes, String key, Supplier<Integer> supplier) Retrieves a property that may be prefixed by more than one string.int
getIntegerProperty
(String name, int defaultValue) Gets the named property as an integer.getLongProperty
(String[] prefixes, String key, Supplier<Long> supplier) Retrieves a property that may be prefixed by more than one string.long
getLongProperty
(String name, long defaultValue) Gets the named property as a long.static PropertiesUtil
Returns the PropertiesUtil used by Log4j.getStringProperty
(String name) Gets the named property as a String.getStringProperty
(String[] prefixes, String key, Supplier<String> supplier) Retrieves a property that may be prefixed by more than one string.getStringProperty
(String name, String defaultValue) Gets the named property as a String.static Properties
Return the system properties or an empty Properties object if an error occurs.boolean
hasProperty
(String name) Returnstrue
if the specified property is defined, regardless of its value (it may not have a value).boolean
Returns true if system properties tell us we are running on Windows.static Map<String,
Properties> partitionOnCommonPrefixes
(Properties properties) Partitions a properties map based on common key prefixes up to the first period.static Map<String,
Properties> partitionOnCommonPrefixes
(Properties properties, boolean includeBaseKey) Partitions a properties map based on common key prefixes up to the first period.void
reload()
Deprecated.since 2.24.0 caching of property values is disabled.void
removePropertySource
(PropertySource propertySource) Removes aPropertySource
.
-
Constructor Details
-
PropertiesUtil
Constructs a PropertiesUtil using a given Properties object as its source of defined properties.- Parameters:
props
- the Properties to use by default
-
PropertiesUtil
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
-
-
Method Details
-
getProperties
Returns the PropertiesUtil used by Log4j.- Returns:
- the main Log4j PropertiesUtil instance.
-
addPropertySource
Allows aPropertySource
to be added afterPropertiesUtil
has been created.- Parameters:
propertySource
- thePropertySource
to add.- Since:
- 2.19.0
-
removePropertySource
Removes aPropertySource
.- Parameters:
propertySource
- thePropertySource
to remove.- Since:
- 2.24.0
-
hasProperty
Returnstrue
if the specified property is defined, regardless of its value (it may not have a value).- Parameters:
name
- the name of the property to verify- Returns:
true
if the specified property is defined, regardless of its value- Since:
- 2.9.0
-
getBooleanProperty
Gets the named property as a boolean value. If the property matches the string"true"
(case-insensitive), then it is returned as the boolean valuetrue
. Any other non-null
text in the property is consideredfalse
.- Parameters:
name
- the name of the property to look up- Returns:
- the boolean value of the property or
false
if undefined.
-
getBooleanProperty
Gets the named property as a boolean value.- Parameters:
name
- the name of the property to look updefaultValue
- the default value to use if the property is undefined- Returns:
- the boolean value of the property or
defaultValue
if undefined.
-
getBooleanProperty
public boolean getBooleanProperty(String name, boolean defaultValueIfAbsent, boolean defaultValueIfPresent) Gets the named property as a boolean value.- Parameters:
name
- the name of the property to look updefaultValueIfAbsent
- the default value to use if the property is undefineddefaultValueIfPresent
- the default value to use if the property is defined but not assigned- Returns:
- the boolean value of the property or
defaultValue
if undefined. - Since:
- 2.9.0
-
getBooleanProperty
Retrieves a property that may be prefixed by more than one string.- Parameters:
prefixes
- The array of prefixes.key
- The key to locate.supplier
- The method to call to derive the default value. If the value is null, null will be returned if no property is found.- Returns:
- The value or null if it is not found.
- Since:
- 2.13.0
-
getCharsetProperty
Gets the named property as a Charset value.- Parameters:
name
- the name of the property to look up- Returns:
- the Charset value of the property or
Charset.defaultCharset()
if undefined. - Since:
- 2.8
-
getCharsetProperty
Gets the named property as a Charset value. If we cannot find the named Charset, see if it is mapped in fileLog4j-charsets.properties
on the class path.- Parameters:
name
- the name of the property to look updefaultValue
- the default value to use if the property is undefined- Returns:
- the Charset value of the property or
defaultValue
if undefined. - Since:
- 2.8
-
getDoubleProperty
Gets the named property as a double.- Parameters:
name
- the name of the property to look updefaultValue
- 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. - Since:
- 2.6
-
getIntegerProperty
Gets the named property as an integer.- Parameters:
name
- the name of the property to look updefaultValue
- the default value to use if the property is undefined- Returns:
- the parsed integer value of the property or
defaultValue
if it was undefined or could not be parsed.
-
getIntegerProperty
Retrieves a property that may be prefixed by more than one string.- Parameters:
prefixes
- The array of prefixes.key
- The key to locate.supplier
- The method to call to derive the default value. If the value is null, null will be returned if no property is found.- Returns:
- The value or null if it is not found.
- Since:
- 2.13.0
-
getLongProperty
Gets the named property as a long.- Parameters:
name
- the name of the property to look updefaultValue
- the default value to use if the property is undefined- Returns:
- the parsed long value of the property or
defaultValue
if it was undefined or could not be parsed.
-
getLongProperty
Retrieves a property that may be prefixed by more than one string.- Parameters:
prefixes
- The array of prefixes.key
- The key to locate.supplier
- The method to call to derive the default value. If the value is null, null will be returned if no property is found.- Returns:
- The value or null if it is not found.
- Since:
- 2.13.0
-
getDurationProperty
Retrieves a Duration where the String is of the format nnn[unit] where nnn represents an integer value and unit represents a time unit.- Parameters:
name
- The property name.defaultValue
- The default value.- Returns:
- The value of the String as a Duration or the default value, which may be null.
- Since:
- 2.13.0
-
getDurationProperty
Retrieves a property that may be prefixed by more than one string.- Parameters:
prefixes
- The array of prefixes.key
- The key to locate.supplier
- The method to call to derive the default value. If the value is null, null will be returned if no property is found.- Returns:
- The value or null if it is not found.
- Since:
- 2.13.0
-
getStringProperty
Retrieves a property that may be prefixed by more than one string.- Parameters:
prefixes
- The array of prefixes.key
- The key to locate.supplier
- The method to call to derive the default value. If the value is null, null will be returned if no property is found.- Returns:
- The value or null if it is not found.
- Since:
- 2.13.0
-
getStringProperty
Gets the named property as a String.- Parameters:
name
- the name of the property to look up- Returns:
- the String value of the property or
null
if undefined.
-
getStringProperty
Gets the named property as a String.- Parameters:
name
- the name of the property to look updefaultValue
- the default value to use if the property is undefined- Returns:
- the String value of the property or
defaultValue
if undefined.
-
getSystemProperties
Return the system properties or an empty Properties object if an error occurs.- Returns:
- The system properties.
-
reload
Deprecated.since 2.24.0 caching of property values is disabled.Reloads all properties. This is primarily useful for unit tests.- Since:
- 2.10.0
-
extractSubset
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.
- Since:
- 2.4
-
partitionOnCommonPrefixes
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 partitionincludeBaseKey
- 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
-
isOsWindows
public boolean isOsWindows()Returns true if system properties tell us we are running on Windows.- Returns:
- true if system properties tell us we are running on Windows.
- Since:
- 2.5
-