我的类LoggerInitializer
具有方法:
public static void initLogger() {
try {
LogManager
.getLogManager()
.readConfiguration(
Service.class.getResourceAsStream("/logging.properties"));
} catch (IOException e) {
System.err.println("Could not setup logger configuration: " + e.toString());
}
}
当我在具有方法
的类中开始测试时@BeforeClass
public static void initEnvironment() {
LoggerInitializer.initLogger();
}
遇到错误
java.lang.NullPointerException
at java.util.Properties$LineReader.readLine(Properties.java:434)
at java.util.Properties.load0(Properties.java:353)
at java.util.Properties.load(Properties.java:341)
at java.util.logging.LogManager.readConfiguration(LogManager.java:1409)
at com.netcracker.edu.inventory.LoggerInitializer.initLogger(LoggerInitializer.java:23)
at com.netcracker.edu.inventory.model.impl.BatteryTest.initEnvironment(BatteryTest.java:18)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
这些错误的原因可能是什么?如何解决此问题?
答案 0 :(得分:0)
您的文件位于com
包中,因此应该为
Service.class.getResourceAsStream("/com/logging.properties")
更改该文件或将该文件移到更高一级。此外,应将它(不是必须的,但是一种很好的做法)放置在资源目录中,这样它就不会与源代码混合在一起(但是在生成的jar中效果是相同的)。
答案 1 :(得分:0)
在名称为 Log4j.properties 的文件中粘贴以下代码。
# DEFAULT FILE OUTPUT IS IN USER'S HOME DIRECTORY.
log4j.rootLogger=DEBUG, Appender1,Appender2
log4j.appender.Appender1=org.apache.log4j.ConsoleAppender
log4j.appender.Appender1.layout=org.apache.log4j.PatternLayout
log4j.appender.Appender1.layout.ConversionPattern=%-7p %d [%t] %c %x - %m%n
log4j.appender.Appender2=org.apache.log4j.FileAppender
log4j.appender.Appender2.File=log.txt
log4j.appender.Appender2.layout=org.apache.log4j.PatternLayout
log4j.appender.Appender2.layout.ConversionPattern=%-7p %d [%t] %c %x - %m%n
为log4j添加依赖项
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
将其导入您的班级
import org.apache.log4j.Logger;
在您的班级中创建记录器
Logger logger = Logger.getLogger(className.class);
logger.info("Http Image is converted into Byte Array");