Replies: 2 comments 2 replies
-
I should clarify, tests where I explicitly create new logger objects work, e.g: @Test
public void testMarkerFilter() throws Exception {
Logger logger = LogManager.getLogger("TestRingBufferAppenderDispatchedByMarker");
final Marker MARK = MarkerManager.getMarker("MARK");
logger.info("On yer mark!");
logger.debug("Don't mind me");
logger.info("Ready, set...");
logger.info(MARK, "GO!");
logger.info("Nothing interesting in here.");
logger.debug("All is fine.");
logger.error("Not even errors are interesting.");
// Ensure all logs are flushed to the appropriate appenders
LogManager.shutdown();
List<LogEvent> events = listAppender.getEvents();
assertEquals(3, events.size());
assertEquals("On yer mark!", events.get(0).getMessage().getFormattedMessage());
assertEquals("Ready, set...", events.get(1).getMessage().getFormattedMessage());
assertEquals("GO!", events.get(2).getMessage().getFormattedMessage());
} However, this does not work in a more typical scenario where it is the application managing the Logger object, not the test code. In this case, as soon as I call LogManager.shutdown(); Subsequent logs no longer seem to work, and this is the only way I found to force synchronisation (dispatch events of an asynchronous logger to be flushed to the underlying appenders). |
Beta Was this translation helpful? Give feedback.
-
This is not a direct response to your question, but I would like to share it anyway. I personally think one shouldn't create an entire
The code is already publicly available in dba68fd. If there is enough interest, I can make it an official part of |
Beta Was this translation helpful? Give feedback.
-
When I try to create a (or fix an existing) unit test that checks for the existence of certain error messages with the LoggerContextRule, as described in the docs, I get the following error:
This happens on:
Dependencies (mvn):
Looking at the Junit docs, it looks like org.junit.rules.TestRule only became available in 4.9. However, after updating Junit to v4.13.2 I still get the same error.
I resorted to creating a custom ListAppender for testing things like custom Log4J appenders, but this only seems to work in that specific tests. Log events do not get written to the List Appender until the logger context is closed, which seems to require the logger to be re-opened. The core issue is likely that log4j2 seems to default to using an Async Logger, even for the most basic test configuration, I have yet to figure out how to make log4j use a synchronous logger instead.
My use-case is fairly straight forward, and I'm sure I'm not the only one that has the use-case: I want to check with Junit if a test that I expect to log, was in fact logged.
The recommended approach documented on the Log4j website seems broken due to Junit dependencies. Coming up with alternative approaches is painful due to many caveats like (often undocumented) lifetimes of objects and implicit asynchronous behaviour.
Could this use-case please be fixed / documented better?
Beta Was this translation helpful? Give feedback.
All reactions