Results 1 to 10 of 10

Thread: Logging help

  1. #1
    Join Date
    Jun 2007
    Location
    New Jersey, US
    Posts
    8

    Default Logging help

    Hi,
    I am trying to implement logging in my project. I have used example from
    http://www.devx.com/Java/Article/30799/0/page/2 but it is not working. Application works fine and does not throw any exception. But the logging is not happenig
    I have carefully read some tutorials but I am not able to solve this problem.

    Can someone tell me where am I wrong?

    Here are my code snippets
    applicationContext.xml
    Code:
    <bean id="AbcServiceBean" class="org.springframework.aop.framework.ProxyFactoryBean">
    			              <property name="proxyInterfaces">
                              	<value>com.logic.GenericDAO</value>
                              </property>
    		<property name="target">
    			<ref bean="AbcDAO"/>
    		</property>
    		<property name="interceptorNames">
    			<list>				
    				<value>loggingInterceptor</value>
    			</list>
    		</property>		
    </bean>	
    
    <bean id="AbcDAO" class="com.logic.AbcDAO">
    	<property name="sessionFactory"><ref bean="hibernateSessionFactory" /></property>
    </bean>
    <bean id="loggingInterceptor"  class="com.web.LoggingInterceptor"/>	
    </beans>
    AbcDAO
    Code:
    public class AbcDAO extends HibernateDaoSupport implements GenericDAO {
    
    .....
    ....
    some business methods
    ....
    ....
    
    }
    GenericDAO

    Code:
    public interface GenericDAO {
    
    }
    Logging interceptor it is basically exactly the same as given in the tutorial
    Code:
    public class LoggingInterceptor implements MethodBeforeAdvice,
            AfterReturningAdvice, ThrowsAdvice {
        private static Logger log = null;
        public LoggingInterceptor(){
        }
        
        public void before(Method arg0, Object[] arg1, Object arg2) throws Throwable {
            log = Logger.getLogger(arg2.getClass());
            log.info("Beginning method: "+arg0.getName());
        }
        
        public void afterReturning(Object arg0, Method arg1, Object[] arg2, Object arg3) throws Throwable {     
            log = Logger.getLogger(arg3.getClass());
            log.info("Ending method: "+arg1.getName());     
        }
        
        public void afterThrowing(Method m, Object[] args, Object target, Throwable ex) 
        { 
            log = Logger.getLogger(target.getClass());
            log.info("Exception in method: "+m.getName()+" Exception is: "+ex.getMessage());    
        }
    
    }
    Last edited by parasjain01; Jun 15th, 2007 at 10:21 AM. Reason: [code] tags

  2. #2
    Join Date
    Sep 2004
    Location
    London, UK
    Posts
    64

    Default

    Am I right that GenericDAO interface doesn't have any method? In this case LoggingInterceptor will intercept nothing...

  3. #3
    Join Date
    Jun 2007
    Location
    New Jersey, US
    Posts
    8

    Default GenericDAO updated

    Even when I update the interface to

    Code:
    public Map enroll(AbcUser abcUser);
    it is not working.

    When the enroll method of AbcDAO is called then the logging module is not hit.

  4. #4
    Join Date
    Sep 2006
    Location
    UK
    Posts
    8,424

    Default

    Is it possible to see the code you are using to test this? There are a couple of common causes for this, #1 you are creating your bean using the new operator instead of looking it up, #2 you are looking up the wrong bean, instead of looking up the proxy e.g. 'AbcServiceBean' you are looking up 'AbcDAO'.
    Last edited by karldmoore; Aug 29th, 2007 at 11:45 AM.
    Barracuda Networks SSL VPN Lead Developer
    http://pramatr.wordpress.com
    http://twitter.com/karldmoore
    http://www.linkedin.com/in/karldmoore
    Any postings are my own opinion, and should not be attributed to my employer or clients.

  5. #5
    Join Date
    Jun 2007
    Location
    New Jersey, US
    Posts
    8

    Default Yes that may be the cause

    You guys are great. Probably here I am making some mistake. But I don't know how to correct it because I have not much experience in Spring. We use Spring just for little bit IoC etc

    I cannot put the full code here. But I am doing something like this
    Code:
    AbcDAO dao = (AbcDAO) webCtx.getBean("AbcDAO");
    I think I should do something like
    Code:
    (ProxyFactoryBean)webCtx.getBean("AbcServiceBean")
    But then how to extract AbcDao from it?

  6. #6
    Join Date
    Sep 2004
    Location
    London, UK
    Posts
    64

    Default

    Can you try
    Code:
    GenericDAO dao = (GenericDAO) webCtx.getBean("AbcServiceBean");
    System.out.println( dao.enroll( <some your user instance> ) );

  7. #7
    Join Date
    Sep 2006
    Location
    UK
    Posts
    8,424

    Default

    The code you've posted is correct, but the cast is actually to the type rather than the proxy. If you were programming to interfaces then you could simply cast to that type, if you're not then you need to set proxyTargetClass="true" on ProxyFactoryBean and cast to the concrete type.
    http://www.springframework.org/docs/...l#aop-proxying
    Last edited by karldmoore; Aug 29th, 2007 at 11:45 AM.
    Barracuda Networks SSL VPN Lead Developer
    http://pramatr.wordpress.com
    http://twitter.com/karldmoore
    http://www.linkedin.com/in/karldmoore
    Any postings are my own opinion, and should not be attributed to my employer or clients.

  8. #8
    Join Date
    Jun 2007
    Location
    New Jersey, US
    Posts
    8

    Default Its working

    Thanks a ton to karldmoore and mpetrashev

  9. #9

    Lightbulb Is the code in side one spring controller?

    Quote Originally Posted by parasjain01 View Post
    You guys are great. Probably here I am making some mistake. But I don't know how to correct it because I have not much experience in Spring. We use Spring just for little bit IoC etc

    I cannot put the full code here. But I am doing something like this
    Code:
    AbcDAO dao = (AbcDAO) webCtx.getBean("AbcDAO");
    I think I should do something like
    Code:
    (ProxyFactoryBean)webCtx.getBean("AbcServiceBean")
    But then how to extract AbcDao from it?
    Hello, I am also new in Spring AOP, I just wanna ask is the code
    Code:
    AbcDAO dao = (AbcDAO) webCtx.getBean("AbcDAO");
    and
    Code:
    (ProxyFactoryBean)webCtx.getBean("AbcServiceBean")
    in a controller in order to get the logging info for a application??? Or my question is where I should put the above two code in my spring application in order to get the logging infor?? Where I should active the logging task, how to active? by call a URL which mapped to a controller, then controller active the logging event or what?Thanks

  10. #10
    Join Date
    Sep 2006
    Location
    UK
    Posts
    8,424

    Default

    Quote Originally Posted by blust1984 View Post
    in a controller in order to get the logging info for a application??? Or my question is where I should put the above two code in my spring application in order to get the logging infor?? Where I should active the logging task, how to active? by call a URL which mapped to a controller, then controller active the logging event or what?Thanks
    I'm not exactly sure about the question. Any chance you can explain a little more about what you're not sure about?
    Last edited by karldmoore; Aug 29th, 2007 at 11:45 AM.
    Barracuda Networks SSL VPN Lead Developer
    http://pramatr.wordpress.com
    http://twitter.com/karldmoore
    http://www.linkedin.com/in/karldmoore
    Any postings are my own opinion, and should not be attributed to my employer or clients.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •