views:

48

answers:

1

I need to log many classes in some packages in a project which I can not change its source code. So I need a solution which I can specify package name, and with spring aop add logging to that package's classes without change them but I dont know how can I do that. How can I do that?

A: 

With Spring AOP you can only log these classes if they are used as Spring Beans, and even then you can only log public method executions.

Here is an aspect in @AspectJ notification (this is the style that is compatible with both "real AspectJ" and Spring AOP, read about the difference in the spring reference) that you can use in both Spring AOP and AspectJ byte code weaving:

@Aspect
public class LoggingAspect{

    @Pointcut("execution(* com.mycompany.myproject.*.*(..))")
    public void methodToLog(){
    };

    @Around("methodToLog()")
    public Object logMethod(final ProceedingJoinPoint joinPoint) throws Throwable{
        final StaticPart staticPart = joinPoint.getStaticPart();
        final String sig =
            "" + staticPart.getSignature() + " with args: "
                + Arrays.deepToString(joinPoint.getArgs());
        System.out.println("Entering method " + sig);
        final Object result = joinPoint.proceed();
        System.out.println("Leaving method " + sig);
        return result;
    }

}

Here is a stupid class with some methods:

package com.mycompany.myproject;
public class Dummy1{

    public static void main(final String[] args){
        final Dummy1 dummy = new Dummy1();
        dummy.doSomeStuff();
        dummy.doSomeStuffWithSomeArgs("Hello", 123);
    }

    private void doSomeStuff(){}

    public void doSomeStuffWithSomeArgs(final String firstArg,
        final int secondArg){}

}

When you start this class in Eclipse / AJDT as Java/AspectJ application, you get the following output:

Entering method void com.mycompany.myproject.Dummy1.main(String[]) with args: [[]]
Entering method void com.mycompany.myproject.Dummy1.doSomeStuff() with args: []
Leaving method void com.mycompany.myproject.Dummy1.doSomeStuff() with args: []
Entering method void com.mycompany.myproject.Dummy1.doSomeStuffWithSomeArgs(String, int) with args: [Hello, 123]
Leaving method void com.mycompany.myproject.Dummy1.doSomeStuffWithSomeArgs(String, int) with args: [Hello, 123]
Leaving method void com.mycompany.myproject.Dummy1.main(String[]) with args: [[]]

To test this in Spring AOP would involve more work (the main method approach won't work, you will have to create an ApplicationContext and register a bean of type Dummy1, on which you will call the methods), so I'll leave that to you, but I am pretty sure the private method call will not be logged.

If you download the SpringSource Tool Suite, you get nice tools for aspect visualisation and testing. You should also read the AspectJ book, even if you only want to use Spring AOP. It's a great book.


BTW: you will obviously want to use a real logger, not system.out. You can either define one per aspect, or (only with real aspectj) you can introduce it as a static member in the target class to get per-class logging. A killer feature of AspectJ in my opinion.

seanizer