在运行时设置文件路径时,log4j日志文件未滚动

时间:2018-06-27 09:39:57

标签: java spring-mvc reflection log4j

代码2 正在将Logger实例注入代码1 中的LOG字段。日志将转到正确的文件,但日志未滚动。但是,将代码1 代码3 结合使用时,日志将滚动。我需要使代码1 代码2 一起使用。追加RollingFileAppender的核心逻辑是相同的,但是我不知道为什么在第一种情况下日志不会滚动。请您说明一下我做错了什么以及可能的解决方案。

@Retention(RUNTIME)
@Target(FIELD)
@Documented
public @interface Log {
    Class<?> clazz();
}

代码1:-

@RestController
@RequestMapping(value = "/util/")
public class UtilityController {
    @Log(clazz = COScheduler.class)
    private static Logger LOG;
}

代码2:-

package text.worker.config.log;

import java.lang.reflect.Field;
import java.lang.reflect.Type;

import javax.annotation.PostConstruct;

import org.apache.log4j.Logger;
import org.apache.log4j.PatternLayout;
import org.apache.log4j.RollingFileAppender;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.stereotype.Component;
import org.springframework.util.ClassUtils;
import org.springframework.util.ReflectionUtils;    

@Component
public class LogInjector implements BeanPostProcessor {

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

    @Override
    public Object postProcessBeforeInitialization(final Object bean, String name) throws BeansException {
        ReflectionUtils.doWithFields(bean.getClass(), new ReflectionUtils.FieldCallback() {
            public void doWith(Field field) throws IllegalArgumentException, IllegalAccessException {
                // make the field accessible if defined private
                ReflectionUtils.makeAccessible(field);
                if (field.getAnnotation(Log.class) != null) {
                    Class<?> cl = ClassUtils.getUserClass(field.getAnnotation(Log.class).clazz());

                    Logger log = Logger.getLogger(cl);

                    if(log.getAppender("worker") == null) {
                        PatternLayout patternLayout = new PatternLayout();
                        patternLayout.setConversionPattern("%d{yyyy-MM-dd HH:mm:ss.SSS} %-5p %c{1}:%L - %m%n");
                        try {
                            RollingFileAppender appender = new RollingFileAppender(patternLayout, "D://aa.log",true);
                            appender.setMaxFileSize("500KB");
                            appender.setMaxBackupIndex(10);
                            appender.setName("worker");
                            appender.activateOptions();
                            log.addAppender(appender);
                        } catch (IOException e) {
                        }
                    }   
                    //log.warn("LogInjector postProcessBeforeInitialization Log getAppender "+field.getAnnotation(Log.class).clazz());                    

                    field.set(bean, log);
                }
            }
        });
        return bean;
    }
}

代码3:-

if(LOG.getAppender("worker") == null) {
            PatternLayout patternLayout = new PatternLayout();
            patternLayout.setConversionPattern("%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n");
            try {
                RollingFileAppender appender = new RollingFileAppender(patternLayout, "D://aa.log",true);
                appender.setMaxFileSize("500KB");
                appender.setMaxBackupIndex(10);             
                appender.setName("worker");
                appender.activateOptions();
                LOG.addAppender(appender);              
            } catch (IOException e) {
            }
        }

0 个答案:

没有答案