0

I need to solve the following task.

We are usign log4j in version 1.2x and want to keep it so if possible. We have a graylog appender in the log4j.properties file where we set the ip address and the port. If they are hardcoded, the logging to the gray log works.

The task is however to make these two parameters (IP and port) configurable somehow in the log4j.properties file.

Afterwards the operation team on each deployment environment would just set this configuration for the IP and port once and it would work in all the java projects that use this variable in log4j.properties file.

Ideal solution would be using environment variable of the operating system, but any other configuration as a property file (not that one of the log4j itself and except for using JVM arguments - this is not an option for us) or some other solution

I have read that this is possible with log4j version 2.x but this would take us quite a lot of time (converting the log4j.propeties files to log4j2.xml by hand, changing the code ..).

I`ll be glad for any reasonable answer.

LubosD
  • 222
  • 4
  • 11
  • This is almost a duplicate. Do any of these answers help? [Using system environment variables in log4j xml configuration](http://stackoverflow.com/questions/201188/using-system-environment-variables-in-log4j-xml-configuration?rq=1) – Duncan Jones Mar 17 '15 at 09:50

2 Answers2

1

This could be done in the log4j.properties file. Note, you can not use environment variables (that's a platform specific concept).

These 2 properties (IP and port) can be set in code or using the "-D" JVM option.

In your example:

java ... -Dip=%YOUR_IP% -Dport=%YOUR_PORT% ... your_app

This allows you to reference the property using ${...} notation Eg:

log4j.graylog_appender.LOGGER.IP=${ip}
log4j.graylog_appender.LOGGER.PORT=${port}

Hope it helps!

aviad
  • 8,229
  • 9
  • 50
  • 98
  • Thanx for your answer aviad ;) . I have found this solution already before having asked my question and it works. But this isn't an option for us as the cofiguration should be in some property file or the like, not as a JVM arguments. – LubosD Mar 17 '15 at 10:07
0

System.setProperty("key", "value");

caot
  • 3,066
  • 35
  • 37