Discussion:
AOP base logging in Guice
Ankur Mahajan
2016-07-25 11:10:14 UTC
Permalink
I am trying to implement AOP based logging in Google - Guice. I have used "
*MethodInterceptor*" for this but it doesn't work. I have used same in
Spring by defining point-cuts. Everything is working fine there.

*Spring Code for AOP based logging -*

@Aspect
public class LoggingAspect {

private static Logger logger = LoggerFactory.getLogger(LoggingAspect.class
);

@Around("requiredLog()")
public Object bentoBoxAround(ProceedingJoinPoint proceedingJoinPoint) {

Object returnValue = null;
try {

logger.info("Entered into the method -> " + proceedingJoinPoint.
getSignature().toShortString()
+ " and input arguments are -> " + Arrays.asList(
proceedingJoinPoint.getArgs()));
returnValue = proceedingJoinPoint.proceed();
logger.info("Method Execution over !! " + proceedingJoinPoint.
getSignature().toShortString());
} catch (Throwable e) {
logger.error("Method has an exception " + e.getMessage());
}
return returnValue;
}

@Pointcut("within(org.cal.bento..*)")
public void allRequiredPakageLog() {
}

}


From above code we can log all the class and method executions inside the "
org.cal.bento.*" package.

*Guice code for AOP based logging -*

public class GuiceLoggingInterceptor implements MethodInterceptor {

private static Logger logger = LoggerFactory
.getLogger(GuiceLoggingInterceptor.class);

@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
Object returnValue = null;
try {
logger.info("GUICE - Entered into the method -> " + invocation.
getMethod().getName()
+ " and input arguments are -> " + Arrays.asList(
invocation.getArguments()));
returnValue = invocation.proceed();
logger.info("Method Execution over !! " + invocation.getMethod().
getName());
} catch (Throwable e) {
logger.error("GUICE - Method has an exception " + e.getMessage());
}
return returnValue;
}
}

*Binding Class -*

public class GuiceAopModule extends AbstractModule {

@Override
protected void configure() {
bindInterceptor(Matchers.any(), Matchers.any(), new
GuiceLoggingInterceptor());
}
}

Can we do similar in Guice for logging (write only one Aspect based class
for whole logging system). I don't want to modify every class.
*Refered Tutorial* -
https://schakrap.wordpress.com/2009/07/30/method-entry-exit-logging-in-guice-with-aop/
Any help would be highly appreciated.
--
You received this message because you are subscribed to the Google Groups "google-guice" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-guice+***@googlegroups.com.
To post to this group, send email to google-***@googlegroups.com.
Visit this group at https://groups.google.com/group/google-guice.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-guice/23ee65b0-983b-47db-81f1-06d9ed618b69%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Olivier Grégoire
2016-07-25 11:21:16 UTC
Permalink
You should use another class Matcher. Currently you configured your binder
to adapt all injected classes, no matter where they're located.

To do what you want, you should change your binding to the following:

bindInterceptor(Matchers.inSubpackage("org.cal.bento"), Matchers.any(),
new GuiceLoggingInterceptor());
Post by Ankur Mahajan
I am trying to implement AOP based logging in Google - Guice. I have used "
*MethodInterceptor*" for this but it doesn't work. I have used same in
Spring by defining point-cuts. Everything is working fine there.
*Spring Code for AOP based logging -*
@Aspect
public class LoggingAspect {
private static Logger logger = LoggerFactory.getLogger(LoggingAspect.
class);
@Around("requiredLog()")
public Object bentoBoxAround(ProceedingJoinPoint proceedingJoinPoint) {
Object returnValue = null;
try {
logger.info("Entered into the method -> " + proceedingJoinPoint.
getSignature().toShortString()
+ " and input arguments are -> " + Arrays.asList(
proceedingJoinPoint.getArgs()));
returnValue = proceedingJoinPoint.proceed();
logger.info("Method Execution over !! " + proceedingJoinPoint.
getSignature().toShortString());
} catch (Throwable e) {
logger.error("Method has an exception " + e.getMessage());
}
return returnValue;
}
@Pointcut("within(org.cal.bento..*)")
public void allRequiredPakageLog() {
}
}
From above code we can log all the class and method executions inside the "
org.cal.bento.*" package.
*Guice code for AOP based logging -*
public class GuiceLoggingInterceptor implements MethodInterceptor {
private static Logger logger = LoggerFactory
.getLogger(GuiceLoggingInterceptor.class);
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
Object returnValue = null;
try {
logger.info("GUICE - Entered into the method -> " + invocation.
getMethod().getName()
+ " and input arguments are -> " + Arrays.asList(
invocation.getArguments()));
returnValue = invocation.proceed();
logger.info("Method Execution over !! " + invocation.getMethod().
getName());
} catch (Throwable e) {
logger.error("GUICE - Method has an exception " + e.getMessage());
}
return returnValue;
}
}
*Binding Class -*
public class GuiceAopModule extends AbstractModule {
@Override
protected void configure() {
bindInterceptor(Matchers.any(), Matchers.any(), new
GuiceLoggingInterceptor());
}
}
Can we do similar in Guice for logging (write only one Aspect based class
for whole logging system). I don't want to modify every class.
*Refered Tutorial* -
https://schakrap.wordpress.com/2009/07/30/method-entry-exit-logging-in-guice-with-aop/
Any help would be highly appreciated.
--
You received this message because you are subscribed to the Google Groups
"google-guice" group.
To unsubscribe from this group and stop receiving emails from it, send an
Visit this group at https://groups.google.com/group/google-guice.
To view this discussion on the web visit
https://groups.google.com/d/msgid/google-guice/23ee65b0-983b-47db-81f1-06d9ed618b69%40googlegroups.com
<https://groups.google.com/d/msgid/google-guice/23ee65b0-983b-47db-81f1-06d9ed618b69%40googlegroups.com?utm_medium=email&utm_source=footer>
.
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "google-guice" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-guice+***@googlegroups.com.
To post to this group, send email to google-***@googlegroups.com.
Visit this group at https://groups.google.com/group/google-guice.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-guice/CAORw%3DcO4Esa3jTSq80uEj%3DpEHK2CfKqi8o-LsYFue%2B6Lf%3D7rMw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Ankur Mahajan
2016-07-25 11:43:00 UTC
Permalink
Hi Olivier,

