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); }
}
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.