Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: Logging by AOP doesnot work

  1. #1
    Join Date
    Jun 2007
    Posts
    5

    Default Logging by AOP doesnot work

    I have an application which does logging by LOG4j currently. Recently I tried using Spring AOP concept to do all the logging but it is not logging the details whenever it enters/exits any method. I am able to see the default Spring logs in the log file, but not my logs. Here is my code

    for applicationContext.xml

    <bean id="input_adapter"
    class="org.springframework.aop.framework.ProxyFact oryBean">
    <property name="proxyInterfaces">
    <value>com.xyz.transaction.util.LoggingMarkerIfc </value>
    </property>
    <property name="target">
    <ref bean="loginputadapter"/>
    </property>
    <property name="interceptorNames">
    <list>
    <value>loggingInterceptor</value>
    </list>
    </property>
    </bean>

    <bean id="loginputadapter"
    class="com.xyz.transaction.adapters.LogInputAdapte r"/>
    <bean id="loggingInterceptor"
    class="com.xyz.transaction.util.LoggingInterceptor "/>



    for the logger class.


    public class LoggingInterceptor implements MethodBeforeAdvice, AfterReturningAdvice, ThrowsAdvice {

    private static Log log = null;

    /* public LoggingInterceptor() {
    }
    */
    public void before(Method arg0, Object[] arg1, Object arg2)
    throws Throwable {
    log =LogFactory.getLog(arg2.getClass());
    log.info("Beginning method: " + arg0.getName());
    System.out.println("This is before");
    }

    public void afterReturning(Object arg0, Method arg1, Object[] arg2,
    Object arg3) throws Throwable {
    log = LogFactory.getLog(arg3.getClass());
    System.out.println("This is after");
    log.info("Ending method: " + arg1.getName());
    }

    public void afterThrowing(Method m, Object[] args, Object target,
    Throwable ex) {
    log = LogFactory.getLog(target.getClass());
    log.info("Exception in method: " + m.getName() + " Exception is: "
    + ex.getMessage());
    }


    Please help me out if this has anything wrong in it.

    Thanks in anticipation

  2. #2
    Join Date
    Dec 2006
    Posts
    3

    Default

    Hy,

    Have you configured log4j correclty so that it shows the debug logs of your classes ?

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

    Default

    It really helps if you wrap your code in [code] [ /code] tags! Is it possible to see the TestCase you're driving this from?

  4. #4
    Join Date
    Dec 2005
    Posts
    269

    Default

    1st, I would use a single Around advice instead of 3
    2nd, are you sure, that log4j.jar is in your app's classpath?

  5. #5
    Join Date
    Jun 2007
    Posts
    5

    Default

    Quote Originally Posted by karldmoore View Post
    It really helps if you wrap your code in [code] [ /code] tags! Is it possible to see the TestCase you're driving this from?
    Hi,
    Regarding my Log4J.jar it is already set in the classpath. Below is my Log4J.properties where the patterns and the properties are set.


    log4j.appender.R=org.apache.log4j.RollingFileAppen der
    log4j.appender.R.layout=org.apache.log4j.PatternLa yout
    log4j.appender.R.layout.ConversionPattern= %d %-5p [%c] %m%n
    log4j.appender.R.File=..\/TransactionHub\/logs\/itg.log
    log4j.appender.R.MaxFileSize=5000KB
    log4j.appender.R.MaxBackupIndex=100

    log4j.rootLogger=DEBUG, R

    log4j.category.org.springframework=DEBUG, R
    #log4j.category.org.springframework=DEBUG, R
    log4j.category.org.hibernate=WARN, R
    log4j.category.net.sf.acegisecurity=WARN, R
    log4j.category.com.siemens=WARN, R
    log4j.category.org.apache=WARN, R

    Thanks

  6. #6
    Join Date
    Mar 2007
    Posts
    515

    Default

    If you see all 'System.out.println' messages, then it is not a Spring AOP issue. If you don't then, as karldmoore said, it will be helpful if you could post your test case... I assume you're not using proxied instance (ie input_adapter bean).
    Last edited by Andrei Stefan; Jun 11th, 2007 at 05:55 AM.

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

    Default

    In response to your PM, what I meant by TestCase was simply the class with the main method in it or the actual JUnit test you are using here. Basically just want to see the complete code for what you are trying to do.

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

    Default

    This example might also be useful.
    http://www.devx.com/Java/Article/30799/0/page/1

  9. #9
    Join Date
    Jun 2007
    Posts
    5

    Default the test case code

    Quote Originally Posted by karldmoore View Post
    In response to your PM, what I meant by TestCase was simply the class with the main method in it or the actual JUnit test you are using here. Basically just want to see the complete code for what you are trying to do.

    This is the test case. What I guess is once any method in the below class is
    found then the logs are generated and appended to the log file.

    Here is the below code.

    public class LogInputAdapter implements JMSConstants, Runnable
    {

    private JmsTemplate jmsTemplate;

    Destination inputQueue;
    Logger logger = Logger.getLogger(ORPOSLogInputAdapter.class);

    public LogInputAdapter()
    {

    }

    public LogInputAdapter(ApplicationContext ctx)
    {
    initialize(ctx);
    }

    public void initialize(ApplicationContext ctx)
    {
    inputQueue = (Destination) ctx.getBean(INPUT_QUEUE_NAME);
    // get the JmsTemplate
    jmsTemplate = (JmsTemplate) ctx.getBean(JMS_TEMPLATE_BEAN_ID);
    }

    public void run()
    {
    while (true)
    {
    logger.debug("listening on queue");
    String poslog = "";
    try
    {
    soaplog = LogConnector.getSoapLogMessage();
    if (poslog != null)
    {
    ArrayList transactionsList = ORLogMapper.parseSoapLog(soaplog);
    sendToDispatcher(transactionsList);
    }
    }
    catch (Exception e)
    {
    // TODO: handle exception properly
    e.printStackTrace();
    }
    }
    }

    private void sendToDispatcher(ArrayList transactions)
    {
    MessageCreator mc = new DispatcherMessageCreator(transactions);
    jmsTemplate.send(inputQueue, mc);
    }

    private class DispatcherMessageCreator implements MessageCreator
    {

    private ArrayList transactions;

    public DispatcherMessageCreator(ArrayList transactions)
    {
    this.transactions = transactions;
    }

    public Message createMessage(Session session) throws JMSException
    {
    return session.createObjectMessage(transactions);
    }

    }

    }

  10. #10
    Join Date
    Jun 2007
    Posts
    5

    Default the test case code

    Quote Originally Posted by karldmoore View Post
    In response to your PM, what I meant by TestCase was simply the class with the main method in it or the actual JUnit test you are using here. Basically just want to see the complete code for what you are trying to do.

    This is the test case. What I guess is once any method in the below class is
    found then the logs are generated and appended to the log file.

    Here is the below code.

    public class LogInputAdapter implements JMSConstants, Runnable
    {

    private JmsTemplate jmsTemplate;

    Destination inputQueue;
    Logger logger = Logger.getLogger(ORPOSLogInputAdapter.class);

    public LogInputAdapter()
    {

    }

    public LogInputAdapter(ApplicationContext ctx)
    {
    initialize(ctx);
    }

    public void initialize(ApplicationContext ctx)
    {
    inputQueue = (Destination) ctx.getBean(INPUT_QUEUE_NAME);
    // get the JmsTemplate
    jmsTemplate = (JmsTemplate) ctx.getBean(JMS_TEMPLATE_BEAN_ID);
    }

    public void run()
    {
    while (true)
    {
    logger.debug("listening on queue");
    String soaplog = "";
    try
    {
    soaplog = LogConnector.getSoapLogMessage();
    if (poslog != null)
    {
    ArrayList transactionsList = ORLogMapper.parseSoapLog(soaplog);
    sendToDispatcher(transactionsList);
    }
    }
    catch (Exception e)
    {
    // TODO: handle exception properly
    e.printStackTrace();
    }
    }
    }

    private void sendToDispatcher(ArrayList transactions)
    {
    MessageCreator mc = new DispatcherMessageCreator(transactions);
    jmsTemplate.send(inputQueue, mc);
    }

    private class DispatcherMessageCreator implements MessageCreator
    {

    private ArrayList transactions;

    public DispatcherMessageCreator(ArrayList transactions)
    {
    this.transactions = transactions;
    }

    public Message createMessage(Session session) throws JMSException
    {
    return session.createObjectMessage(transactions);
    }

    }

    }

Posting Permissions

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