Redirect Tomcat 9 servlet exception logs to log4j2

442 Views Asked by At

I am using log4j2 for logging my JSF web application.

  1. I created log4j2.xml file under WEB-INF folder and configured it

  2. Then added these dependencies to my pom.xml and used log4j2 in my application

    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-api</artifactId>
        <version>2.13.3</version>
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-core</artifactId>
        <version>2.13.3</version>
    </dependency>
    
  3. I followed official guide for log4j2 integration with my Tomcat 9 here Tomcat log4j2

  4. I added org.apache.logging.log4j:log4j-jul:2.13.3 jar to the created lib directory in step 3 then configured Tomcat logging by adding the following code to setenv.bat in Tomcat/bin

    set LOGGING_MANAGER=-Djava.util.logging.manager=org.apache.logging.log4j.jul.LogManager

Now I can get Tomcat log messages, my web application logs and JSF logs printed according to my log4j2 format.

The problem is, I am still getting javax.servlet.ServletException messages which do not follow my log4j2 log format. They seem to ignore my log4j2 configuration.

How can I fix this issue?

Thank you.

1

There are 1 best solutions below

0
On BEST ANSWER

I figured out that javax.servlet.ServletException messages were not handled by my application. Therefore they are not formatted according to my log4j2 configuration. I have solved this issue by creating a custom JSF exception handler and logging catched exceptions with log4j2.