tree: af82ec343e0edb20441177807b53306732db009e [path history] [tgz]
  1. bad/
  2. customplugin/
  3. LOG4J2-1100/
  4. META-INF/
  5. org/
  6. s p a c e s/
  7. witness/
  8. AsyncLoggerConfigAutoFlushTest.xml
  9. AsyncLoggerConfigTest.xml
  10. AsyncLoggerConfigTest2.xml
  11. AsyncLoggerLocationTest.xml
  12. AsyncLoggersWithAsyncAppenderTest.xml
  13. AsyncLoggersWithAsyncLoggerConfigTest.xml
  14. AsyncLoggerTest.xml
  15. AsyncLoggerThreadContextTest.xml
  16. AsyncLoggerTimestampMessageTest.xml
  17. ContextMapLookupTest.xml
  18. FlumeFuncTest.xml
  19. JeroMqAppenderTest.xml
  20. JmsAppenderTest.xml
  21. JsonCompleteFileAppenderTest.xml
  22. KafkaAppenderTest.xml
  23. log4j+config+with+plus+characters.xml
  24. log4j-advertiser.xml
  25. log4j-asynch-no-location.xml
  26. log4j-asynch.xml
  27. log4j-burst.xml
  28. log4j-collectionLogging.xml
  29. log4j-config.json
  30. log4j-customLevel.xml
  31. log4j-customLevels.xml
  32. log4j-customLevelsOverride.xml
  33. log4j-customLevelsWithFilters.xml
  34. log4j-date.xml
  35. log4j-deadlock.xml
  36. log4j-failover.xml
  37. log4j-fatalOnly.xml
  38. log4j-filetest.xml
  39. log4j-jira965.xml
  40. log4j-Level.xml
  41. log4j-list.xml
  42. log4j-loggerprops.xml
  43. log4j-lookup-main.xml
  44. log4j-lookup.xml
  45. log4j-marker-lookup.yaml
  46. log4j-props.xml
  47. log4j-props1.xml
  48. log4j-reference-level.json
  49. log4j-reference-level.xml
  50. log4j-replace.xml
  51. log4j-rewrite.xml
  52. log4j-rolling-bzip2.xml
  53. log4j-rolling-deflate.xml
  54. log4j-rolling-gz.xml
  55. log4j-rolling-pack200.xml
  56. log4j-rolling-xy.xml
  57. log4j-rolling-zip.xml
  58. log4j-rolling.properties
  59. log4j-rolling2.xml
  60. log4j-rolling3.xml
  61. log4j-rolling4.xml
  62. log4j-rootthrowablefilter.xml
  63. log4j-routing-by-jndi.xml
  64. log4j-routing.json
  65. log4j-routing.properties
  66. log4j-routing.xml
  67. log4j-routing2.json
  68. log4j-routing3.xml
  69. log4j-socket.xml
  70. log4j-socket2.xml
  71. log4j-strict1.xml
  72. log4j-style.xml
  73. log4j-test1.json
  74. log4j-test1.xml
  75. log4j-test1.yaml
  76. log4j-test2.xml
  77. log4j-test3.xml
  78. log4j-threaded.xml
  79. log4j-throwable.xml
  80. log4j-throwablefilter.xml
  81. log4j-xinclude-appenders.xml
  82. log4j-xinclude-loggers.xml
  83. log4j-xinclude.xml
  84. log4j.dtd
  85. log4j12-perf.xml
  86. log4j2-1002.xml
  87. log4j2-180.xml
  88. log4j2-272.xml
  89. log4j2-319.xml
  90. LOG4J2-739.xml
  91. log4j2-744.xml
  92. LOG4J2-807.xml
  93. log4j2-962.xml
  94. log4j2-calling-class.xml
  95. log4j2-config.xml
  96. log4j2-console-default-suppressed-throwable.xml
  97. log4j2-console-highlight-default.xml
  98. log4j2-console-highlight-logback.xml
  99. log4j2-console-highlight.xml
  100. log4j2-console-noConsoleNoAnsi.xml
  101. log4j2-console-progress.xml
  102. log4j2-console-style-ansi.xml
  103. log4j2-console-style-name-ansi.xml
  104. log4j2-console-style-no-ansi.xml
  105. log4j2-console.xml
  106. log4j2-dynamicfilter.xml
  107. log4j2-mapfilter.xml
  108. log4j2-perf-filter.xml
  109. log4j2-perf.xml
  110. log4j2-properties.properties
  111. log4j2-sdfilter.xml
  112. logback-flume.xml
  113. logback-perf-filter.xml
  114. logback-perf.xml
  115. logback-subst.xml
  116. MemoryMappedFileAppenderLocationTest.xml
  117. MemoryMappedFileAppenderRemapTest.xml
  118. MemoryMappedFileAppenderTest.xml
  119. missingRootLogger.xml
  120. NanoTimeToFileTest.xml
  121. perf-log4j12-async.xml
  122. perf-log4j12.xml
  123. perf-logback-async.xml
  124. perf-logback.xml
  125. perf1syncFastFile.xml
  126. perf1syncFile.xml
  127. perf2syncRollFastFile.xml
  128. perf2syncRollFile.xml
  129. perf3PlainNoLoc.xml
  130. perf4PlainLocation.xml
  131. perf5AsyncApndNoLoc.xml
  132. perf6AsyncApndLoc.xml
  133. perf7MixedNoLoc.xml
  134. perf8MixedLoc.xml
  135. perf9MMapNoLoc.xml
  136. RandomAccessFileAppenderLocationTest.xml
  137. RandomAccessFileAppenderTest.xml
  138. README.md
  139. reconfiguration-deadlock.xml
  140. RollingRandomAccessFileAppenderHeaderFooterTest.xml
  141. RollingRandomAccessFileAppenderLocationTest.xml
  142. RollingRandomAccessFileAppenderTest.xml
  143. rollover-test.xml
  144. serializedEvent.dat
  145. ShutdownCallbackRegistryTest.xml
  146. xml-events.xml
  147. XmlCompactFileAppenderTest.xml
  148. XmlCompactFileAppenderValidationTest.xml
  149. XmlCompactFileAsyncAppenderValidationTest.xml
  150. XmlCompleteFileAppenderTest.xml
  151. XmlFileAppenderTest.xml
  152. XmlRandomAccessFileAppenderTest.xml