*Thanks *for the reply, I've tried what you suggested but No LUCK :(
Could you please suggest something else which would work like charm :).
Post by Olivier Grégoire
You should use another class Matcher. Currently you configured your binder
to adapt all injected classes, no matter where they're located.
bindInterceptor(Matchers.inSubpackage("org.cal.bento"),
Matchers.any(), new GuiceLoggingInterceptor());
Post by Ankur Mahajan
I am trying to implement AOP based logging in Google - Guice. I have used
"*MethodInterceptor*" for this but it doesn't work. I have used same in
Spring by defining point-cuts. Everything is working fine there.
*Spring Code for AOP based logging -*
@Aspect
public class LoggingAspect {
private static Logger logger = LoggerFactory.getLogger(LoggingAspect.
class);
@Around("requiredLog()")
public Object bentoBoxAround(ProceedingJoinPoint proceedingJoinPoint) {
Object returnValue = null;
try {
logger.info("Entered into the method -> " + proceedingJoinPoint
.getSignature().toShortString()
+ " and input arguments are -> " + Arrays.asList(
proceedingJoinPoint.getArgs()));
returnValue = proceedingJoinPoint.proceed();
logger.info("Method Execution over !! " + proceedingJoinPoint.
getSignature().toShortString());
} catch (Throwable e) {
logger.error("Method has an exception " + e.getMessage());
}
return returnValue;
}
@Pointcut("within(org.cal.bento..*)")
public void allRequiredPakageLog() {
}
}
From above code we can log all the class and method executions inside the
"org.cal.bento.*" package.
*Guice code for AOP based logging -*
public class GuiceLoggingInterceptor implements MethodInterceptor {
private static Logger logger = LoggerFactory
.getLogger(GuiceLoggingInterceptor.class);
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
Object returnValue = null;
try {
logger.info("GUICE - Entered into the method -> " + invocation.
getMethod().getName()
+ " and input arguments are -> " + Arrays.asList(
invocation.getArguments()));
returnValue = invocation.proceed();
logger.info("Method Execution over !! " + invocation.getMethod().
getName());
} catch (Throwable e) {
logger.error("GUICE - Method has an exception " + e.getMessage
());
}
return returnValue;
}
}
*Binding Class -*
public class GuiceAopModule extends AbstractModule {
@Override
protected void configure() {
bindInterceptor(Matchers.any(), Matchers.any(), new
GuiceLoggingInterceptor());
}
}
Can we do similar in Guice for logging (write only one Aspect based class
for whole logging system). I don't want to modify every class.
*Refered Tutorial* -
https://schakrap.wordpress.com/2009/07/30/method-entry-exit-logging-in-guice-with-aop/
Any help would be highly appreciated.
--
You received this message because you are subscribed to the Google Groups
"google-guice" group.
To unsubscribe from this group and stop receiving emails from it, send an
<javascript:>.
Visit this group at https://groups.google.com/group/google-guice.
To view this discussion on the web visit
https://groups.google.com/d/msgid/google-guice/23ee65b0-983b-47db-81f1-06d9ed618b69%40googlegroups.com
<https://groups.google.com/d/msgid/google-guice/23ee65b0-983b-47db-81f1-06d9ed618b69%40googlegroups.com?utm_medium=email&utm_source=footer>
.
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "google-guice" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-guice+***@googlegroups.com.
To post to this group, send email to google-***@googlegroups.com.
Visit this group at https://groups.google.com/group/google-guice.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-guice/0265d515-676f-4f8a-916c-9539d95d1eec%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Ankur Mahajan
2016-07-25 11:43:59 UTC
Permalink
*Hi Olivier,*

