I am migrating an existing web application from Tomcat 8.5 to Tomcat 9.
The webapp uses Log4j 1 with Commons-Logging. While everything used to work fine in Tomcat 8.5 I don't get any log output in Tomcat 9. Trying to get to the bottom of this, I created a simple test-servlet and also tried to migrate from Log4J1 to Log4J2. The effect is the same. I have found a similar post on Stackoverflow but the author stated at the end that the troubles originated from some SLF4J-dependencies, which does not count for my case. I am just using Log4J.
This is the pom.xml I am using
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>mygroup</groupId>
<artifactId>webtest2</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>webtest2 Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!-- Log4J 1.2 + commons-logging
https://o7planning.org/de/10151/anleitung-java-commons-logging
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.3</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
-->
<!-- Log4J 2 + commons-logging
https://www.logicbig.com/tutorials/misc/java-logging/jcl-log4j2.html -->
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.8.2</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-jcl</artifactId>
<version>2.8.2</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<finalName>webtest2</finalName>
</build>
</project>
Testcase 1: For Log4J1, following the tutorial for Log4J1 I am using the following commons-logging.properties
org.apache.commons.logging.Log=org.apache.commons.logging.impl.Log4JLogger
and the following log4j.properties
log4j.rootLogger=DEBUG, CA
#Console Appender
log4j.appender.CA=org.apache.log4j.ConsoleAppender
log4j.appender.CA.layout=org.apache.log4j.PatternLayout
log4j.appender.CA.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n
Testcase 2: For Log4J2, following the tutorial 1 for Log4J2 I am using the following log4j2.properties (the commons-logging.properties does not seem to be needed)
appenders=xyz
appender.xyz.type = Console
appender.xyz.name = myOutput
appender.xyz.layout.type = PatternLayout
appender.xyz.layout.pattern = %d{yy-MM-dd HH:mm:ss:SSS} %-5p %c{1} [%L] - %m%n
rootLogger.level = info
rootLogger.appenderRefs = abc
rootLogger.appenderRef.abc.ref = myOutput
Testcase 3: For Log4J2, alternatively I tried to use the log4j2.xml following the reference for Log4J2
<!-- https://logging.apache.org/log4j/2.x/manual/configuration.html -->
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</Console>
</Appenders>
<Loggers>
<Root level="info">
<AppenderRef ref="Console"/>
</Root>
</Loggers>
</Configuration>
All the simple test cases I stated, work fine with Tomcat 8.5. With Tomcat 9 I don't get any output. Specifically I am using Tomcat Version / JVM Version
Apache Tomcat/8.5.39 (Ubuntu) 11.0.9.1+1-Ubuntu-0ubuntu1.18.04
Apache Tomcat/9.0.16 (Ubuntu) 11.0.9.1+1-Ubuntu-0ubuntu1.18.04
Just as the author of the question I stated above, I am wondering if this has to do with some conflicts with the internal logging in Tomcat 9. But the logging-documentation explicitely states that any web app may come with its own logging. Right now, I am just lost and appreciate any help.
Thanx!!
PS:
In case you are interested, this is the servlet-code
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
* Servlet implementation class Testservlet
*/
public class Testservlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private Log log = LogFactory.getLog(Testservlet.class);
/**
* Default constructor.
*/
public Testservlet() {
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
log.info("Starting doGet ...");
response.getWriter().append("Served at: ").append(request.getContextPath());
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
log.info("Starting doPost ...");
doGet(request, response);
}
}
And this is the web.xml code
<web-app>
<display-name>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>Testservlet</servlet-name>
<display-name>Testservlet</display-name>
<description></description>
<servlet-class>mygroup.Testservlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Testservlet</servlet-name>
<url-pattern>/Testservlet</url-pattern>
</servlet-mapping>
</web-app>