I need to use two separate log4j loggers in my Spring project running on tomcat 9. I started by getting the dependencies and placing in my pom.xml:
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.17.1</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.17.1</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-web</artifactId>
<version>2.17.1</version>
</dependency>
Then I created the xml log configuration file, and afterwards in my web.xml I placed the following:
<listener>
<listener-class>org.apache.logging.log4j.web.Log4jServletContextListener</listener-class>
</listener>
<filter>
<filter-name>log4jServletFilter</filter-name>
<filter-class>org.apache.logging.log4j.web.Log4jServletFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>log4jServletFilter</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
<dispatcher>INCLUDE</dispatcher>
<dispatcher>ERROR</dispatcher>
</filter-mapping>
<context-param>
<param-name>log4jContextName</param-name>
<param-value>PersonLogger</param-value>
</context-param>
<context-param>
<param-name>log4jConfiguration</param-name>
<param-value>file:../conf/properties/PersonLogConfig.xml</param-value>
</context-param>
Then, in my application I used it like so:
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
static final Logger LOG = LogManager.getLogger("PersonLogger." + PersonAgentImpl.class);
LOG.info("working");
This all worked without issue, the folder and log file were created and logged to as expected. However when I tried to add a second logger- it would not work. Neither folder/log file would be created. (they both work separately if I comment out one though)
This is how I attempted to set up two instances in my web.xml:
<listener>
<listener-class>org.apache.logging.log4j.web.Log4jServletContextListener</listener-class>
</listener>
<filter>
<filter-name>log4jServletFilter</filter-name>
<filter-class>org.apache.logging.log4j.web.Log4jServletFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>log4jServletFilter</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
<dispatcher>INCLUDE</dispatcher>
<dispatcher>ERROR</dispatcher>
</filter-mapping>
<!-- PersonLogger -->
<context-param>
<param-name>log4jContextName1</param-name>
<param-value>PersonLogger</param-value>
</context-param>
<context-param>
<param-name>log4jConfiguration1</param-name>
<param-value>file:../conf/properties/PersonLogConfig.xml</param-value>
</context-param>
<!-- AnimalLogger -->
<context-param>
<param-name>log4jContextName2</param-name>
<param-value>AnimalLogger</param-value>
</context-param>
<context-param>
<param-name>log4jConfiguration2</param-name>
<param-value>file:../conf/properties/AnimalLogConfig.xml</param-value>
</context-param>
Any ideas on what might be wrong? From what I understand as long as the param names are different than 2 instances of log4j should be able to work at the same time- using the context name to specify which one is used.