Aspect logging in Guice?

174 views
Skip to first unread message

Rob Withers

unread,
Oct 9, 2012, 8:31:49 PM10/9/12
to google...@googlegroups.com
Before I run off and implement a LoggingInterceptor, is there an AOP logging facility in core guice or an extension I could use?

Thanks,
Rob

Cédric Beust ♔

unread,
Oct 9, 2012, 10:55:44 PM10/9/12
to google...@googlegroups.com
Something like that?

Or you could inject your own loggers with type listeners.

-- 
Cédric




--
You received this message because you are subscribed to the Google Groups "google-guice" group.
To view this discussion on the web visit https://groups.google.com/d/msg/google-guice/-/rphjmZue5wQJ.
To post to this group, send email to google...@googlegroups.com.
To unsubscribe from this group, send email to google-guice...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-guice?hl=en.

Rob Withers

unread,
Oct 9, 2012, 11:37:37 PM10/9/12
to google...@googlegroups.com

The first one you point to is a “Built-in Binding”.  However, I do not see this in the guice-3.0.jar.  Also, how do I use it?

 

The second link on Custom Injections scans for a Logger in each class, so this does not seem to be using AOP.

 

Here is the class I came up with, below.  My problem is that I don’t see how to install the pointcut to intercept a specific method in a class.  I can bind it to all methods, but not one method.  How can I do that?

 

public class WhisperModule extends AbstractMurmurModule {

 

       @Override

       protected void configure() {

              …

 

bindInterceptor(Matchers.subclassesOf(WhisperTerminal.class), Matchers.any(), new LoggingInterceptor());

       }

}

 

public class LoggingInterceptor implements MethodInterceptor {

    private Logger logger;

 

    public LoggingInterceptor() {

       PropertyConfigurator.configure("log4j.properties");

       logger = Logger.getLogger(LoggingInterceptor.class);

    }

 

    public Object invoke(MethodInvocation methodInvocation) throws Throwable {

       String header = methodInvocation.getThis().getClass()

                     + ":" + methodInvocation.getMethod().getName();

        logger.info(header + " invocation");

        Object result = null;

        try {

            result = methodInvocation.proceed();

            logger.info(header + " return: " + result);

            return result;

        } catch (Exception ex) {

            logger.error(header, ex);

              throw ex;

        }

    }

}

 

 

Thanks,

Rob

Stephan Classen

unread,
Oct 11, 2012, 2:45:58 AM10/11/12
to google...@googlegroups.com
You can write your own matche and use it instead of passing Matchers.any().
Or if it is ok for you to annotate the methods which you want to intercept you can use the existing Matchers.annotatedWith(...)

Rob Withers

unread,
Oct 11, 2012, 10:42:25 AM10/11/12
to google...@googlegroups.com

I did find the Annotations solution.

 

What would my own Matcher look like, perhaps using reflection and AspectJ expressions to specify methods?  Is there such a duck out there?

 

Thanks,

Rob

Stephan Classen

unread,
Oct 15, 2012, 4:03:26 AM10/15/12
to google...@googlegroups.com
For implementing your own matcher AbstractMatcher would be a good start.

i.e.
public class MyMatcher extends AbstractMatcher<Method> {
    public boolean matches(Method method) {
        // your code here
    }
}

Note: the above can only match methods. if it should also be able to match classes make your matcher extend one of the following:
 - AbstractMatcher<AnnotatedElement>
 - AbstractMatcher<GenericDeclaration>
 - AbstractMatcher<Object>
Reply all
Reply to author
Forward
0 new messages