Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I specify a default value for a log4j property

Tags:

log4j

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?

like image 666
datguy Avatar asked Oct 20 '22 20:10

datguy


1 Answers

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.

like image 156
user3341082 Avatar answered Oct 24 '22 01:10

user3341082