How to configure log4j2 with Tomcat 8.5.15
Asked Answered
L

5

7

I have log4j2 jars under $CATALINA_HOME/lib:

  • log4j-api-2.10.0.jar
  • log4j-core-2.10.0.jar
  • log4j-jul-2.10.0.jar

export JAVA_OPTS="${JAVA_OPTS} -Djava.util.logging.manager=org.apache.logging.log4j.jul.LogManager"

In catalina.properties I've got common classloader and I tried to add log4j-jul-2.10.0.jar again even if it is already under the CATALINA_HOME/lib, but no success.

common.loader="${catalina.base}/lib","${catalina.base}/lib/.jar","${catalina.home}/lib","${catalina.home}/lib/.jar","/opt/tomcat/apache-tomcat-8.5.15/lib/log4j-jul-2.10.0.jar"

I have deleted logging.properties under Tomcat and add a new log4j2.xml to path

ERRORMESSAGE:

Could not load Logmanager "org.apache.logging.log4j.jul.LogManager" java.lang.ClassNotFoundException: org.apache.logging.log4j.jul.LogManager

Any idea why LogManager is still missing or should I use some other jars instead. In another messages they are speaking juli.jar and extras, but in their case they have older Tomcat version, 6 or 7.

Loathing answered 15/3, 2018 at 12:35 Comment(0)
P
2

I know that this is a little late to answer this question, but I'm sure it could help someone struggling like me trying to configure tomcat so that it uses lo4j. I've been working on something similar for the past 3 days, and I found out that the extras folder provided by tomcat's website are not what I need. But, you can still grab them using maven. I was able to configure tomcat so that it uses the mentioned jar files ( tomcat-extras-juli.jar and tomcat-extras-juli-adapters.jar ). Just remember to include the VM argument -Dlog4j.debug to make your life easier and catch errors quicker.

Maven repo: https://mvnrepository.com/artifact/org.apache.tomcat.extras/tomcat-extras-juli-adapters

I came upon the same problem after I included the mentioned jars provided by tomcat's repository. After a quick analysis I found that the interface org.apache.juli.WebAppProperties was not included in the jar file tomcat-extras-juli.jar which is utilized by the file org.apache.catalina.loader.WebappClassLoaderBase. After researching a bit more, I realized that the tomcat jar files are included in the Maven Repo. I downloaded the mentioned jar files under the same version of tomcat ( currently 8.5 ), plugged those jars in my tomcat installation and everything worked as expected. Now my version of tomcat uses log4j instead of juli.

Poinsettia answered 14/10, 2020 at 23:40 Comment(0)
C
3

You just need to add the log4j2-api, log4j2-core and log4j2-appserver libraries into the Tomcat classpath, provide the log4j2 configuration file and remove the $CATALINA_BASE/conf/logging.properties from your installation.

