注意:尽管抛出的异常是一个空指针,但我相信它不是由于某些随机代码,而是由于一些log4j配置问题。我对log4j尤其是自定义配置不是很精通,所以即使有人可以将我定向到log4j自定义配置的相关示例,也很棒。
在使用log4j异步日志记录写入日志时,出现空指针异常。我们有一个自定义的logger类,它扩展了org.apache.logging.log4j.spi.ExtendedLoggerWrapper和 用于扩展org.apache.logging.log4j.core.config.ConfigurationFactory的cutomloggerconfigurationfactory。 该项目是一个testng项目,已启用并行执行(2个线程),并且每个线程运行一个测试类。
以下是带有上述异常的日志:
2019-06-11 00:33:32 | https://environment.mydomain.com/ | machine-name.mydomain.com | Thread::mydomain.test2 | 287BF9617A3FFB12BD0286ABD34F3D22 | INFO | | com.mydomain.bootupClass.onTrigger:193 | ******* Starts here ********
2019-06-11 00:33:32 | https://environment.mydomain.com/ | machine-name.mydomain.com | Thread::mydomain.test2 | 287BF9617A3FFB12BD0286ABD34F3D22 | INFO | | com.mydomain.bootupClass.onTrigger:212 | setting up environment
2019-06-11 00:33:33 | https://environment.mydomain.com/ | machine-name.mydomain.com | Thread::mydomain.test2 | 287BF9617A3FFB12BD0286ABD34F3D22 | INFO | initilizing | com.mydomain.CustomInter.makeCon:43 | AUT address is : https://environment.mydomain.com/
2019-06-11 00:33:32 | https://environment.mydomain.com/ | machine-name.mydomain.com | Thread::mydomain.test1 | 9CA36EEF6441726D1862176A8EBE907B | INFO | | com.mydomain.bootupClass.onTrigger:193 | ******* Starts here ********
2019-06-11 00:33:32 | https://environment.mydomain.com/ | machine-name.mydomain.com | Thread::mydomain.test1 | 9CA36EEF6441726D1862176A8EBE907B | INFO | | com.mydomain.bootupClass.onTrigger:212 | setting up environment
java.lang.NullPointerException
at org.apache.logging.log4j.core.async.AsyncLoggerConfigDisruptor.tryEnqueue(AsyncLoggerConfigDisruptor.java:345)
at org.apache.logging.log4j.core.async.AsyncLoggerConfig.callAppenders(AsyncLoggerConfig.java:93)
at org.apache.logging.log4j.core.config.LoggerConfig.processLogEvent(LoggerConfig.java:432)
at org.apache.logging.log4j.core.config.LoggerConfig.log(LoggerConfig.java:416)
at org.apache.logging.log4j.core.config.LoggerConfig.log(LoggerConfig.java:402)
at org.apache.logging.log4j.core.config.AwaitCompletionReliabilityStrategy.log(AwaitCompletionReliabilityStrategy.java:63)
at org.apache.logging.log4j.core.Logger.logMessage(Logger.java:146)
at org.apache.logging.log4j.spi.ExtendedLoggerWrapper.logMessage(ExtendedLoggerWrapper.java:217)
at org.apache.logging.log4j.spi.AbstractLogger.logMessageSafely(AbstractLogger.java:2091)
at org.apache.logging.log4j.spi.AbstractLogger.logMessage(AbstractLogger.java:1988)
at org.apache.logging.log4j.spi.AbstractLogger.logIfEnabled(AbstractLogger.java:1960)
at org.apache.logging.log4j.spi.AbstractLogger.info(AbstractLogger.java:1297)
at com.mydomain.CustomInter.makeCon(CustomInter.java:43)
at com.mydomain.Combo.init(Combo.java:55)
at com.mydomain.SomeOtherClass.init(SomeOtherClass:109)
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.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:86)
at org.testng.internal.Invoker.invokeConfigurationMethod(Invoker.java:514)
at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:215)
at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:142)
at org.testng.TestRunner.beforeRun(TestRunner.java:656)
at org.testng.TestRunner.run(TestRunner.java:624)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:366)
at org.testng.SuiteRunner.access$000(SuiteRunner.java:39)
at org.testng.SuiteRunner$SuiteWorker.run(SuiteRunner.java:400)
at org.testng.internal.thread.ThreadUtil$2.call(ThreadUtil.java:64)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
在上面的堆栈跟踪中,异常似乎来自以下行:com.mydomain.CustomInter.makeCon(CustomInter.java:43)
makeCon方法第43行的代码段:
Driver.getLogger().info("AUT address is : {}", appUrl));
getLogger是一个静态方法。即使我假设getLogger()返回null,堆栈跟踪也应该在此行本身处停止。但是,log4j库中也显示了跟踪。