From this article, to enable the Log4j 1.2 bridge, you should
- 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.
- 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?
CodePudding user response:
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:
- https://gist.github.com/chewbakartik/e8f165f6b8c89d8461abe3c4fc6531c4
- https://gist.github.com/jeduoliveira/9f06ba3f20975c57bc9d0b6e5c1c0462
- https://gist.github.com/patmandenver/cadb5f3eb567a439ec01
- https://gist.github.com/hardyoyo/8664b2171d26adcf7b7e
- https://gist.github.com/lesstif/98dfb8c0eeae28e5edb1
CodePudding user response:
Whenever Log4j documentation says "system properties" it actually refers to any available property source (cf. System properties section). This gives you a rather large number 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.propertieswhich is sourced by Tomcat just after it sets up its own logging system (see this question), - adding the system property to a
log4j2.system.propertiesfile 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.
If you want to set it for your application only, put it in a log4j2.component.properties file in your application's classpath.