This is most easily done by:

  1. Creating a set of directories in catalina home named log4j2/lib and log4j2/conf.

  2. Placing log4j2-api-2.x.x.jar, log4j2-core-2.x.x.jar, and log4j2-appserver-2.x.x.jar in the log4j2/lib directory.

  3. Creating a file named log4j2-tomcat.xml, log4j2-tomcat.json, log4j2-tomcat.yaml, log4j2-tomcat.yml, or log4j2-tomcat.properties in the log4j2/conf directory.

  4. Create or modify setenv.sh in the tomcat bin directory to include CLASSPATH=$CATALINA_HOME/log4j2/lib/*:$CATALINA_HOME/log4j2/conf


You can force the applications that use the JUL framework to use log4j2 format changing the environment variable LOGGING_MANAGER. You can do this by adding in the setenv.sh file: LOGGING_MANAGER="-Djava.util.logging.manager=org.apache.logging.log4j.jul.LogManager"

Remember that org.apache.logging.log4j.jul.LogManager is included in the log4j-jul-2.x.x.jar bridge which must be added to your classpath.


refs:

https://db-blog.web.cern.ch/blog/luis-rodriguez-fernandez/2019-03-keeping-your-logs-clean-apache-tomcat-9-log4j2-and-spring-boot

https://logging.apache.org/log4j/2.x/log4j-appserver/index.html

Chronicles answered 25/1, 2021 at 13:13 Comment(0)
S
2

Create in tomcat\bin\ file setenv.bat and add to file:

set "CLASSPATH=%CLASSPATH%%CATALINA_BASE%\bin;%CATALINA_BASE%\bin\log4j-core-2.10.0.jar;%CATALINA_BASE%\bin\log4j-api-2.10.0.jar;%CATALINA_BASE%\bin\log4j-jul-2.10.0.jar"

copy jars files

log4j-api-2.10.0.jar 
log4j-core-2.10.0.jar 
log4j-jul-2.10.0.jar

to folder tomcat\bin\

create file log4j2.xml in tomcat\bin folder

Slub answered 12/11, 2019 at 12:17 Comment(0)
P
2

I know that this is a little late to answer this question, but I'm sure it could help someone struggling like me trying to configure tomcat so that it uses lo4j. I've been working on something similar for the past 3 days, and I found out that the extras folder provided by tomcat's website are not what I need. But, you can still grab them using maven. I was able to configure tomcat so that it uses the mentioned jar files ( tomcat-extras-juli.jar and tomcat-extras-juli-adapters.jar ). Just remember to include the VM argument -Dlog4j.debug to make your life easier and catch errors quicker.

Maven repo: https://mvnrepository.com/artifact/org.apache.tomcat.extras/tomcat-extras-juli-adapters

I came upon the same problem after I included the mentioned jars provided by tomcat's repository. After a quick analysis I found that the interface org.apache.juli.WebAppProperties was not included in the jar file tomcat-extras-juli.jar which is utilized by the file org.apache.catalina.loader.WebappClassLoaderBase. After researching a bit more, I realized that the tomcat jar files are included in the Maven Repo. I downloaded the mentioned jar files under the same version of tomcat ( currently 8.5 ), plugged those jars in my tomcat installation and everything worked as expected. Now my version of tomcat uses log4j instead of juli.

Poinsettia answered 14/10, 2020 at 23:40 Comment(0)
S
1

log4j2 jars must be loaded along with bootstrap.jar (tomcat startup) and tomcat-juli.jar (logging)

These jars are present in CATALINA_HOME/bin directory and are responsible for initialization of tomcat including logging.

In CATALINA_HOME/cataline.bat in case of windows, you will find below code -

set "CLASSPATH=%CLASSPATH%%CATALINA_HOME%\bin\bootstrap.jar"

Here, you should add log4j2 jars at the classpath so that when tomcat starts, these jars are there.

Syncopate answered 20/3, 2018 at 7:48 Comment(2)
Where can you get the tomcat-juli.jar? I looked here and it's missing? mirror.metrocast.net/apache/tomcat/tomcat-8/v8.5.39/bin/extrasViewing
Look inside the zip file - mirror.metrocast.net/apache/tomcat/tomcat-8/v8.5.39/bin/… . Extract this zip file and you will find this JAR inside bin directory.Syncopate
L
1

The issue is that the content of $CATALINA_BASE/lib is part of the Common class loader, but logging classes are required early and thus must be part of the System class loader. See Tomcat Class Loader How-To.

I would recommend creating a new log4j directory under $CATALINA_HOME, put the log4j config files and jars there, and add it to the classpath using setenv.sh (or setenv.bat under Windows):

$ ls $CATALINA_HOME/log4j
log4j2.xml
log4j-api-2.22.0.jar
log4j-core-2.22.0.jar
log4j-jul-2.22.0.jar
$ cat $CATALINA_HOME/bin/setenv.sh 
LOGGING_MANAGER="-Djava.util.logging.manager=org.apache.logging.log4j.jul.LogManager"
CLASSPATH="${CATALINA_HOME}/log4j/*:${CATALINA_HOME}/log4j"

NB: The classpath entry ${CATALINA_HOME}/log4j/* is a Class Path Wild Cards that includes all jars inside ${CATALINA_HOME}/log4j, but not this directory itself.

Lincolnlincolnshire answered 9/12, 2023 at 10:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.