Results 1 to 6 of 6

Thread: Duplicate entries (messages) in logging.

  1. #1

    Post Duplicate entries (messages) in logging.

    I have written the class "LogAspect.java":

    package fi.wmdata.hr.apps.recruiting.common.logging;

    import org.apache.commons.logging.Log;
    import org.apache.commons.logging.LogFactory;
    import org.aspectj.lang.ProceedingJoinPoint;
    import org.aspectj.lang.annotation.Around;
    import org.aspectj.lang.annotation.Aspect;

    @Aspect
    public class LogAspect {
    private static Log log = null;

    @Around("fi.wmdata.hr.apps.recruiting.common.loggi ng.LogPointcut.Around()")
    public Object log(ProceedingJoinPoint call) throws Throwable
    {
    log = LogFactory.getLog(getClass());
    System.out.println(call.toLongString());
    log.info("************** Before Executing : "+ call.getSignature().toLongString()+" with parameters: ");
    int paramLength = call.getArgs().length;
    for(int i=0; i<paramLength; i++){
    log.info(call.getArgs()[i]);
    }
    Object point = call.proceed();
    log.info("************** After method Exection: "+ call.getSignature().toLongString()+" **************");
    return point;
    }
    }


    and "LogPointcut" pointcut class:

    package fi.wmdata.hr.apps.recruiting.common.logging;

    import org.aspectj.lang.annotation.Aspect;
    import org.aspectj.lang.annotation.Pointcut;

    @Aspect
    public class LogPointcut {
    @Pointcut("execution(public * fi.wmdata.hr.apps.recruiting.um.*.*.*(..)) ||"+
    "execution(public * fi.wmdata.hr.apps.recruiting.ln.*.*.*(..))"
    )
    public void Around(){
    }
    }

    and the configuration file is as follows:

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schem...ring-beans.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop.xsd">

    <!-- Logging entries start -->
    <aop:aspectj-autoproxy/>
    <aop:config>
    <aop:aspect ref="logAspect" >
    <aopointcut id="around"
    expression="fi.wmdata.hr.apps.recruiting.common.lo gging.LogPointcut.Around()" />
    <aop:around pointcut-ref="around"
    method="log" />

    </aop:aspect>
    </aop:config>

    <bean id="logAspect" class="fi.wmdata.hr.apps.recruiting.common.logging .LogAspect" />
    <bean id="logPointcut" class="fi.wmdata.hr.apps.recruiting.common.logging .LogPointcut" />
    <!-- Logging entries end -->
    </beans>


    Problem is i am getting duplicate messages in the logging.
    Please give me some advice.

    I went through the similar one's, but i was not able to understand correctly.

    In one of the posts it is explained that this will happen when:
    "logging of the Interceptor is done instead of logging the class or package defined as a pointcut".

    I am a begineer in Spring so please even if it is a simple one also reply.

    Thanking in advance
    Mahammad Ghouse

  2. #2
    Join Date
    Sep 2007
    Location
    Oceanside, CA
    Posts
    187

    Default

    It looks like you have defined an aspect using @AspectJ annotations and also with XML configuration. You might be able to resolve the duplicate logging issue by removing the XML configuration for this aspect (keeping the version defined with @AspectJ annotations):

    Code:
    <aop:config>
      <aop:aspect ref="logAspect" >
        <aop:pointcut id="around"
          expression="fi.wmdata.hr.apps.recruiting.common.logging.LogPointcut.Around()"   />
        <aop:around pointcut-ref="around" method="log" />
      </aop:aspect>
    </aop:config>
    You'll still need to keep the following configuration:

    Code:
    <aop:aspectj-autoproxy/>
    Mike Bingham

  3. #3

    Post

    Thank you Very much Mike.

    can you explain me :

    1) When to use the xml configuration and
    2) when to use the @Aspectj.

    what is the difference?

  4. #4
    Join Date
    Sep 2007
    Location
    Oceanside, CA
    Posts
    187

    Default

    Whether to use @AspectJ annotations or XML configuration is a matter of personal preference if you're using Java 5+. @AspectJ annotations have the benefit of encapsulating the aspect (advice, pointcut definition, etc.) in one place, and provide a simpler migration path if you ever decided that you needed to leverage the AspectJ language. XML configuration is also slightly more limited than the @AspectJ approach. For example, you cannot combine named pointcuts in XML.

    The following section in the reference documentation provides more information about choosing between @AspectJ annotations and XML configuration:

    http://www.springframework.org/docs/...aspectj-or-xml
    Mike Bingham

  5. #5

    Default

    Hi,

    if we want to configure using xml then we should remove the @Aspect and @Around in the java class? will it work with these changes or do we need to do any other changes?

    Thanking you in advance

    Mahammad Ghouse G
    Programmer

  6. #6
    Join Date
    Sep 2007
    Location
    Oceanside, CA
    Posts
    187

    Default

    Yes, you would remove the @Aspect and @Around annotations from your aspect class. If you're wanting to go with a completely XML-based approach, you would also remove your Pointcut class (and all @Pointcut annotations), and modify your pointcut definition(s) in the XML file to contain the actual matching expression(s). For example:

    Code:
    <aop:pointcut id="around"
    expression="execution(public * fi.wmdata.hr.apps.recruiting.um.*.*.*(..)) or execution(public * fi.wmdata.hr.apps.recruiting.ln.*.*.*(..)" />
    If you want to keep your annotation-based pointcut definitions, however, you could continue referencing them from XML the way you are now. For example:

    Code:
    <aop:pointcut id="around"
    expression="fi.wmdata.hr.apps.recruiting.common.logging.LogPointcut.Around()" />
    ...
    <bean id="logPointcut" class="fi.wmdata.hr.apps.recruiting.common.logging.LogPointcut" />
    Mike Bingham

Posting Permissions

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