I am trying to configure tomcat 7 internal logging with log4j2
. I have followed the answer provided at Logging server classes in Tomcat 6 with log4j2.
I am using tomcat 7.0.54, and log4j-core-2.1.jar
, log4j-api-2.1.jar
.
I have down loaded the extras and did all the steps below, but when I start tomcat, I get an error:
ERROR StatusLogger No log4j2 configuration file found. Using default configuration: logging only errors to the console.
These are the steps I performed:
log4j2.xml
in $CATALINA_BASE/lib
tomcat-juli.jar
and tomcat-juli-adapters.jar
from "extras"log4j-api-2.1.jar
, log4j-core-2.1.jar
, log4j-jul-2.1.jar
, and tomcat-juli-adapters.jar
from "extras" into $CATALINA_HOME/lib
.$CATALINA_HOME/bin/tomcat-juli.jar
with tomcat-juli.jar
from "extras".$CATALINA_BASE/conf/logging.properties
log4j2-jul
bridge (log4j-jul-2.1.jar
). Alter catalina.sh
to ensure that the classpath includes bin/tomcat-juli.jar
, lib/log4j-jul-2.1.jar
, lib/log4j-api-2.1.jar
and lib/log4j-core-2.1.jar
, and the command used to start tomcat includes
-Djava.util.logging.manager=org.apache.logging.log4j.jul.LogManager`I even tried adding this (LOGGING_CONFIG="-Djava.util.logging.config.file=$CATALINA_HOME/lib/log4j2.xml"
) in catalina.sh
but didn't work.
Please let me know if anyone could configure it successfully.
For Docker you can use the following approach.
1 Add a Maven plugin to copy the dependancies to the target directory:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>3.3.0</version>
<executions>
<execution>
<id>copy</id>
<phase>package</phase>
<goals>
<goal>copy</goal>
</goals>
</execution>
</executions>
<configuration>
<artifactItems>
<artifactItem>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>${log4j.version}</version>
<destFileName>log4j-api.jar</destFileName>
</artifactItem>
<artifactItem>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>${log4j.version}</version>
<destFileName>log4j-core.jar</destFileName>
</artifactItem>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-jul</artifactId>
<version>${log4j.version}</version>
<destFileName>log4j-jul.jar</destFileName>
</dependency>
<dependency>
<groupId>com.lmax</groupId>
<artifactId>disruptor</artifactId>
<version>3.4.2</version>
<destFileName>disruptor.jar</destFileName>
</dependency>
</artifactItems>
<outputDirectory>${project.build.directory}</outputDirectory>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>true</overWriteSnapshots>
</configuration>
</plugin>
2 Add a setenv.sh to your repo:
CLASSPATH=/usr/local/tomcat/lib/log4j-api.jar:/usr/local/tomcat/lib/log4j-core.jar:/usr/local/tomcat/lib/log4j-jul.jar:/usr/local/tomcat/lib/disruptor.jar
LOGGING_MANAGER=-Djava.util.logging.manager=org.apache.logging.log4j.jul.LogManager
3 Finally create a Dockerfile
FROM tomcat:8.5-jre8-openjdk-slim
# Copy log4j2.
COPY --chmod=0755 setenv.sh /usr/local/tomcat/bin/setenv.sh
COPY target/log4j-api.jar /usr/local/tomcat/lib/log4j-api.jar
COPY target/log4j-core.jar /usr/local/tomcat/lib/log4j-core.jar
COPY target/log4j-jul.jar /usr/local/tomcat/lib/log4j-jul.jar
COPY target/disruptor.jar /usr/local/tomcat/lib/disruptor.jar
# Delete default logging file
RUN rm -rf /usr/local/tomcat/conf/logging.properties
COPY target/classes/log4j2-fargate.xml /usr/local/tomcat/lib/log4j2.xml
COPY target/salesapp-cloudcache.war /usr/local/tomcat/webapps/ROOT.war
ENV JAVA_OPTS="-Dlog4j2.configurationFile=/usr/local/tomcat/lib/log4j2.xml"
EXPOSE 8080
CMD ["catalina.sh", "run"]
4 Then build and run your Docker container.
I took the following steps and it worked for me.
The trick is to follow the official tomcat 7 documentation to setup log4J 1.X but instead use log4j2 artifacts instead. Also this solution does not require any changes in $CATALINA_HOME/bin/catalina.sh or any other files $CATALINA_HOME/bin
My mistake, I needed to include $CATALINA_BASE/lib
in classpath for log4j2.xml
to be picked up.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With