开发者

Using java annotation to inject logger dependency

开发者 https://www.devze.com 2023-03-12 19:34 出处:网络
I am using spring with aspect-j annotation support to allow for an @Loggable ann开发者_如何学Pythonotation.This allows automatic logging on a class based on the configuration.

I am using spring with aspect-j annotation support to allow for an @Loggable ann开发者_如何学Pythonotation. This allows automatic logging on a class based on the configuration.

I am wondering if I can somehow use this annotation to expose an slf4j Logger variable into the class for direct use, so that I don't have to do something to the effect of:

Logger logger = LoggerFactory.getLogger(MyClass.class);

It would be nice if the above was implicitly available due to the annotation and I could just go about doing logger.debug("..."); without the declaration. I'm not sure if this is even possible.


You can use the BeanPostProcessor interface, which is called by the ApplicationContext for all created beans, so you have the chance to fill the appropriate properties.

I created a simple implementation, which does that:

import java.lang.reflect.Field;
import java.util.List;

import net.vidageek.mirror.dsl.Mirror;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.stereotype.Component;

@Component
public class LoggerPostProcessor implements BeanPostProcessor {

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        List<Field> fields = new Mirror().on(bean.getClass()).reflectAll().fields(); 
        for (Field field : fields) {
            if (Logger.class.isAssignableFrom(field.getType()) && new Mirror().on(field).reflect().annotation(InjectLogger.class) != null) {
                new Mirror().on(bean).set().field(field).withValue(LoggerFactory.getLogger(bean.getClass()));
            }
        }
        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        return bean;
    }
}

You don't have to do any complex registration step, since the ApplicationContext is capable of recognizing BeanPostProcessor instances and automatically register them.

The @InjectLogger annotation is:

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

@Retention(RetentionPolicy.RUNTIME)
public @interface InjectLogger {
}

And then you can easily use the annotation:

public static @InjectLogger Logger LOGGER;

...

LOGGER.info("Testing message");

I used the Mirror library to find the annotated fields, but obviously you may perform a manual lookup in order to avoid this additional dependency.

It's actually a nice idea to avoid repeated code, and even small issues that come from copying and paste the Logger definitions from other classes, like when we forget to change the class parameter, which leads to wrong logs.


You can't do it with an aspect, but lombok can help you in a, in my opinion, elegant way. See @Log annotation.


I think the solution from @Redder is a great way of doing this. However, I didn't want to include the Mirror library so I wrote an implementation of LoggerPostProcessor that uses the Java reflect library instead. Here it is:

package com.example.spring.postProcessor;

import com.example.annotation.InjectLogger;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.Arrays;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.stereotype.Component;

@Component
public class LoggerPostProcessor implements BeanPostProcessor {

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

    /* (non-Javadoc)
     * @see org.springframework.beans.factory.config.BeanPostProcessor#postProcessBeforeInitialization(java.lang.Object, java.lang.String)
     */
    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {

        List<Field> fields = Arrays.asList(bean.getClass().getDeclaredFields());

        for (Field field : fields) {
            if (Logger.class.isAssignableFrom(field.getType()) && field.getAnnotation(InjectLogger.class) != null) {

                logger.debug("Attempting to inject a SLF4J logger on bean: " + bean.getClass());

                if (field != null && (field.getModifiers() & Modifier.STATIC) == 0) {
                    field.setAccessible(true);
                    try {
                        field.set(bean, LoggerFactory.getLogger(bean.getClass()));
                        logger.debug("Successfully injected a SLF4J logger on bean: " + bean.getClass());
                    } catch (IllegalArgumentException e) {
                        logger.warn("Could not inject logger for class: " + bean.getClass(), e);
                    } catch (IllegalAccessException e) {
                        logger.warn("Could not inject logger for class: " + bean.getClass(), e);
                    }
                }
            }
        }

        return bean;
    }

    /* (non-Javadoc)
     * @see org.springframework.beans.factory.config.BeanPostProcessor#postProcessAfterInitialization(java.lang.Object, java.lang.String)
     */
    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        return bean;
    }

}


I want to make some improvements to @Redder's solution.

  • First - we can omit introduction of new annotation @Log, instead we can use Spring's @Autowired annotation with 'required' flag set to 'false' to make Spring not to check that bean was injected or not (because, we will inject it later).
  • Second - use Spring's ReflectionUtils API that provides all needed methods for field discovering and manipulation, so we don't need additional external dependencies.

Here an example (in Java 8, but can be rewritten in Java 7/6/etc., also slf4j facade is used but it can be replaced with any other logger):

@Component
public class LoggerPostProcessor implements BeanPostProcessor {

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {

        Logger logger = getLogger(bean.getClass());
        doWithFields(bean.getClass(), field -> {

            makeAccessible(field);
            setField(field, bean, logger);

        }, field -> field.isAnnotationPresent(Autowired.class) && Logger.class.equals(field.getType()));

        return bean;
    }
    ...
}
...
//logger injection candidate
@Autowired(required = false)
private Logger log;


Since I got this as the first result when trying to do the same thing in CDI (JSR 299: Context and Dependency Injection), this link shows the straightforward way to do this using CDI (and also an alternative using Spring):

Basically, you only need to inject:

class MyClass {
   @Inject private Log log;

And have a logger factory like so:

@Singleton
public class LoggerFactory implements Serializable {
    private static final long serialVersionUID = 1L;

    static final Log log = LogFactory.getLog(LoggerFactory.class);

   @Produces Log createLogger(InjectionPoint injectionPoint) {
    String name = injectionPoint.getMember().getDeclaringClass().getName(); 
    log.debug("creating Log instance for injecting into " + name); 
    return LogFactory.getLog(name);
    }   
}

I found that I needed to add transient to the injected log so that I did not get a passivating scope exception in my session scoped beans:

@Named()
@SessionScoped()
public class MyBean implements Serializable {
    private static final long serialVersionUID = 1L;

    @Inject
    private transient Log log;


Herald provides a very simple BeanPostProcessor which does all the magic for you. You can annotate any field of Spring bean with a @Log annotation to let Herald inject suitable logger in this field.

Supported logging frameworks:

  • JavaTM 2 platform's core logging framework
  • Apache Commons Logging
  • Simple Logging Facade for Java (SLF4J)
  • SLF4J Extended logger
  • Logback
  • Apache Log4j
  • Apache Log4j 2
  • JBoss Logging
  • Syslog4j
  • Syslog4j fork from Graylog
  • Fluent Logger for Java

It is also possible to add other logging frameworks.

Github repo: https://github.com/vbauer/herald

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号