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.
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:
Creating a set of directories in catalina home named log4j2/lib and
log4j2/conf.
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.
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.
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
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.
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.
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
Related
I have a problem with one of my tomcat installations.
The webapp do not pick up a specific log4j.properties file. The only way to make this work was by delete the $CATALINA_BASE/conf/logging.properties file and adding my log4j.properties file in $CATALINA_BASE/lib/ folder.
I was wondering how to configure it to pick up my webapp specific property file (preferably without deleting conf/logging.properties).
I have tried putting the log4j.properties file in WEB-INF/lib, WEB-INF/classes and in a jar to be picked up but to no avail. The only thing that worked was what I described earlier.
Tomcat version is 6.0.18
The properties file is correct as it is working as described
According to the Tomcat 6 docs, put log4j.jar and log4j.properties into WEB-INF/lib and WEB-INF/classes of your web application.
If that doesn't resolve the issue, please edit your question and include a copy of your log4j.properties file.
Hello I have a webapp in Tomcat server on linux
I need to be able to tell it in what order to load the jars located in WEB-INF/bin.
I tried to set it up in the META-INF/MANIFEST.MF under Class-Path but it didn't work, and I've searched the web for a solution and did not find any.
(My project is not inside a war file)
(The solution to chane the jar filename to "aaaaaaaRealName.jar" is unacceptable)
(I'm using Tomcat 7 (I think its v7.0.27))
Ido
The tomcat loads jars in the following order (extract from here: http://tomcat.apache.org/tomcat-8.0-doc/class-loader-howto.html):
bootstrap (JRE/lib/ext folder)
system (can't used to add yours)
webapp libraries (first WEB-INF/classes, then WEB-INF/lib)
common libraries (check the common.loader property , then tomcat instance/lib, then common tomcat folder/lib)
So if you want to specify the order, just put the jars in the right place.
I am running Websphere Liberty 8.5. My application reads some files from the file system that are obtained via the Java CLASSPATH and I can't seem to find a way to append a directory from the file system to the CLASSPATH Liberty reads from. I've tried manually changing JAVA_CMD and JAVA_DEBUG but none of them take.
Does anyone know how this is possible?
It is not possible to modify the JVM application classpath (normally specified via the -classpath JVM argument or the CLASSPATH environment variable). I would recommend using a <library> to give your applications visibility to the resources. If you need a directory, you should use <library><folder dir="..."/></library>.
(As an aside, modifying the wlp/bin scripts or setting those "internal" script variables is not a supported external.)
ANOTHER WARNING! It only reads JAR files! I had the same issue with endless frustration. Even if you use the <library><folder dir="..."/></library> method above, it will only read jar files from the classpath. I had a bunch of properties files that I read from the classpath in JBOSS, WebSphere (full), and Glassfish, but the same method doesn't work for Websphere Liberty. What I ended up doing is the above, and putting my properties files all in a .jar file. Very annoying but a work-around, in case you need to read non-jar files.
I'm looking to upgrade our tomcat instance from 5.5.27 to 6.0.32 and am having some issues with the logging from jars in the shared/lib directory (I've recreated this directory in tomcat 6).
We have a jar file that we create as part of our build process with some common code in it and under tomcat 5 this lived under shared/lib. When we wrote log statements from code in this jar file, they were written to the log file of the web application calling the jar at that moment. Each of our webapps has a log4j.properites and log4j.jar in it's WEB-INF/lib directory, and there was also a log4j.jar in shared/lib, but not log4j/properties.
We're using log4j and obtain references to logs as follows:
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class MyClass {
private final static Log CLASS_LOG = LogFactory.getLog(MyClass.class);
}
But when I move the exact same configuration of our jars, webapps, log4.properties and log4j.jars into tomcat 6, the logging statements from our common jar just go to catalina.log.
I've done a fair amount of reading (including http://www.mulesoft.com/tomcat-classpath, and the classloader and logging docs on the tomcat website) and mostly I can't work out how it worked the way it did in tomcat 5! (I didn't set it up, it's something I've inherited that until now has 'just worked').
Does anyone have any similar experience of trying to get logs from shared libs into webapp log files? I'm thinking that putting the shared jar in each webapp's WEB-INF/lib directory would sort this but then I'd end up with many copies of the same thing.
Check out tomcat/conf/catalina.properties in tomcat6/7. They deprecated shared and server paths, only the common path is explicitly configured (e.g. tomcat6/lib is common). You can re-enable shared and my guess is that you'd get a similar experience as tomcat 5.x.
However, you said you're putting logging.properties in WEB-INF/lib I don't think that's right, as that should only contain jar files. It should go in WEB-INF/classes/
Also, at this point in time, stop using commons-logging. Instead Code to slf4j, as it has fewer tomcat issues. Also if you use logback instead of log4j, it avoids even having to use an adaptor (e.g. your slf4j calls are actually direct logback calls). Uses a different config-file (but there are online log4j.property converters).
If you're using Logback, http://logback.qos.ch/manual/contextSelector.html may do what you want.
I don't know exactly how tomcat's classloader is configured. With our websphere application server we had the same issue and had to reconfigure the classloader to load "parent last".
This means that the searches for configuration files and libs first in the application folders and only after he doesn't find the required resources he loads the configuration of the webserver.
Maybe thats the case and the tomcat 5.x was configured that way.
In Tomcat I want to use a jar inside a web application. The jar file will exist outside of the Tomcat directory.
To include the jar file in tomcat classpath, I modified the TomcatHome/conf/catalina.properties to include the absolute path of my jar file like,
shared.loader=D:\jaa\MyJarFile.jar
as per the suggestion given in link,
http://www.mulesoft.com/tomcat-classpath
But it throws the error,
java.lang.NoClassDefFoundError
I have also tried ,
shared.loader=D:\jaa\*.jar
shared.loader=file:\\D:\jaa\MyJarFile.jar
None of them seem to work :(
If I try placing the jar inside tomcat/lib it seem to work. But I am not allowed to do that.
Please help me out with this issue as I have implementation the next week..
I figured myself how to add the classpath for tomcat. Instead of editing catalina.properties, just create a "setenv.sh" in the Tomcat Bin directory with the classpath,
Example,
CLASSPATH=D:\jaa\MyJarFile.jar
I just checked the catalina.sh in Tomcat/bin and these classpath variable will be set while setting the bootstrap as the classpath.
I was using IntelliJ and I tried everything like:
Using CtrlAltShiftS (Project Settings) and adding a dependency of mysql-connector.jar. Didn't work. (The only thing that worked was that code completion inside IntelliJ was working fine.
Adding mysql-connector.jar to apache-home/lib/ folder. Didn't work.
Including mysql-connector.jar from Maven inside IntelliJ. Just didn't work.
The thing that worked for me:
Include the mysql-connector.jar file in the PROJECT/web/WEB-INF/lib folder.
No need to add it as a dependency anywhere. Just compile this and it will work fine.
I find myself copying extra JARs, which should be available for all contexts and should therefor go into the root loader, into the following directory:
C:\Program Files\Apache Software Foundation\Tomcat 9.0\lib
But this only possible if you have access to this directory, which might not be the case for all ISPs. At least you can do it at home.
Bye