Is this possible? @Inject private static final Logger log;

1,048 views
Skip to first unread message

Jarrod Roberson

unread,
May 9, 2011, 5:20:00 PM5/9/11
to google...@googlegroups.com
I am trying to evaluate Guice as our standard for doing what it does best. 

But I am having a hard time accepting some of the trade offs?

I like the auto-magically assigning with @Inject private static Logger logger;

But I don't want to sacrifice the ability to make that "final" as well.

Is there some magical incantation to allow me to specify injects on private static final variables?

Lucas Cavalcanti

unread,
May 9, 2011, 5:26:23 PM5/9/11
to google...@googlegroups.com
why do you need that? what would be the use?

static final fields are final (ta-da), you can't change them (without VM specific nasty codes)
i

--
You received this message because you are subscribed to the Google Groups "google-guice" group.
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.

jMotta

unread,
May 9, 2011, 8:47:12 PM5/9/11
to google...@googlegroups.com
Jarrod,

What is the meaning of doing this? Inversion of Control is about making your objects more cohesive as possible, loose coupled and modular. But with logging there is no benefits, once because you'll create the logger object as an static variable and this is responsability of the class and not of the object itself.

What you can do in this sense and Guice will help you either is write some crosscut concern code with interceptors, you create an annotation, annotate your method / class and then creates a bind in your Guice module that activate an interceptor for every class / method annotated with your created annotation.

Because what I've understood about your "doubt" isn't that you don't know how to use the features in Guice and yes where you can use, what benefits it will bring and how can you use them. Right?

This link will help you, I guess. Just for information, moving the responsabilities of creating objects for you and even binding the correct implementation of some interface is about Inversion of Control while sharing some behavior among some layers of your code is about Aspect Oriented Programming.

Hope it helps you! :)

Atenciosamente,

Jayr Motta
Software Developer




On Mon, May 9, 2011 at 6:20 PM, Jarrod Roberson <jarrod....@gmail.com> wrote:
--

Jarrod Roberson

unread,
May 10, 2011, 1:09:35 PM5/10/11
to google...@googlegroups.com
I know about IoC, DI and all the other theory, I don't need information on that, I know what Guice does, I know what Spring does, I know how they do things differently.

What I do like the ability to say

@Inject
private static Logger logger;

and have Guice auto magically put the class name as the logger, it relieves my team of mindless boilerplate code

I just don't like the trade off of not being able to mark the logger reference as final, I am just looking for some one with more experience with Guice to 
see if there is a way to do this with the final keyword still there.

Colin Decker

unread,
May 10, 2011, 1:36:34 PM5/10/11
to google...@googlegroups.com
As mentioned both here and on stackoverflow, you can't inject a static final field. Personally, I consider the Logger injection Guice can do a novelty at most and not worth thinking about much. If you want a static final Logger, just create it like normal. It's not that much boilerplate. Alternatively, just don't worry about it not being final. Do you think someone's going to accidentally reassign it?

-- 
Colin


--

Lucas Cavalcanti

unread,
May 10, 2011, 1:45:24 PM5/10/11
to google...@googlegroups.com
you could create a simple eclipse template (or whatever IDE you like) that generates the logger, automatically:

Eclipse >> Window >> Preferences >> Java >> Editor >> Templates >> New
give it a name, like logger, Java type members as context, and use the code below as body:

${:import(org.slf4j.LoggerFactory, org.slf4j.Logger)}
private static final Logger logger = LoggerFactory.getLogger(${enclosing_type}.class);


or similar code if you don't use slf4j.

This way you create the log line simply by typing logger<Ctrl+Space>

[]'s

Filipe Sousa

unread,
May 10, 2011, 4:07:12 PM5/10/11
to google...@googlegroups.com
I have been using this template
private static final ${loggerType:newType(org.slf4j.Logger)} ${loggerName:newName(org.slf4j.Logger)} = ${loggerFactoryType:newType(org.slf4j.LoggerFactory)}.getLogger(${enclosing_type}.class);

and this
if (${loggerName:newName(org.slf4j.Logger)}.isInfoEnabled())
${loggerName}.info("${cursor}");

Willi Schönborn

unread,
May 10, 2011, 4:15:13 PM5/10/11
to google...@googlegroups.com
Honestly why would you still use that pattern when you have parameterized logging?
http://www.slf4j.org/faq.html#logging_performance

Jarrod Roberson

unread,
May 11, 2011, 1:12:20 PM5/11/11
to google...@googlegroups.com
because I don't want to use slf4j

Lucas Cavalcanti

unread,
May 11, 2011, 1:23:18 PM5/11/11
to google...@googlegroups.com
change the imports on the template to the respective Log4j (or your logging library) code

On Wed, May 11, 2011 at 2:12 PM, Jarrod Roberson <jarrod....@gmail.com> wrote:
because I don't want to use slf4j

--

Gary Pampara

unread,
May 11, 2011, 1:39:05 PM5/11/11
to google...@googlegroups.com
Barring the separate issue with the final modifier, you can achieve
your aim of getting the logger injected with the classname defined for
you by Guice.

You'll need to employ a custom injection to get it working
(http://code.google.com/p/google-guice/wiki/CustomInjections). It is
possible to still use the normal @Inject annotation with some
additional checks using reflection to make the integration a little
simpler. YMMV.

Hope this helps. I did something similar previously for a contract
where the company decided on writing their own logger (why?) and
wanted it injected.

Reply all
Reply to author
Forward
0 new messages