AOP to slf4j logger.info

58 views
Skip to first unread message

Yan Jiang

unread,
Oct 18, 2017, 7:56:42 AM10/18/17
to google-guice
Hi, all.

I am thinking to add some pre&post actions for SLF4J, logger.info() and logger.error().
Is it possible to do this with Guice method interceptor?

Currently, I am doing something like:

Matcher<Class> subClassMatcher = Matchers.subclassesOf(org.slf4j.Logger.class);
bindInterceptor
(packageMatcher, Matchers.any(), new intercepters.Logger());


Backend, we are using SLF4J and LogBack.

But it doesn't work.


Luke Sandberg

unread,
Oct 18, 2017, 12:15:58 PM10/18/17
to google-guice
Guice AOP only works on objects that Guice constructs.  So probably not 

--
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/c2ba51d4-9352-45bf-acc6-4c41c4b1dbd3%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Yan Jiang

unread,
Oct 18, 2017, 9:56:35 PM10/18/17
to google-guice
Thanks, Luke.

I get it now.

I still get one shot, SLF4j.logger is an interface, and Logback.Logger implements that interface.
So I am thinking if I can do 

import slf4j.Logger;

class Student {
@Logger Logger logger;
}

and bind(slf4j.Logger.class).to(logback.Logger.class);
I can still do it, right?

The tricky part is to Provider all the necessary information in constructor, right?

Stephan Classen

unread,
Oct 19, 2017, 2:15:16 AM10/19/17
to google...@googlegroups.com
If the @Logger causes Guice to inject an instance of a logger class which was not created by you (i.e. bindInstance) then it should work. Given the class injected is not final.

Yan Jiang

unread,
Oct 19, 2017, 3:41:56 AM10/19/17
to google-guice
Thank you, sci.

That is a good point.
I think I'll have to go with creating my own LoggerWrapper.
Reply all
Reply to author
Forward
0 new messages