0

I'm currently developing a Java Struts2 webapp (using Maven) and have successfully configured the use of datasources to connect with the database. Since May application needs the ability of being moved between environments (Development > Homologation > Production), it must use server config files to load paramenters (such as the datasources and log levels).

I can't find any references on how to configure a java webapp to use a log4j.xml file from outside the application (out of the WAR). With maven I simply put the log4j.xml inside my src/main/resources folder and it loads automatically. What should I do to ensure that my webapp loads a external log4j.xml file on deploy?

Felipe Leão
  • 915
  • 2
  • 15
  • 27
  • Configure Log4J programmatically, maybe in a custom ServletContextListener or @WebListener, see [here](http://stackoverflow.com/questions/8965946/configuring-log4j-loggers-programmatically) – Stefan Jun 05 '14 at 18:49

1 Answers1

5

Maven by default places all files from src/main/resources into /WEB-INF/classes/. With log4j, you can specify where your configuration file is located with help of Web App Deployment Descriptor (web.xml). Simply, add log4j listener:

<listener>
    <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>

And context parameter:

<context-param>
    <param-name>log4jConfigLocation</param-name>
    <param-value>classpath:log4j.xml</param-value>
</context-param>

classpath: is equivalent to /WEB-INF/classes, you may specify any path for example, with ${catalina.home}, a tomcat specific variable. Use it with file:// prefix

kamil
  • 3,482
  • 1
  • 40
  • 64