My current build lead has a great idea in theory - construct a custom Log4J appender that takes in Spring-managed beans and uses them to log errors to various other sources than just the standard log file. However, other than creating a singleton initialized at startup with the application context (code in just a moment), I can't seem to think of any other options of retrieving a Spring managed bean in a Log4J appender.
public class SpringSingleton implements ApplicationContextAware {
private static ApplicationContext context;
public SpringSingleton() {
super();
}
public static ApplicationContext getContext() {
return SpringSingleton.context;
}
public void setApplicationContext(ApplicationContext context) {
if(SpringSingleton.context != null) {
throw new IllegalStateException("Context is already set!");
}
SpringSingleton.context = context;
}
}
Ideally, these properties could be set just like beans in Spring via dependency injection - the bean references will never change, no matter how many appenders are initialized. Any ideas?