1919public class LogCapturerWithJulTest {
2020
2121 @ RegisterExtension
22- LogCapturer testLoggerInfoCapturer = LogCapturer .create ().captureForType (LogCapturerWithJulTest .class );
22+ LogCapturer testLoggerInfoCapturer = LogCapturer .create ().captureForType (LoggingObject .class );
2323
2424 @ RegisterExtension
2525 LogCapturer namedLoggerWarnCapturer = LogCapturer .create ().captureForLogger (LOGGER_NAME , Level .WARN );
2626
2727 private static final String LOGGER_NAME = "CUSTOM_LOGGER" ;
2828
29- private Logger testLogger = Logger .getLogger (LogCapturerWithJulTest .class .getName ());
30-
3129 private Logger namedLogger = Logger .getLogger (LOGGER_NAME );
3230
3331 /**
@@ -38,11 +36,15 @@ public class LogCapturerWithJulTest {
3836 * <li>that the namedLogger (by logger name) captures only the WARN level
3937 * and above as specified</li>
4038 * <li>both loggers and their capturers don't affeact each other</li>
39+ * <li>logger is not garbage collected, losing interception</li>
4140 * </ul>
4241 */
4342 @ Test
44- public void test1CaptureMessages () {
45- logEverythingOnce (testLogger );
43+ public void test1CaptureMessages () throws InterruptedException {
44+ System .gc ();
45+ Thread .sleep (50 );
46+
47+ logEverythingOnce (new LoggingObject ().testLogger );
4648 logEverythingOnce (namedLogger );
4749
4850 Assertions .assertEquals (3 , testLoggerInfoCapturer .size (),
@@ -83,4 +85,10 @@ private void logEverythingOnce(Logger logger) {
8385 logger .warning ("Some warning message" );
8486 logger .severe ("Some severe message" );
8587 }
88+
89+ // The logger will not be constructed until the object is. This tests that log messages will be captured
90+ // without allowing the intercepted Logger to be garbage collected.
91+ private static class LoggingObject {
92+ Logger testLogger = Logger .getLogger (LoggingObject .class .getName ());
93+ }
8694}
0 commit comments