Package org.logevents.config
Class Configuration
- java.lang.Object
-
- org.logevents.config.Configuration
-
public class Configuration extends Object
Used to configureLogEventObserver
instances. InstantiateConfiguration
withMap
and a String prefix and get values withgetString(java.lang.String)
andoptionalString(java.lang.String)
. Values are read using the prefix + the key given togetString(java.lang.String)
, or from environment variables. E.g. if prefix is "observer.console",getString("threshold")
looks for property "observer.console.threshold" or environment variable "LOGEVENTS_OBSERVER_CONSOLE_THRESHOLD".Use
createInstance(java.lang.String, java.lang.Class<T>)
to create objects based on configuration, and special methodsgetMdcFilter()
,getPackageFilter()
,getApplicationName()
,getNodeName()
andgetApplicationNode()
to read commonly used configuration. UseoptionalGlobalString(String)
to read values that may be configured for several observers at once.
-
-
Constructor Summary
Constructors Constructor Description Configuration()
Configuration(Map<String,String> properties, String prefix)
Configuration(Map<String,String> properties, String prefix, Map<String,String> environment)
Configuration(Properties properties, String prefix)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
checkForUnknownFields()
Checks if all non-empty values inproperties
prefixed withprefix
have been requested withoptionalString(java.lang.String)
(or a method using it) and throws an exception with any unused properties.boolean
containsKey(String key)
Returns true if this key (prefixed with the current context) is either in the configuration properties or an environment variableLogEventFormatter
createFormatter(String key, Class<? extends LogEventFormatter> defaultClass)
<T> T
createInstance(String key, Class<T> clazz)
Instantiates a class with the name in the provided property name and verifies that it's a subtype of the argument class.<T> T
createInstance(String key, Class<T> clazz, String defaultPackage)
Instantiates a class with the name in the provided property name.<T> T
createInstanceOrGlobal(String key, Class<T> targetType, Class<? extends T> defaultClass)
Instantiates a class with the name in the provided property name and verifies that it's a subtype of the targetType.<T> T
createInstanceWithDefault(String key, Class<? extends T> defaultClass, String packageName)
<T> T
createInstanceWithDefault(String key, Class<T> defaultClass)
Instantiates a class with the name in the provided property name and verifies that it's a subclass of the defaultClass.<T> T
createInstanceWithDefault(String key, Class<T> targetType, Class<? extends T> defaultClass)
LogEventObserver
createObserver(String key)
String
getApplicationName()
Ifobserver.whatever.applicationName
orobserver.*.applicationName
is set, returns that value, otherwise calculates the name of the application based on the JAR-file of the main class.String
getApplicationNode()
boolean
getBoolean(String key)
boolean
getBoolean(String key, boolean defaultValue)
Duration
getDuration(String key)
List<String>
getGlobalStringList(String key)
Locale
getLocale()
static Optional<String>
getMainClassName()
MdcFilter
getMdcFilter()
Returns a filter to determine which MDC variables to include in input.String
getNodeName()
Ifobserver.whatever.nodeName
orobserver.*.nodeName
is set, returns that value, otherwise returns the hostname of the computer running this JVM.List<String>
getPackageFilter()
Returns a list of classname prefixes that should be removed from stack tracesString
getPrefix()
String
getServerUser()
String
getString(String key)
Convenience method foroptionalString(String)
which throwsLogEventConfigurationException
if key is missing.List<String>
getStringList(String key)
List<String>
getStringListOrGlobal(String key)
URL
getUrl(String key)
static boolean
isRunningInTest()
Set<String>
listProperties(String key)
List all direct property names under the specified key.Optional<Duration>
optionalDuration(String key)
Optional<String>
optionalGlobalString(String key)
Returns global value from properties or environment.Optional<Integer>
optionalInt(String key)
Optional<String>
optionalString(String key)
Returns prefixed value from properties or environment.Optional<String>
optionalStringOrGlobal(String key)
Returns prefixed or global value from properties or environment.Optional<URL>
optionalUrl(String key)
String
prefixedKey(String key)
String
toString()
-
-
-
Method Detail
-
checkForUnknownFields
public void checkForUnknownFields()
Checks if all non-empty values inproperties
prefixed withprefix
have been requested withoptionalString(java.lang.String)
(or a method using it) and throws an exception with any unused properties. When implementing an observer, call this method after you have read the whole configuration to alert the user of misconfiguration
-
containsKey
public boolean containsKey(String key)
Returns true if this key (prefixed with the current context) is either in the configuration properties or an environment variable
-
listProperties
public Set<String> listProperties(String key)
List all direct property names under the specified key. For example, if a Configuration with prefix "observer.test" has properties "observer.test.markers.a.foo", "observer.test.markers.a.bar" and "observer.test.markers.b",listProperties("markers")
will return ["a", "b"].
-
optionalString
public Optional<String> optionalString(String key)
Returns prefixed value from properties or environment. For example, ifprefix
is "observer.console",optionalString("threshold")
will check property value "observer.console.threshold" and environment variable "LOGEVENTS_OBSERVER_CONSOLE_THRESHOLD"
-
optionalGlobalString
public Optional<String> optionalGlobalString(String key)
Returns global value from properties or environment. For example,optionalString("threshold")
will check property value "observer.*.threshold" and environment variable "LOGEVENTS_THRESHOLD"
-
optionalStringOrGlobal
public Optional<String> optionalStringOrGlobal(String key)
Returns prefixed or global value from properties or environment. For example, ifprefix
is "observer.console",optionalStringOrGlobal("threshold")
will check property values "observer.console.threshold" and "observer.*.threshold" and environment variables "LOGEVENTS_OBSERVER_CONSOLE_THRESHOLD" and "LOGEVENTS_CONSOLE".
-
getString
public String getString(String key)
Convenience method foroptionalString(String)
which throwsLogEventConfigurationException
if key is missing.
-
getBoolean
public boolean getBoolean(String key)
-
getBoolean
public boolean getBoolean(String key, boolean defaultValue)
-
createInstance
public <T> T createInstance(String key, Class<T> clazz)
Instantiates a class with the name in the provided property name and verifies that it's a subtype of the argument class. If the class has a constructor with String, Properties, this is invoked with the argument key added to the current prefix. Otherwise, the default constructor is used.
-
createInstance
public <T> T createInstance(String key, Class<T> clazz, String defaultPackage)
Instantiates a class with the name in the provided property name. If the configured class name doesn't have a package name, defaultPackage.- See Also:
createInstance(String, Class)
-
createInstanceWithDefault
public <T> T createInstanceWithDefault(String key, Class<T> defaultClass)
Instantiates a class with the name in the provided property name and verifies that it's a subclass of the defaultClass. If no property is configured, the defaultClass is used instead- See Also:
createInstance(String, Class)
-
createInstanceOrGlobal
public <T> T createInstanceOrGlobal(String key, Class<T> targetType, Class<? extends T> defaultClass)
Instantiates a class with the name in the provided property name and verifies that it's a subtype of the targetType. If no property is configured, the defaultClass is used instead- See Also:
createInstance(String, Class)
-
createInstanceWithDefault
public <T> T createInstanceWithDefault(String key, Class<T> targetType, Class<? extends T> defaultClass)
-
createInstanceWithDefault
public <T> T createInstanceWithDefault(String key, Class<? extends T> defaultClass, String packageName)
-
createFormatter
public LogEventFormatter createFormatter(String key, Class<? extends LogEventFormatter> defaultClass)
-
createObserver
public LogEventObserver createObserver(String key)
-
getMdcFilter
public MdcFilter getMdcFilter()
Returns a filter to determine which MDC variables to include in input. These are set either withobserver.<name>.includedMdcKeys
,observer.<name>.excludedMdcKeys
,observer.*.includedMdcKeys
,observer.*.excludedMdcKeys
. Only one of the options is used and inclusion is preferred over exclusion.
-
getPackageFilter
public List<String> getPackageFilter()
Returns a list of classname prefixes that should be removed from stack traces
-
getLocale
public Locale getLocale()
-
getServerUser
public String getServerUser()
-
getApplicationNode
public String getApplicationNode()
-
getNodeName
public String getNodeName()
Ifobserver.whatever.nodeName
orobserver.*.nodeName
is set, returns that value, otherwise returns the hostname of the computer running this JVM.
-
getApplicationName
public String getApplicationName()
Ifobserver.whatever.applicationName
orobserver.*.applicationName
is set, returns that value, otherwise calculates the name of the application based on the JAR-file of the main class. If run from a directory classpath, use the name of the current working directory instead
-
isRunningInTest
public static boolean isRunningInTest()
-
getPrefix
public String getPrefix()
-
-