I want to have a single configuration that will run in multiple environments by changing a system property. Following is an example:
log4j.appender.file.File=/opt/apps/${myEnvironment}/log/MyService.log
However, I want to make sure it still functions properly if someone forgets to set the system property. Is there a way to specify a default value in the properties file?
That might be possible. According to the API docs for org.apache.log4j.PropertyConfigurator
, your variable should first look for its value in the system properties, and if it is not found there, it should instead search through the configuration file.
All option values admit variable substitution. The syntax of variable substitution is similar to that of Unix shells. The string between an opening "${" and closing "}" is interpreted as a key. The value of the substituted variable can be defined as a system property or in the configuration file itself. The value of the key is first searched in the system properties, and if not found there, it is then searched in the configuration file being parsed. The corresponding value replaces the ${variableName} sequence. For example, if java.home system property is set to /home/xyz, then every occurrence of the sequence ${java.home} will be interpreted as /home/xyz.
https://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/PropertyConfigurator.html
You will have to verify if the variable set in the configuration file would override the system properties though.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With