1

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?

Piotr P. Karwasz
  • 12,857
  • 3
  • 20
  • 43
Gary Liu
  • 41
  • 1
  • 6

3 Answers3

2

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:

JRichardsz
  • 14,356
  • 6
  • 59
  • 94
  • Thanks JRicardsz. Since I'm running Tomcat in Windows as a service, I use tomcat8w.exe to set the Java Options and it works. – Gary Liu Jan 26 '22 at 07:19
2

Whenever Log4j documentation says "system properties" it actually refers to any available property source (cf. System properties section). This gives you a rather large spectrum of possibilities.

If you want to set log4j.configuration globally for all Java applications, set the environment variable LOG4J_CONFIGURATION.

If you want to set it for all applications running on Tomcat, set a Java system property, by either:

  • passing the -Dlog4j.configuration=... option to the JVM as in JRichardsz's answer.

  • adding the system property to $CATALINA_BASE/conf/catalina.properties which is sourced by Tomcat just after it sets up its own logging system (see this question),

  • adding the system property to a log4j2.system.properties file in Tomcat's server classpath (e.g. $CATALINA_BASE/lib/log4j2.system.properties), which is sourced by Log4j on startup (cf. source code).

  • Putting such a file in your application would be bad practice, since the contents of the file end up in Java's system properties and would apply to all applications. If you want to set it for your application only, put it in a log4j2.component.properties file in your application's classpath.

Piotr P. Karwasz
  • 12,857
  • 3
  • 20
  • 43
  • Thanks Piotr. I tried catalina.properties and log4j2.component.properties, and they all work. I will use the log4j2.component.propertes way. – Gary Liu Jan 26 '22 at 07:35
1

Just as a reminder. If you run Tomcat as a service in Windows, you can set the Java Options in tomcat8w.exe application.

enter image description here

Gary Liu
  • 41
  • 1
  • 6
  • You could use a stronger statement: you **must** use Tomcat Monitor/Procmgr. If you run the server as a Windows service `catalina.bat` and `setenv.bat` are **not** used. – Piotr P. Karwasz Jan 26 '22 at 08:09