0

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.

  • Where did you get the understanding that you can use alternate names to `log4jContextName`? I don't know about this specific value in particular, but my general knowledge of log4j suggests that context parameter names are always specific values that log4j is looking for. Looking at [the docs](https://people.apache.org/~nickwilliams/log4j/manual/webapp.html#:~:text=Parameters%20section%20below.-,Context,-Parameters), being able to do this isn't mentioned. – CryptoFool Jan 04 '23 at 23:02
  • @CryptoFool Ah you're right, looks like a single one doesn't work without log4jContextName - adding a 1/2 causes it to fail. – Magitrix Alyxra Jan 04 '23 at 23:09

0 Answers0