Java – SLF4J and LogBack configuration in GWT (Eclipse and Jetty)

javajettylog4jlogbackslf4j

I've made an app init function that I'm using both in Java and GWT applications. I have external logback.xml file that I'm setting the path to the "logback.configurationFile" System property. In pure Java projects, all works as expected, but not in GWT projects.

I've implemented my ServletContextListener and in method contextInitialized I'm setting the System property. Logback does not read it, but falls back to basic (red letters in console).

So, I tried to follow instructions from logback configuration

LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();

to reconfigure Logback, but that throws

java.lang.ClassCastException: org.slf4j.impl.SimpleLoggerFactory cannot be cast to ch.qos.logback.classic.LoggerContext

I also tried to put logback.xml in folders: src, war, war/WEB_INF, but it doesn't read it.

I'm switching to slf4j because previous log4j started to throw many "class not found" exceptions (something with commons-logging)

The question is:

  1. What is wrong?

    or

  2. How can I get sfl4j (logback) to read the external configuration XML file?

    or

  3. How can I get sfl4j (logback) to read any configuration XML file?

Help appreciated

EDIT: Tried to use log4j adapter with slf4j, and it doesn't work either.

EDIT2: I reverted back to pure log4j that didn't work before. However, I added log4j.jar directly in "Installed JRE" in Eclipse in the main system JRE and now the pure log4j works. What seems to me is that there is quite a difference between the OpenJDK and the Sun's JDK, and that is causing problems. I'll try to fix this slf4j issue in a few days. Maybe there is also a need for some jar on some weird place.

EDIT3: slf4j now works with log4j, but I have to manually configure it. Doesn't matter where I put log4j.xml, it doesn't read it. Looks like classloader problem with Sun's JDK. I'll try with Logback soon. It might be similar problem.

Best Answer

I have logback working in my gwt,eclipse, jetty projects. works quite well. Kind of worked out of the box. I am using maven. What I did is: add slf4j-api and logback as a dependencies.

        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.6.4</version>
        </dependency>
            <dependency>
           <groupId>ch.qos.logback</groupId>
           <artifactId>logback-classic</artifactId>
           <version>1.0.0</version>
       </dependency>

in my code I get a logger by using:

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
protected Logger log = LoggerFactory.getLogger(this.getClass());

The logback.xml is in my src/main/resources folder. This way it will be placed in target/myproject/WEB-INF/classes after beeing compiled.

Hope this will help you.

Related Topic