Code 2 is injecting Logger instance into LOG field in Code 1. The logs are going to the correct file but the logs are not rolling. However, when using Code 1 with Code 3, logs are rolling. What I need is for Code 1 to work with Code 2. The core logic for appending the RollingFileAppender is the same but I am out of ideas why the log won't roll in the first case. Will you please explain what I am doing wrong and what could be a possible solution.
@Retention(RUNTIME)
@Target(FIELD)
@Documented
public @interface Log {
Class<?> clazz();
}
Code 1:-
@RestController
@RequestMapping(value = "/util/")
public class UtilityController {
@Log(clazz = COScheduler.class)
private static Logger LOG;
}
Code 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;
}
}
Code 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) {
}
}
Aucun commentaire:
Enregistrer un commentaire