有没有一种简单的方法为每个类创建一个记录器实例? [英] Is there an easy way to create a logger instance for every class?

查看:125
本文介绍了有没有一种简单的方法为每个类创建一个记录器实例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在使用静态方法进行记录(因为我觉得登录Android非常容易),但现在我需要为不同的类配置不同的appender,所以我的静态日志记录方法有问题。

I am now using static method to log (because I find it very easy to log in Android), but now I need to configure different appenders for different classes, so I have a problem with static logging method.

我读了 Log4J:创建Logger的策略实例,我注意到我现在是时候改变日志记录策略,所以我需要以下代码到我的班级,这需要一个特殊的记录器。

And I read Log4J: Strategies for creating Logger instances, I notice it is time for me to change the logging strategy, so I will need the following code to my class which needs a special logger.

private static final Logger logger  =  Logger.getLogger(XXXX.class);

我觉得既然这是重复的工作,那么我们有什么方法可以避免添加重复的代码每一个类,或者只是编写更少的代码。 AOP或依赖注入可以做这样的工作吗?

I feel since this is repeated job, so is there any ways for us to avoid adding repeated code to every single class, or just write less code. Will AOP or dependency injection can do such jobs?

我正在使用log4j和Spring,但是其他任何方法都将受到高度赞赏。

I am using log4j and Spring, but any other ways to do that will be highly appreciated.

推荐答案

是的,你可以。


  • 创建自定义注释说@Logger

  • Create your customized annotation say @Logger

@Retention(RUNTIME)//will be used at runtime, so retain till runtime
@Target(FIELD)//field level annotation
public @interface Logger {
}


  • 将Spring记录器字段注释为由Spring注入的bean中的@Logger。

  • Annotate your Logger field as @Logger in the beans which get's injected by Spring.

    @Logger
    private Logger logger;
    

    在这里你可以使用slf4j logger但是你可以直接使用log4j等等。看看我是如何注释的记录器在这里。这是由Spring创建的bean。

    Here you could use slf4j logger but upto you as you can directly use log4j etc. See how i annotated logger here. This is within the bean which gets created by Spring.

    使用名为BeanPostProcessor的Spring生命周期接口和使用reflection utils类的方法postProcessBeforeInitialization注入记录器。

    Use of Spring lifecycle interface called BeanPostProcessor and in method postProcessBeforeInitialization, using reflection utils class, to inject the logger.

    public class LoggerInjector implements BeanPostProcessor {
    
    /**
     * Return the bean itself.
    */
    public Object postProcessAfterInitialization(final Object bean, final String beanName) throws BeansException {
        return bean;
    }
    
    /**
     * For all beans before initialization, inject the logger using slf4j.
     * @param bean
     * @param beanName
     * @return returns same bean by injecting logger.
     */
    public Object postProcessBeforeInitialization(final Object bean, final String beanName) throws BeansException {
        ReflectionUtils.doWithFields(bean.getClass(), new FieldCallback() {
            public void doWith(final Field field) throws IllegalArgumentException, IllegalAccessException {
                ReflectionUtils.makeAccessible(field);
                if (field.getAnnotation(Logger.class) != null) {
                    if (field.get(bean) == null) {
                        final Logger logger = LoggerFactory.getLogger(bean.getClass());
                        field.set(bean, logger);
                    }
                }
            }
        });
        return bean;
    }
    }
    


  • 一旦完成,让春天知道你有一个记录器注入器,如:

  • Once done, let spring know that you have a logger injector like:

    <bean id="loggerInjector" 
        class="com.mypackage.LoggerInjector"/>
    


  • 这篇关于有没有一种简单的方法为每个类创建一个记录器实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    相关文章
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