Hmmm.... is there a way to configure Guice to inject any static fields with
@Inject annotations ? :-(

Anthony, desesperate developer


2008/8/26 Anthony MULLER <[EMAIL PROTECTED]>

> Hello !
>
> Is there a way to say once to Guice: inject any static fields named LOGGER?
>
> Regards,
> Anthony
>
>
> 2008/8/1 Anthony MULLER <[EMAIL PROTECTED]>
>
> Yes, I do the same but at method level and... In fact, my need is to log
>> all methods call BUT only methods defined in my public interfaces of my
>> objects, not other internal methods... So, I have to do some extra efforts
>> to guess if a method is defined in a "public" interface or not...
>>
>> I think I will keep my first solution because the one with
>> java.util.logging is not good for me... I use another logger... But, I hoped
>> to use the same mechanism: if Guice can inject a such logger, that means it
>> knows the type of the injectee... And it is what I need!
>>
>> Regards
>>
>> Anthony
>>
>>
>>
>> 2008/8/1 jordi <[EMAIL PROTECTED]>
>>
>>  i used a @Log annotation at type level to intercept all classes i wanted
>>> to log, with an optional attribute logger if you want to specify the name of
>>> the logger.
>>>
>>> Then i used Matchers from Guice to find all that classes and intercept
>>> them. The only thing that interceptor does is to manually set the Logger
>>> field in your logged class.
>>>
>>> I'll tune this @Log thing and write a post somewhere...
>>>
>>> jordi
>>>
>>>
>>> On Thu, Jul 31, 2008 at 4:01 PM, Anthony MULLER <
>>> [EMAIL PROTECTED]> wrote:
>>>
>>>> Any reponses on this issue please?
>>>>
>>>> I tried to add an handler on the logger automatically injected by Guice,
>>>> but it is not really fine...
>>>>
>>>> I believed java.util.logging was only an API and I could change the
>>>> underlying implementation but I was wrong :(
>>>>
>>>> Are there alternative to my primary solution?
>>>>
>>>> will Guice 2.0 help me anymore?
>>>>
>>>> Cheers,
>>>>
>>>> Anthony MÜLLER
>>>>
>>>>
>>>> 2008/7/31 Anthony MULLER <[EMAIL PROTECTED]>
>>>>
>>>>> Hmm hmm, thanks for reply!
>>>>>
>>>>> I didn't know this tip :)
>>>>>
>>>>> In fact, I didn't use java.util.logging... So, is it possible:
>>>>>
>>>>> 1) to specify my own Logger implementation (internal) through
>>>>> 'java.util.logging' (i don't know this class alot... I need to learn about
>>>>> it)
>>>>>
>>>>> or
>>>>>
>>>>> 2) overload Guice mecanism somewhere to tell it to inject my logger
>>>>> rather than java.util.logging
>>>>>
>>>>> Regards,
>>>>> Anthony MÜLLER
>>>>>
>>>>>
>>>>> 2008/7/31 Stuart McCulloch <[EMAIL PROTECTED]>
>>>>>
>>>>> 2008/7/31 Anthony MULLER <[EMAIL PROTECTED]>
>>>>>>
>>>>>>> Hello,
>>>>>>>
>>>>>>> I'm trying to use Guice to inject Logger into my objects... The main
>>>>>>> problem is that I didn't know which class is being injected. So, I can't
>>>>>>> build a logger with the convenient class.
>>>>>>>
>>>>>>
>>>>>> FYI, Guice has a default "java.util.logging.Logger" binding for each
>>>>>> class being injected
>>>>>>
>>>>>> so you can do this:
>>>>>>
>>>>>>    import java.util.logging.Logger;
>>>>>>
>>>>>>    @Inject
>>>>>>    Logger logger;
>>>>>>
>>>>>> and it will automatically use the correct name - however this is only
>>>>>> for java.util.logging.
>>>>>>
>>>>>> Other 'little' problem is that the traditional way to use Logger is to
>>>>>>> have a static field into class... And Guice doesn't like this (I mean 
>>>>>>> use of
>>>>>>> static)... Even if it is possible to inject them with a special Guice
>>>>>>> method.
>>>>>>>
>>>>>>> So, I'd like to know if somebody already think to this (and have a
>>>>>>> proposal to solve it).
>>>>>>>
>>>>>>> What I've done:
>>>>>>>
>>>>>>> 1) I create a class ILogger which have traditional looger méthods
>>>>>>> (like isWarnEnabled(), warn(String msg))
>>>>>>> 2) I had an extra parameter to this method to take as argument the
>>>>>>> object where log operation occured
>>>>>>>
>>>>>>> This is how I use it:
>>>>>>>
>>>>>>> public class MyObject {
>>>>>>>
>>>>>>>     // Note: field in NOT static
>>>>>>>     @Inject
>>>>>>>     private final ILogger LOGGER = null;
>>>>>>>
>>>>>>>     public void myMethod() {
>>>>>>>             //...
>>>>>>>             // Note the "this" parameter
>>>>>>>             if (LOGGER.isWarnEnabled(*this*)) {
>>>>>>>                 LOGGER.warn(*this*, "My message to log");
>>>>>>>             }
>>>>>>>             //...
>>>>>>>     }
>>>>>>> }
>>>>>>>
>>>>>>> The ILogger implementation looks like:
>>>>>>>
>>>>>>> @Singleton
>>>>>>> public class LoggerImpl implements ILogger {
>>>>>>>
>>>>>>>     // Cache to store already created logger: one logger per class
>>>>>>> strategy
>>>>>>>     private final Map<Class<?>, Logger> loggers = new
>>>>>>> HashMap<Class<?>, Logger>();
>>>>>>>
>>>>>>>     /**
>>>>>>>      * [EMAIL PROTECTED]
>>>>>>>      */
>>>>>>>     public void debug(Object src, String msg) {
>>>>>>>         getLogger(src).debug(msg);
>>>>>>>     }
>>>>>>>
>>>>>>>     // ... other traditional log methods
>>>>>>>
>>>>>>>     /**
>>>>>>>      * Return a logger instance or get it from cache.
>>>>>>>      *
>>>>>>>      * @param src
>>>>>>>      * @return Logger
>>>>>>>      */
>>>>>>>     private Logger getLogger(Object src) {
>>>>>>>         Class<?> clazz = null;
>>>>>>>         if (src instanceof Class<?>) {
>>>>>>>             clazz = (Class<?>) src;
>>>>>>>         } else {
>>>>>>>             clazz = src.getClass();
>>>>>>>         }
>>>>>>>         Logger logger = loggers.get(clazz);
>>>>>>>         if (logger == null) {
>>>>>>>             logger = Logger.getInstance(clazz.getName());
>>>>>>>             loggers.put(src.getClass(), logger);
>>>>>>>         }
>>>>>>>         return logger;
>>>>>>>     }
>>>>>>> }
>>>>>>>
>>>>>>> What do you think about this?
>>>>>>>
>>>>>>> Is it a good way to deal with logging issue ?
>>>>>>>
>>>>>>> Thanks!
>>>>>>>
>>>>>>> Anthony MÜLLER
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>
>>>>>>
>>>>>> --
>>>>>> Cheers, Stuart
>>>>>>
>>>>>>
>>>>>>
>>>>>
>>>>
>>>>
>>>>
>>>
>>> >>>
>>>
>>
>

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"google-guice" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/google-guice?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to