log4j-core/src/test/resources/README.md

This directory is mainly for storing configuration files used in various unit tests. As such, there are a few things provided for your convenience in writing and maintaining unit tests.

Running Unit Tests

First, make sure you've recently run mvn clean install in log4j-api to get an up to date dependency. Then you can run mvn test to run unit tests, or run mvn verify to run both unit tests and integration/performance tests.

Test Plugins

Included are various Log4j plugins that are only useful for writing tests. These test plugins are automatically made available to test classes. For instance, to use the ListAppender:

<Configuration>
  <Appenders>
    <List name="List"/>
  </Appenders>
  <Loggers>
    <Logger name="org.apache.logging.log4j" level="debug">
      <AppenderRef ref="List"/>
    </Logger>
  </Loggers>
</Configuration>

Note that if you don't specify a layout for a ListAppender, your log messages will be stored in a list of LogEvents. If you use a SerializedLayout, your log messages will be stored in a list of byte arrays. If you specify any other type of layout, your log messages will be stored in a list of strings. For more details, check out the class org.apache.logging.log4j.test.appender.ListAppender.

Specifying Configuration Files in JUnit Tests

Added in JUnit 4.9, the concept of test fixtures (i.e., the @Before, @After, etc. methods) has been expanded into the concept of test rules. A test rule is a reusable test fixture such as the TemporaryFolder JUnit rule for creating temporary directories and files on a test-by-test (or suite) basis. To use a test rule, you need to use the @Rule or @ClassRule annotation to get a method-level or class-level test fixture respectively. For instance, suppose your test class uses the file named MyTestConfig.xml in this directory. Then you can use the following rule in your test class:

@Rule
public InitialLoggerContext init = new InitialLoggerContext("MyTestConfig.xml");

@Test
public void testSomeAwesomeFeature() {
    final LoggerContext ctx = init.getContext();
    final Logger logger = init.getLogger("org.apache.logging.log4j.my.awesome.test.logger");
    final Configuration cfg = init.getConfiguration();
    final ListAppender app = init.getListAppender("List");
    logger.warn("Test message");
    final List<LogEvent> events = app.getEvents();
    // etc.
}

Using this rule will automatically create a new LoggerContext using the specified configuration file for you to retrieve via the getContext() method shown above. After the method finishes (or if you use @ClassRule and make the field static), the LoggerContext is automatically stopped. No longer do you need to set any system properties, reset the StatusLogger configuration, and all that other fun boilerplate code.

Cleaning Up Test Log Files

The CleanFiles rule is also available to automatically delete a list of files after every test.

@Rule
public CleanFiles files = new CleanFiles("target/file1.log", "target/file2.log", "more files");

You can specify either a list of strings or a list of Files.

If you have any questions about writing unit tests, feel free to send an email to the dev mailing list, or check out the JUnit documentation over at junit.org.

Specifying Test Categories

If your test is something more than just a unit test, it's usually a good idea to add a JUnit category to it. This can be done at the class or method level:

@Category(PerformanceTests.class)
@Test
public void testRandomAccessFileLogging() {
    // ...
}

Various pre-defined categories are defined in org.apache.logging.log4j.categories in log4j-core test. If you only want to run your test as part of the maven-failsafe-plugin integration tests phase, then simply name your test FooIT instead of FooTest for automatic configuration.