Search code examples
tomcatlog4jlog4j2

Setting system properties for Log4j in Tomcat


From this article, to enable the Log4j 1.2 bridge, you should

  1. Set the system property “log4j1.compatibility” to a value of “true”. Log4j 2 will then add log4j.properties, log4j-test.properties, log4j.xml and log4j-test.xml to the configuration files it searches for on the class path.
  2. Set the Log4j 1 system property “log4j.configuration” to the location of the log4j 1 configuration file. The files must have a file extension of either “.properties” or “.xml”.

But how to set the system property if my class is running under Tomcat 8.5?


Solution

  • First you need to understand what is a system property. Check this

    Then, if your concern is just how to add a system property on tomcat like classic ram increment, you just need to add key=value to the JAVA_OPTS or CATALINA_OPTS on some script of tomcat:

    -Dlog4j1.compatibility=true
    

    If you don't want to change some default tomcat file, you could create the setenv.sh in the bin folder and add something like this:

    #!/bin/sh
    
    MIN_MEMORY="384m"
    MAX_MEMORY="768m"
    LOG4J_BRIDGE="-Dlog4j1.compatibility=true"
    
    JAVA_OPTS="-Xms${MIN_MEMORY} -Xmx${MAX_MEMORY} ${LOG4J_BRIDGE}  ${JAVA_OPTS}"
    

    Basically tomcat say us: If you want to add, change or override special tomcat settings, add them in the file setenv.sh and I will load that vars before the startup.

    Here are some setenv.sh examples to see more special java/tomcat system properties and environment variables: