Tuesday, 15 July 2014

java - How is it possible to reference a variable from log4j.properties file? -


I have to solve the following tasks.

We are usign log4j in version 1.2x and want to keep it if possible, we have a graylog app in log4j.properties file where we set IP address and port. If they are harsh, logging to the gray logs to work.

Although the task is to configure these two parameters (IP and port) in any way, log4j.properties file.

Later on each deployment environment, the operation team will set this configuration for IP and port only once and it will work in all Java projects that are available in the log4j.properties file Use.

The ideal solution would be to use the environment variable of the operating system, but leaving the other file as a valid file (which is not one of log4j but to use the JVM logic - it is our Or some other solution)

I've read that it is possible with log4j version 2.x but it will take a lot of time (Change log4j.propaches files

This can be done in the log4j.properties file. Environmental variables can not be used (this is a platform specific concept).

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

In your example:

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

This allows you to use the $ {...} encoding example To refine the property:

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

Hope it helps!


No comments:

Post a Comment