Thanks for the reply, I've tried what you suggested but No LUCK :(
Could you please suggest something else which would work like charm :).
--
You received this message because you are subscribed to the Google Groups "google-guice" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-guice+***@googlegroups.com.
To post to this group, send email to google-***@googlegroups.com.
Visit this group at https://groups.google.com/group/google-guice.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-guice/912f0de3-45a9-45f5-b04d-d5f603d9e21c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Olivier Grégoire
2016-07-25 12:20:04 UTC
Permalink
Hello,


Your problem then isn't with Guice but probably with your logger. As proof,
here's a working example. Feel free to directly adapt it.

com/example/logged/Echoer.java

package com.example.logged;

public class Echoer {
public String echo(String s) { return String.format("echo %s", s); }
}

com/example/notlogged/Printer.java

package com.example.notlogged;

public class Printer {
public String print(String s) { return String.format("print %s", s); }
}

com/example/Main.java

package com.example;

import com.example.logged.Echoer;
import com.example.notlogged.Printer;
import com.google.inject.*;
import com.google.inject.matcher.Matchers;
import org.aopalliance.intercept.*;
import java.util.Arrays;

public class Main {
@Inject Echoer echoer;
@Inject Printer printer;
public static void main(String[] args) {
Main app = Guice.createInjector(new AbstractModule() {
@Override
protected void configure() {
bindInterceptor(Matchers.inSubpackage("com.example.logged"),
Matchers.any(), new MethodInterceptor() {
@Override
public Object invoke(MethodInvocation mi) throws Throwable {
System.out.printf("Calling method %s with arguments %s%n",
mi.getMethod().getName(), Arrays.toString(mi.getArguments()));
try {
Object returnValue = mi.proceed();
System.out.printf("Method %s returned %s%n",
mi.getMethod().getName(), returnValue);
return returnValue;
} catch (Throwable t) {
System.out.printf("Method %s threw an exception %s%n",
mi.getMethod().getName(), t);
throw t;
}
}
});
}
}).getInstance(Main.class);
System.out.println(app.echoer.echo("foo"));
System.out.println(app.printer.print("bar"));
}
}

The result is :

Calling method echo with arguments [foo]
Method echo returned echo foo
echo foo
print bar

It is expected that this is the correct result, since we only wrap around
the package "com.example.logged" which contains the class Echoer, not the
class Printer.
Post by Ankur Mahajan
*Hi Olivier,*
Thanks for the reply, I've tried what you suggested but No LUCK :(
Could you please suggest something else which would work like charm :).
--
You received this message because you are subscribed to the Google Groups
"google-guice" group.
To unsubscribe from this group and stop receiving emails from it, send an
Visit this group at https://groups.google.com/group/google-guice.
To view this discussion on the web visit
https://groups.google.com/d/msgid/google-guice/912f0de3-45a9-45f5-b04d-d5f603d9e21c%40googlegroups.com
<https://groups.google.com/d/msgid/google-guice/912f0de3-45a9-45f5-b04d-d5f603d9e21c%40googlegroups.com?utm_medium=email&utm_source=footer>
.
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "google-guice" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-guice+***@googlegroups.com.
To post to this group, send email to google-***@googlegroups.com.
Visit this group at https://groups.google.com/group/google-guice.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-guice/CAORw%3DcMwYP%3DLkVgqAiwW4kYLbpVtv1gwt4rZY3FW7Qr9J3j25Q%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Ankur Mahajan
2016-07-27 06:05:45 UTC
Permalink
Hi Olivier,

Thanks for replying, I finally figure out this thing. In my case spring is
loading all the dependencies of Guice so I made some configuration changes
and It start working.
--
You received this message because you are subscribed to the Google Groups "google-guice" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-guice+***@googlegroups.com.
To post to this group, send email to google-***@googlegroups.com.
Visit this group at https://groups.google.com/group/google-guice.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-guice/a5d803a8-31f2-4d67-9861-6b96a553b0a8%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Loading...