问题描述
我创建了一些自定义记录器,它们的某些级别可以覆盖Log4J2中的自定义记录器.我在 http://logging.apache.org/上遵循了该指南.log4j/2.x/manual/customloglevels.html .
I've created a couple of custom loggers with some levels that override the custom ones in Log4J2. I've followed the guide at http://logging.apache.org/log4j/2.x/manual/customloglevels.html.
我需要创建一些单元测试,以验证事件是否已按照其正确的自定义级别和配置进行注册.
I need to create some unit test to verify that the events are being registered on their correct custom levels and configuration.
我很感谢有关如何开始的任何提示.非常感谢.
I appreciate any hint on how to start. Thanks a lot.
推荐答案
我建议看一下log4j2中的JUnit测试.
I recommend taking a look at the JUnit tests in log4j2.
许多log4j2单元测试都使用FileSender并带有InstantFlush = true,然后读入文件并检查输出中是否存在某些预期的字符串.其他人配置一个(org.apache.logging.log4j.test.appender.)ListAppender(此类位于核心测试jar中),并直接从列表中获取LogEvent对象.
A number of log4j2 unit tests use a FileAppender with immediateFlush=true, then read in the file and check that some expected Strings exist in the output. Others configure a (org.apache.logging.log4j.test.appender.) ListAppender (this class lives in the core test jar) and obtain the LogEvent objects directly from the list.
您可能需要为log4j2 JUnit测试创建一个新进程,以确保先前的某个进程尚未加载其他配置.
You may need to fork a new process for your log4j2 JUnit tests to make sure a different configuration has not already been loaded by some previous process.
这篇关于如何在log4j2中为自定义记录器编写单元测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!