7 Replies Latest reply on Feb 3, 2012 2:18 PM by Peter Johnson

    How to set up log4j with Jboss to print out thread ids?

    andres bohorquez Newbie

      Good morning guys,

      I have an application running on JBoss 4.2.3 GA.

      I want log4j to print out thread ids.

       

      I have configured log4j in this way:

       

      <appender name="FILE_APPENDER" class="org.apache.log4j.RollingFileAppender">

                          <param name="File" value="${jboss.server.log.dir}/svr/svr.log"/>

                     <param name="Append" value="true" />

                          <param name="MaxFileSize" value="3000KB" />

                          <param name="MaxBackupIndex" value="100"/>

                          <layout class="org.apache.log4j.PatternLayout">

                                    <param name="ConversionPattern" value="[Date: %d{dd MMM yy HH:mm:ss}] [Level: %p] [Thread: %t %l] %m%n"/>

                          </layout>

      </appender>

       

      This prints out lines like the one below:

       

      2012-01-31 14:13:14,255 INFO  [STDOUT] [Date: 2012-01-31 14:13:14,254] [Thread: http-0.0.0.0-8081-2 - conciliacion.novedades.ConciliacionEJBBean.obtenerConciliaciones(ConciliacionEJBBean.java:1697)] [Level: WARN] XXX  OBTENER CONCILIACIONES

       

      But, I think that this id is not unique and it is used afterwards for other threads.

       

      Could you hhelp to figure out this question?

       

      Thanks in advance.

        • 1. Re: How to set up log4j with Jboss to print out thread ids?
          Wolf-Dieter Fink Master

          Why do you think the ID is not unique?

           

          The thread id is unique for that thread, but the thread is pooled and after a request is done the thread goes back to pool and might be used imediately for an other request.

          But during the thread is in procedure there will no other work done with this thread.

          • 2. Re: How to set up log4j with Jboss to print out thread ids?
            andres bohorquez Newbie

            I have an issue because I am testing a process which is performed by a stateful bean with session scope.

            It seems like the trace of the process is broken in some point. The process should, among other things, create some files which are not being created.

             

            - This is the normal log of the process, made in a test environment with just one thread in the server:

             

            [Date: 31 ene 12 17:26:32] [Level: INFO] [Thread: http-127.0.0.1-8080-2 conciliacion.novedades.ConciliacionEJBBean.obtenerTotalDiferencia(ConciliacionEJBBean.java:2427)] XXX TOTAL DIFERENCIA :-314659.00

             

            2012-01-31 17:28:20,046 INFO  [STDOUT] [Date: 2012-01-31 17:28:20,046] [Thread: http-127.0.0.1-8080-2 -conciliacion.novedades.ConciliacionEJBBean.validarFinConciliacion(ConciliacionEJBBean.java:1984)] [Level: WARN] XXX VALIDAR FIN CONCILIACION

             

             

            - This is the log of the process in a server with some concurrent threads:

             

            2012-01-31 13:59:54,963 INFO  [STDOUT] [Date: 2012-01-31 13:59:54,963] [Thread: http-0.0.0.0-8081-3 -conciliacion.novedades.ConciliacionEJBBean.obtenerTotalDiferencia(ConciliacionEJBBean.java:2351)] [Level: INFO] XXX TOTAL DIFERENCIA :-314659.00

             

            2012-01-31 14:00:32,162 INFO  [STDOUT] [Date: 2012-01-31 14:00:32,161] [Thread: http-0.0.0.0-8081-3 - conciliacion.novedades.ConciliacionEJBBean.consultarNovedadesAConciliar(ConciliacionEJBBean.java:1258)] [Level: WARN] XXX Periodo con carateristicas especiales

             

            THIS LAST LINE IS DEFINITELY BELONGS TO THE SAME PROCESS BUT IT SHOULD BE WRITTEN BEFORE THE LINE BEFORE.

            So, this line is from the same process but of another thread which is calling the same method.

             

             

            Anyway, I do not know why the thread Id is the same?

            How can I identify in a unique way the different sessions in Jboss which are using this bean and its methods?

             

             

            Thanks a lot for your help.

             


            • 3. Re: How to set up log4j with Jboss to print out thread ids?
              Wolf-Dieter Fink Master

              I'm not 100% sure but,

              I suppose that you call the SFSB method that logs 'XXX TOTAL DIFERENCIA :-314659.00' and another that logs 'XXX Periodo con carateristicas especiales'.

              That mean after calling a method the SFSB1 is passive and the thread returned to pool, if SFSB2 is called (or created) the same thread is taken from pool and proceed the request.

               

              This is nothing againts the spec because it is in responsibility of the container to manage threads (and it can be different for version or appservers)

              So I would recommend to use an ID for the SFSB and log this to identify it.

               

               

              andres bohorquez schrieb:

               

              The process should, among other things, create some files which are not being created.

              Do you mean creating a file using java.io classes? Just to notice, this will break the contract, EJBs are not allowed to use java.io or thread handling.

              It might work in special cases but you can not ensure it, e.g. in case of clustering it might be a different physical filesystem.

              • 4. Re: How to set up log4j with Jboss to print out thread ids?
                andres bohorquez Newbie

                Yes, You are right.

                But, Why the SFSB 1 did not continue with the rest of the process? No exceptions were logged and the rest of the trace dissapear from logs.

                 

                Could you recommend me a way to generate ids for SFSBs? in a unique way.

                 

                Thanks a lot for your time.

                • 5. Re: How to set up log4j with Jboss to print out thread ids?
                  Peter Johnson Master

                  Unique bean ID? Here's what I do. Each bean has this code:

                   

                    private static AtomicInteger atom = new AtomicInteger();

                    private int beanId;

                    public MyEJB() {

                      beanId = atom.addAndGet(1);

                    }

                   

                  Then i print the beanId in every log message. I use the same in all beans, whether stateful or not.

                  • 6. Re: How to set up log4j with Jboss to print out thread ids?
                    andres bohorquez Newbie

                    I tried this solution but my applicaction uses Seam and I got this exception:

                     

                    javax.servlet.ServletException: Could not instantiate Seam component: conciliacion

                     

                    This is my EJB contructor:

                     

                    public ConciliacionEJBBean() {

                                        super();

                                        beanId = atom.addAndGet(1);

                                        log.warn("XXX --------- BEANiD: " + beanId);

                              }

                     

                    How can avoid this issue?

                     

                    Thanks.

                    • 7. Re: How to set up log4j with Jboss to print out thread ids?
                      Peter Johnson Master

                      It has always worked for my EJBs, but then I'm not using Seam. Too bad that is all of the error message because it doesn't help very much, does it?