tomcat 7 internal logging with log4j2.xml

Paramita Banerjee picture Paramita Banerjee · Feb 11, 2015 · Viewed 15.9k times · Source

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:

  • put log4j2.xml in $CATALINA_BASE/lib
  • download tomcat-juli.jar and tomcat-juli-adapters.jar from "extras"
  • put 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.
  • replace $CATALINA_HOME/bin/tomcat-juli.jar with tomcat-juli.jar from "extras".
  • delete $CATALINA_BASE/conf/logging.properties
  • set the logging manager to use the manager from the 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.

Answer

thorondor picture thorondor · Feb 25, 2016

I took the following steps and it worked for me.

  1. Put the following jars in $CATALINA_HOME/lib
    • log4j 2 core (log4j-core-2.4.1.jar)
    • log4j 2 api (log4j-api-2.4.1.jar)
    • log4j 2 bridge for log4 j 1.0 (log4j-1.2-api-2.4.1.jar)
    • tomcat-juli-adapters.jar from tomcat extras
  2. Replace existing $CATALINA_HOME/bin/tomcat-juli.jar with the tomcat-juli.jar from tomcat extras
  3. Delete the file $CATALINA_HOME/conf/logging.properties
  4. Put the new log4j 2 config file (log4j2.xml) in $CATALINA_HOME/lib

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