问题描述
我有log4j dailyrolllingfileappender类,其中setfile()方法我需要检查数据库值以决定要使用哪个文件进行登录.
DailyRollingFileAppender class public void setFileName() { isLoginEnabled = authenticationManager.checkLoginLogging(); }
此处'AuthenticationManager'是用于使用Spring依赖注入功能进行数据库调用的类的对象.
spring-beans.xml <bean id="dailyRollingFileAppender" class="com.common.util.DailyRollingFileAppender"> <property name="authenticationManager"> <ref bean="authenticationManager"/> </property> </bean> <bean id="authenticationManager" class="com.security.impl.AuthenticationManagerImpl"> <property name="userService"> <ref bean="userService"/> </property> </bean>
现在,当我启动应用程序log4j时,首先启动log4j,并且由于弹簧beans尚未调用,它会将NullPoInterException扔到方法setFilename()中. 因此,有什么方法可以打电话给'AuthenticationManager.CheckLoginLogging();'从每日fileappender类中,以便当log4j加载时应该能够获得数据库值?
推荐答案
迟到几年,但我希望这对某人有帮助.
我也采用了类似的功能 - 我有一个自定义的应用程序,我想使用自动bean使用我们构建的服务执行一些记录.通过使Appender实现ApplicationContextaware接口,并制作我通常会静态的字段,我可以将弹簧控制的BEAN注入Log4J实例化的Appender实例.
.@Component public class SslErrorSecurityAppender extends AppenderSkeleton implements ApplicationContextAware { private static SecurityLogger securityLogger; @Override protected void append(LoggingEvent event) { securityLogger.log(new SslExceptionSecurityEvent(SecurityEventType.AUTHENTICATION_FAILED, event.getThrowableInformation().getThrowable(), "Unexpected SSL error")); } @Override public boolean requiresLayout() { return false; } @Override public synchronized void close() { this.closed = true; } @Override public void setApplicationContext(ApplicationContext applicationContext) { if (applicationContext.getAutowireCapableBeanFactory().getBean("securityLogger") != null) { securityLogger = (SecurityLogger) applicationContext.getAutowireCapableBeanFactory().getBean("securityLogger"); } } }
问题描述
I have log4j DailyRollingFileAppender class in which setFile() method I need to check database value to decide which file to used for logging.
DailyRollingFileAppender class public void setFileName() { isLoginEnabled = authenticationManager.checkLoginLogging(); }
Here 'authenticationManager' is object of class used to make database call using spring dependency injection feature.
spring-beans.xml <bean id="dailyRollingFileAppender" class="com.common.util.DailyRollingFileAppender"> <property name="authenticationManager"> <ref bean="authenticationManager"/> </property> </bean> <bean id="authenticationManager" class="com.security.impl.AuthenticationManagerImpl"> <property name="userService"> <ref bean="userService"/> </property> </bean>
Now when I start my application log4j gets initiated first and since spring-beans is yet to invoked it throws NullPointerException in method setFileName(). So is there a way I can make call to 'authenticationManager.checkLoginLogging();' from DailyFileAppender class so that when log4j loads it should able to get database value?
推荐答案
A few years late, but I hope this is of help to someone.
I was after similar functionality - I have a custom appender, and i wanted to use an autowired bean to perform some logging using a service we'd built. By making the appender implement the ApplicationContextAware interface, and making the field that i'd normally autowire static, i'm able to inject the spring-controlled bean into the instance of the appender that log4j has instantiated.
@Component public class SslErrorSecurityAppender extends AppenderSkeleton implements ApplicationContextAware { private static SecurityLogger securityLogger; @Override protected void append(LoggingEvent event) { securityLogger.log(new SslExceptionSecurityEvent(SecurityEventType.AUTHENTICATION_FAILED, event.getThrowableInformation().getThrowable(), "Unexpected SSL error")); } @Override public boolean requiresLayout() { return false; } @Override public synchronized void close() { this.closed = true; } @Override public void setApplicationContext(ApplicationContext applicationContext) { if (applicationContext.getAutowireCapableBeanFactory().getBean("securityLogger") != null) { securityLogger = (SecurityLogger) applicationContext.getAutowireCapableBeanFactory().getBean("securityLogger"); } } }