Log4j template file for multiple environments

I want to have a single WAR file that will work in different environments (like dev, ci and prod) and spring, mainly for our use cases. log4j for the environment is one of the standout elements. As suggested in other answers , we can have a log4j config for each environment, but there is a lot of repetition in the files for each of the environments and the only things that differ are minor properties like Address SMTPAppender etc. Is there an easy way that I can use a config file with a configuration like log4j and have parts that are different for each environment, replaced at runtime? Maven won't work here because it will trigger compile time selection.

One solution I can think of is extending Log4jConfigurer to read some of the environment variables, namely map the template key-value pairs and replace the template variables in the template file with the actual values ​​before passing it to the DOMConfigurator. Just wanted to check if there are any easier ways to do this, or if I am barking the wrong tree in terms of best practices.

Thanks in advance!

+3


source to share


1 answer


I don't quite understand your case, but property placeholders should help: http://jvleminc.blogspot.ru/2008/05/avoid-absolute-path-in-log4j-properties.html

  • Use a placeholder in the properties file when specifying the application file, for example

    log4j.appender.file.File=${log4j.logFile}
    
          

  • Pass this placeholder value as a VM parameter to (web application). The log4j engine will try to resolve the placeholder and eventually revert back to system options, so in our example:

    -Dlog4j.logFile=C:/logs/MyApplication.log
    
          



By the way, I think usually people are pushing out the logging configuration i.e. use an external log4j.properties file. Release engineers manage these configurations for each environment. It also allows you to modify the file and restart the application, and in some cases allows you to change some values ​​even without rebooting.

+1


source







All Articles