Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use SLF4J for my Java EE web application deployed on GlassFish 3 with Hibernate JPA?

Is it possible to use SLF4J in a specific GlassFish application and configure SLF4J solely for that application? We are currently struggling with the fact that GlassFish itself GlassFish with Hibernate installed contains the SLF4J API and a logger binding in its lib directory.

Our team is developing a Java EE 6 web application. For development the application is deployed on GlassFish 3.1.2, it should be as portable as possible though.

We decided to use SLF4J to log application messages. Depending on the message level the application has to handle certain log messages differently and thus must be able to configure the logging backend on its own -- as opposed to configuring logging in the application server.

Unfortunately GlassFish 3.1.2 includes slf4j-api-1.5.8.jar and slf4j-jdk14-1.5.8.jar in its lib folder. This prevents our application from using slf4j-api-1.7.5.jar and slf4j-simple-1.7.5.jar (the latter will be replaced by another binding eventually).

Is there any way to tell GlassFish to consider the application lib's before considering its own (so that the correct version of the API is used) and any way to make sure that the application provided SLF4J binding is used?

We are using Eclipse for development but we are not using Maven. Most tweaks/workarounds I found so far refer to configuration in Maven which we cannot use. If you can provide any Eclipse specific tips, that would be great though. In the Eclipse project's properties I already configured the order of the build path so that the Web App Libraries are on top. But somehow this doesn't seem to be enough.

Many thanks for your help!

Update 1: To get it working temporarily I just removed the SLF4J libraries from the GlassFish lib directory. The application then uses its self provided SLF4J libraries. But I don't consider this a solution for the deployment process, I'm not sure if this might break anything used by GlassFish (why would the libraries reside there anyway?), and this is not a practical solution for our development team.

Update 2: Actually it is not GlassFish which includes the interfering SLF4J library out-of-the-box but the Hibernate JPA package I installed via the Update Tool. We currently do not need Hibernate JPA but the requirement to change to Hibernate might arise at some point and we'd also like to keep the application easily portable to application servers with Hibernate JPA or other SLF4J-containing packages installed. I updated the question title accordingly. How can this general issue of interfering dependencies of application server and application be solved?

like image 971
cyan902 Avatar asked Nov 13 '22 05:11

cyan902


1 Answers

This is due to the 'Class Loader Delegation', try to change it so that it takes the classes from your application over the classes from the parent.

like image 168
jgeerts Avatar answered Nov 15 '22 12:11

jgeerts