logback with EJB3.1

前端 未结 4 402
陌清茗
陌清茗 2020-12-17 01:56

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

4条回答
  •  孤城傲影
    2020-12-17 02:37

    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 a ClassLoader to do so), it may fail at picking up its configuration file and fall back on a default BasicConfiguration.

    Not sure how you package your code but the suggested workaround is to include the logback.xml in a EAR lib. If you aren't using an EAR packaging, try to identify the class loader used to see where to put the logback.xml file.

    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 the sun-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.jsp
    

    My pom.xml looks like:

    
    
      4.0.0
      com.stackoverflow.q2418355
      sample
      1.0-SNAPSHOT
      war
      sample Maven Webapp
      http://maven.apache.org
      
        
          javax
          javaee-api
          6.0
          provided
        
        
          junit
          junit
          4.7
          test
        
        
          ch.qos.logback
          logback-classic
          0.9.18
        
        
          org.slf4j
          slf4j-api
          1.5.11
        
      
      
        
          
            org.apache.maven.plugins
            maven-compiler-plugin
            2.0.2
            
              1.6
              1.6
            
          
          
            org.apache.maven.plugins
            maven-war-plugin
            2.1-beta-1
            
              false
            
          
        
        sample
      
    
    

    The code for SimpleEJB.java is:

    package com.stackoverflow.q2418355;
    
    import javax.ejb.Stateless;
    
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    
    @Stateless
    public class SimpleEJB {
        private static Logger logger = LoggerFactory.getLogger(SimpleEJB.class);
    
        public String sayHello(String name) {
            logger.debug(">> sayHello()");
            logger.debug("<< sayHello()");
            return "Hello " + name + "!!!";
        }
    }
    

    The code for SimpleServlet.java is:

    package com.stackoverflow.q2418355;
    
    import java.io.IOException;
    import java.io.PrintWriter;
    
    import javax.ejb.EJB;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    @WebServlet(urlPatterns = { "/SimpleServlet" })
    public class SimpleServlet extends HttpServlet {
        @EJB
        SimpleEJB bean;
    
        private static Logger logger = LoggerFactory.getLogger(SimpleServlet.class);
    
        @Override
        public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
            logger.debug(">> doGet()");
            PrintWriter out = response.getWriter();
            out.println("");
            out.println("

    Serving at: " + request.getContextPath() + "

    "); out.println("

    Invoking EJB: " + bean.sayHello("Duke") + "

    "); out.println(""); logger.debug("<< doGet()"); } }

    The code for index.jsp is:

    
    
    

    Hello World!

    Invoke the Servlet by clicking here.

    And my logback.xml looks like:

    
      
        
          %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
        
      
    
      
        /tmp/logs/testFile.log
        true
    
        
          %-4relative [%thread] %-5level %logger{35} - %msg%n
        
      
    
      
    
      
        
        
      
    
    

    My logback.xml gets propertly loaded and I get the following trace (taken from my log file) when invoking the servlet:

    10913 [http-thread-pool-8080-(1)] DEBUG com.stackoverflow.q2418355.SimpleServlet - >> doGet()
    10928 [http-thread-pool-8080-(1)] DEBUG com.stackoverflow.q2418355.SimpleEJB - >> sayHello()
    10928 [http-thread-pool-8080-(1)] DEBUG com.stackoverflow.q2418355.SimpleEJB - << sayHello()
    10932 [http-thread-pool-8080-(1)] DEBUG com.stackoverflow.q2418355.SimpleServlet - << doGet()
    

    I did also try with the EJB packaged in its own JAR and deployed in WEB-INF/lib and 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).

提交回复
热议问题