This is not a solution only for the configuration file, so I leave the question open for a while.
I renamed log4j.properties to myapp-log4j.properties and changed the log file name property as follows:
log4j.appender.R.File=${catalina.base}/logs/#{context.name}.log
Since I have a servlet that loads at startup, I initialize log4j in the init () function.
String contextPath = getServletContext().getContextPath(); // First reconfigure log4j String contextName = contextPath.substring(1); if (!configureLog4J(contextName)) { return; }
The function for this:
public static final String LOG4JAPPENDERRFILE = "log4j.appender.R.File"; private boolean configureLog4J(String contextName) { Properties props = new Properties(); try { InputStream configStream = getClass().getResourceAsStream("/myapp-log4j.properties"); props.load(configStream); configStream.close(); } catch(IOException e) { System.out.println("FATAL! Cannot load log4j configuration file from classpath."); e.printStackTrace(System.out); return false; } String logFile = props.getProperty(LOG4JAPPENDERRFILE); logFile=logFile.replace("#{context.name}", contextName); props.setProperty(LOG4JAPPENDERRFILE, logFile); LogManager.resetConfiguration(); PropertyConfigurator.configure(props); return true; }
Everything seems to be working fine, but I don't really like that I need to change the properties file in the code.
NagyI
source share