I am using logback/slf4j to handle logging in my application. Everything was working perfectly until I started using EJBs. Once I added a stateless EJB to my app, the logger started ignoring my logback.xml and stopped using my appenders. I switched to a programmatic logger configuration to see what was wrong and now I am getting the following error when I try to use my logger within the EJB:
org.slf4j.impl.JDK14LoggerFactory cannot be cast to ch.qos.logback.classic.LoggerContext
stemming from the line:
LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
Is there any special configuration necessary to get logback to work with EJBs? If it matters I am deploying on glassfish v3.
This looks very close to the problem described in this thread and I suspect a similar class loading issue. Because of the way logback loads
logback.xml(more precisely the way it retrieves aClassLoaderto do so), it may fail at picking up its configuration file and fall back on a defaultBasicConfiguration.Not sure how you package your code but the suggested workaround is to include the
logback.xmlin a EAR lib. If you aren’t using an EAR packaging, try to identify the class loader used to see where to put thelogback.xmlfile.At the end, this might be a problem in logback. Didn’t check their issue tracker though.
Update: If you use a war packaging, try to configure GlassFish to use firstly the child classloaders before to delegate. In thesun-web.xml:Update: I did a little test on my side and… I cannot reproduce your problem. I’ve created a project for a Java EE 6 webapp which has the following structure:
$ tree sample sample |-- pom.xml `-- src `-- main |-- java | `-- com | `-- stackoverflow | `-- q2418355 | |-- SimpleEJB.java | `-- SimpleServlet.java |-- resources | `-- logback.xml `-- webapp |-- META-INF | `-- MANIFEST.MF |-- WEB-INF | `-- lib `-- index.jspMy pom.xml looks like:
The code for
SimpleEJB.javais:The code for
SimpleServlet.javais:The code for
index.jspis:And my
logback.xmllooks like:My
logback.xmlgets propertly loaded and I get the following trace (taken from my log file) when invoking the servlet:I did also try with the EJB packaged in its own JAR and deployed in
WEB-INF/liband get the same result, it just works. Can you spot any obvious difference? Maybe upload a simplified version of your app (will very likely be required for the bug report BTW).I am running GlassFish v3 under Eclipse 3.5 (with the GlassFish v3 plugin).