1

我正在尝试在我的春季示例中包含 log4j。但是,当我尝试

private static final Logger log = Logger.getLogger(MainApp.class);

我得到了错误

java.lang.ExceptionInInitializerError
Caused by: java.lang.NullPointerException
at org.apache.logging.log4j.LogManager.getContext(LogManager.java:203)
at org.apache.log4j.Logger$PrivateManager.getContext(Logger.java:61)
at org.apache.log4j.Logger.getLogger(Logger.java:43)
at core.MainApp.<clinit>(MainApp.java:11)
Exception in thread "main" 

MainApp.java 文件:

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import org.apache.log4j.Logger;


public class MainApp {

private static final Logger log = Logger.getLogger(MainApp.class);
public static void main(String[] args) {




      ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");


       log.info("Going to create HelloWord Obj");

       HelloWorld obj = (HelloWorld) context.getBean("helloWorld");

       obj.getMessage();

       log.info("Exiting the program");

}
}

我还将 log4j.properties 文件保存在我的类路径中。我想我也初始化了我的 log4j。我的 beans.xml:

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

<bean id="helloWorld" class="core.HelloWorld">
   <property name="message" value="Hello World!"/>
</bean>

<bean id="log4jInitializer">
<property name="staticMethod">
    <value>org.springframework.util.Log4jConfigurer.initLogging</value>
</property>
<property name="arguments">
     <list>
        <value>log4j.properties</value>
     </list>
</property>
</bean>

</beans>

我的 log4j.properties 文件:

# Direct log messages to a log file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=D:\\loging.log
log4j.appender.file.MaxFileSize=1MB
log4j.appender.file.MaxBackupIndex=1
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n

# Direct log messages to stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n

# Root logger option
log4j.rootLogger=debug, file, stdout

有谁知道是什么问题?问候!

4

0 回答 0