12 Replies Latest reply on Aug 23, 2006 10:54 AM by Gavin King

    When can @Logger be used?

    Kurt Edegger Newbie

      Hi,

      We've just started to work with Seam by extending the examples provided in the distribution.
      We understood that Seam is providing a logger build on top apache.commons.logging.
      As described in the user documentation it can be used by putting @Logger private Log log; into the bean code.
      We've been trying this with a regular Java bean but the instance variable remains NULL all the time.
      Our question is, when is it possible to use this logger? Can this one only be used with (stateful/stateless) session beans or only with entity beans? We've even tried to add the Seam interceptor (@Interceptors(SeamInterceptor.class)) to our class, but the logger is still uninitialized.

      Could anyone explain the issue?

      Thanks in advance,

      Kurt

        • 1. Re: When can @Logger be used?
          Raja Master

          Is your bean annotated with a "@Name" ? Also have you configured ur bean so that it shouldnt be intercepted?
          Can you post the annotations for your bean?

          • 2. Re: When can @Logger be used?
            Carsten Hoehne Master

            Hello,
            i am using this facility successfully with javabeans.
            Do you try to use a log statement in a constructor?
            This will not function. The bean is first constructed and then injection take place. So you can not use the logger in a constructor.
            If you need it, you have 2 possibilities:
            1. use the old method
            2. use the logger in a method annotated with @Create
            Ciao,
            Carsten

            • 3. Re: When can @Logger be used?
              Raja Master

               

              "raja05" wrote:
              IAlso have you configured ur bean so that it shouldnt be intercepted?

              That should read " Have you configured your bean so that it should be intercepted". Not having any interceptor tags should be enough though.

              • 4. Re: When can @Logger be used?
                Kurt Edegger Newbie

                Thank you for your replies!

                I've tried several annotations. The "current" one is:

                @Name("user1A6")
                @Interceptors(SeamInterceptor.class)
                public class User1A6 {
                

                But I've tried it w/o the interceptor as well.
                I'm trying to log the properties which are injected to the bean from an JSF page.

                Regards, Kurt

                • 5. Re: When can @Logger be used?
                  Carsten Hoehne Master

                  Hello,
                  would you be so kind to post the code of your bean for which you use the Logger annotation.
                  Maybe, the stacktrace of the NPE will help also. There must be one, if your logger is uninitialized.

                  Ciao,
                  Carsten

                  • 6. Re: When can @Logger be used?
                    Kurt Edegger Newbie

                    Ok, I managed to get the @Logger working in the simple bean:

                    @Name("user1A6")
                    public class User1A6 {
                    
                     private String username;
                     private String password;
                    
                     @Logger private org.jboss.seam.log.Log log_seam;
                    
                     public User1A6() {
                     System.out.println("===== User1A6 constructor");
                     }
                    
                     public String getPassword() {
                     System.out.println("===== User1A6.getPassword called");
                     log_seam.info("===== User1A6.getPassword called");
                     return password;
                     }
                    
                     public void setPassword(String password) {
                     System.out.println("===== User1A6.setPassword called: "+password);
                     log_seam.info("===== User1A6.setPassword called: "+password);
                     this.password = password;
                     }
                    
                     public String getUsername() {
                     System.out.println("===== User1A6.getUsername called");
                     log_seam.info("===== User1A6.getUsername called");
                     return username;
                     }
                    
                     public void setUsername(String username) {
                     System.out.println("===== User1A6.setUsername2 called: "+username);
                     log_seam.info("===== User1A6.setUsername called: "+username);
                     this.username = username;
                     }
                    
                     public void frontendLog()
                     {
                     log_seam.debug( "User1A6 has been called from the frontend - @Logger" );
                     }
                    


                    The logger is accessible except in the constructor, which makes sense after all ;)
                    BUT if the bean is a entity-bean, I'm getting exceptions when I'm using the logger again!
                    Here's the code from a seam-example:

                    @Entity
                    @Name("user")
                    @Scope(SESSION)
                    @Table(name = "users")
                    public class User implements Serializable {
                    
                     @Logger private org.jboss.seam.log.Log log;
                    
                     private String username;
                     private String password;
                     private String name;
                    
                     public User(String name, String password, String username) {
                     this.name = name;
                     this.password = password;
                     this.username = username;
                     }
                    
                     public User() {
                     }
                    
                     @Id
                     @NotNull
                     @Length(min = 5, max = 15)
                     public String getUsername() {
                     log.info("===== User.getUsername: ");
                     return username;
                     }
                    
                     public void setUsername(String username) {
                     log.debug("==== User.setUsername: ");
                     this.username = username;
                     }
                    
                     @NotNull
                     @Length(min=5, max=15)
                     public String getPassword() {
                     log.info("===== User.getPassword: " );
                     return password;
                     }
                    
                     public void setPassword(String password) {
                     log.debug("==== User.setPassword: ");
                     this.password = password;
                     }
                    
                    // ... some more getter/setter and methods
                    
                    


                    When the page refering to the user is called, I'll get a NullPointer Exception:

                    javax.faces.el.EvaluationException: /home.xhtml @19,113 value="#{user.username}": org.jboss.seam.example.registration.User
                     at com.sun.facelets.el.LegacyValueBinding.getValue(LegacyValueBinding.java:60)
                     at javax.faces.component.UIOutput.getValue(UIOutput.java:75)
                     at org.apache.myfaces.renderkit.RendererUtils.getStringValue(RendererUtils.java:225)
                     at org.apache.myfaces.renderkit.html.HtmlTextRendererBase.renderInput(HtmlTextRendererBase.java:131)
                     at org.apache.myfaces.renderkit.html.HtmlTextRendererBase.encodeEnd(HtmlTextRendererBase.java:49)
                     at javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:331)
                     at com.sun.facelets.tag.jsf.ComponentSupport.encodeRecursive(ComponentSupport.java:242)
                     at com.sun.facelets.tag.jsf.ComponentSupport.encodeRecursive(ComponentSupport.java:239)
                     at com.sun.facelets.tag.jsf.ComponentSupport.encodeRecursive(ComponentSupport.java:239)
                     at com.sun.facelets.FaceletViewHandler.renderView(FaceletViewHandler.java:554)
                     at org.apache.myfaces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:352)
                     at javax.faces.webapp.FacesServlet.service(FacesServlet.java:107)
                     at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
                     at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
                     at org.jboss.seam.servlet.SeamRedirectFilter.doFilter(SeamRedirectFilter.java:30)
                     at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
                     at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
                     at org.jboss.seam.servlet.SeamExceptionFilter.doFilter(SeamExceptionFilter.java:45)
                     at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
                     at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
                     at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
                     at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
                     at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
                     at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
                     at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
                     at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:175)
                     at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:74)
                     at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
                     at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
                     at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
                     at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
                     at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:869)
                     at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:664)
                     at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
                     at org.apache.tomcat.util.net.MasterSlaveWorkerThread.run(MasterSlaveWorkerThread.java:112)
                     at java.lang.Thread.run(Thread.java:595)
                    Caused by: javax.faces.el.EvaluationException: Bean: org.jboss.seam.example.registration.User, property: username
                     at org.apache.myfaces.el.PropertyResolverImpl.getProperty(PropertyResolverImpl.java:404)
                     at org.apache.myfaces.el.PropertyResolverImpl.getValue(PropertyResolverImpl.java:71)
                     at com.sun.facelets.el.LegacyELContext$LegacyELResolver.getValue(LegacyELContext.java:141)
                     at com.sun.el.parser.AstValue.getValue(AstValue.java:117)
                     at com.sun.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:192)
                     at com.sun.facelets.el.TagValueExpression.getValue(TagValueExpression.java:71)
                     at com.sun.facelets.el.LegacyValueBinding.getValue(LegacyValueBinding.java:56)
                     ... 35 more
                    Caused by: java.lang.reflect.InvocationTargetException
                     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
                     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
                     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
                     at java.lang.reflect.Method.invoke(Method.java:585)
                     at org.apache.myfaces.el.PropertyResolverImpl.getProperty(PropertyResolverImpl.java:400)
                     ... 41 more
                    Caused by: java.lang.NullPointerException
                     at org.jboss.seam.example.registration.User.getUsername(Unknown Source)
                     ... 46 more
                    


                    The logger should work in this case as well, right?
                    If I replace the seam logger with the one from apache.commons.logging everything's great!

                    Sorry for the long post and I'll appreciate your help!

                    Thanks, Kurt

                    • 7. Re: When can @Logger be used?
                      Carsten Hoehne Master

                      Hello,
                      good to here that it is working.


                      The logger should work in this case as well, right?

                      For me the logger works for entity beans (but they are all plain pojos)
                      My environment is tomcat with hibernate setup in the microcontainer(derived from the seam, hibernate example)
                      So i do not know how logger behaves with a real entity bean in an ejb3 container. sorry.
                      Ciao,
                      Carsten

                      • 8. Re: When can @Logger be used?
                        Raja Master

                        Kurt,
                        In your ejb jar, do you have a ejb-jar.xml that has the information about intercepting all calls to the SeamInterceptor? In the case of a JavaBean, the SeamInterceptor is setup for you by Seam , so you dont do any extra plumbing, but for EJBs, you would need to indicate to Seam to pass those through SeamInterceptor.
                        If your ejb.jar file contains a ejb-jar.xml with the following entries, it should work

                        <ejb-jar>
                         <assembly-descriptor>
                         <interceptor-binding>
                         <ejb-name>*</ejb-name>
                         <interceptor-class>org.jboss.seam.ejb.SeamInterceptor</interceptor-class>
                         </interceptor-binding>
                         </assembly-descriptor>
                        </ejb-jar>
                        


                        • 10. Re: When can @Logger be used?
                          Raja Master

                          Thanks Petemuir, that was helpful information.

                          • 11. Re: When can @Logger be used?
                            Kurt Edegger Newbie

                            First of all, thank you all for the info! You have been really helpful.

                            Are there any more advantages except the shorter syntax of @Logger over apache.commons.Log? I still feel like using the latter aproach, since calls to the constructor and entity bean can be logged as well.

                            Regards, Kurt

                            • 12. Re: When can @Logger be used?
                              Gavin King Master

                               

                              "appendix" wrote:
                              First of all, thank you all for the info! You have been really helpful.

                              Are there any more advantages except the shorter syntax of @Logger over apache.commons.Log? I still feel like using the latter aproach, since calls to the constructor and entity bean can be logged as well.

                              Regards, Kurt


                              The other advantage is that you get EL interpolation.