Search code examples
log4jtomcat7virtualhost

Variable substitution in Log4J for multiple instances of same webapp


I have one webapp that I deploy multiple times on Tomcat (version 7) separated by host. The webapp uses Log4J for logging and I'd like use variable substitution for log path, e.g.

log4j.appender.file.File=${webapp_log_dir}/logfile.log

I cannot find other ways to perform this variable substitution than through System.setProperty("webapp_log_dir", "some path"), but this is not useful, as there is only one system, hence this system-wide variable will be overwritten on every new deployment of the webapp.

How can I define a property replacement that has a more narrow scope, so it will be app-local / host-local? Do I need to use another logging mechanism that Log4J?


Solution

  • In Log4j 1.x you can only use Java System Properties (which are global) for interpolation.

    Log4j 2.x offers a much larger set of property sources: cf. Log4j 2 Lookups. The Web Lookup is probably what you are looking for.

    It's also easy to write your own lookup (cf. my log4j-tomcat) for example.

    BTW: Log4j 1.x is unsupported since 2015 and has several known vulnerabilities. Tomcat 7.x reached end-of-life in 2021.