I am working on a stand alone Java application and facing the below log4j warning during start up of my application:
log4j:WARN 找不到记录器 (org.springframework.core.env.StandardEnvironment) 的附加程序。 log4j:WARN 请正确初始化log4j系统。
However, I am able to see all the "spring related" logs after loading the spring context (line 3 in main method below) i.e., the warning comes even before loading the spring context, but after loading the spring context, spring logs are being logged.
我在下面提供我的应用程序 Java 主类和 bean xml:
ApplicationStarter (main Java Class):
public class ApplicationStarter {
@Autowired
private UserInputs userInputs;
@Resource
private volatile BlockingQueue<String> inputsQueue;
public static void main(String[] args) {
GenericXmlApplicationContext context = new GenericXmlApplicationContext();
context.setValidating(false);
context.load("MyProject-Beans.xml");
context.refresh();
MyProjectBean myBeanObj = (MyProjectBean)(context.getBean("myProjectBean"));
myBeanObj.startApplication();
}
}
I have also got the below log4j code at the beginning of "MyProject-Beans.xml" file:
<bean id="log4jInitialization" class=
"org.springframework.beans.factory.config.MethodInvokingFactoryBean">
<property name="targetClass"
value="org.springframework.util.Log4jConfigurer" />
<property name="targetMethod" value="initLogging" />
<property name="arguments">
<list>
<value>file:///{MYPROJECT_HOME}log4j.properties</value>
</list>
</property>
</bean>
My project log4j.properties file:
log4j.category.org.springframework=INFO,MYPROJECT_CONSOLE_LOGS
log4j.category.com.myproject=INFO,MYPROJECT_CONSOLE_LOGS
log4j.appender.MYPROJECT_CONSOLE_LOGS=org.apache.log4j.ConsoleAppender
log4j.appender.MYPROJECT_CONSOLE_LOGS.layout=org.apache.log4j.PatternLayout
log4j.appender.MYPROJECT_CONSOLE_LOGS.layout.ConversionPattern=%d %p
%C{1}:%M():%m\n
Currently I am supressing the warning by adding the below first line in the main() method.
org.apache.log4j.Logger.getLogger("org.springframework").setLevel(Level.OFF);
block 引用>但是,我不喜欢这一行,因为它对应用程序类中的 spring 记录器进行了硬编码。 我想知道是否有更好的方法通过在应用程序外部保留 spring log4j level 属性来消除启动期间的 Spring Log4j 警告。
请帮忙。提前致谢。
最佳答案
此警告告诉我们,运行时在类路径中找不到 log4j.properties。确保当您运行应用程序时,您确实已经掌握了它,而不仅仅是作为某些 IDE 或其他地方的项目的一部分。
并检查 bean 配置中的路径...假设,{MYPROJECT_HOME} 和 log4j.properties 之间没有足够的/
<value>file:///{MYPROJECT_HOME}log4j.properties</value>
如果您确定您的属性位于正确的位置,还有另外 2 种方法可以解决该问题。首先,您可以设置一个 JVM 选项,以显示属性文件的路径:
-Dlog4j.configuration=file:///somepath/log4j.properties
或者你可以这样配置:
BasicConfigurator.configure();
但在这种情况下,您的所有日志记录将仅记录到 System.out,因此这可能不是您想要的。
关于java - 在 Spring 上下文加载之前避免 Log4j 警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32361559/