Skip to content

disableConsoleOutput() in a @SpringBootTest: "Attempted to append to non started appender" #208

Description

@DamianFekete

Describe the bug
A warning is printed to the console if disableConsoleOutput() is called in a @SpringBootTest test.

15:06:51,602 |-WARN in ch.qos.logback.core.ConsoleAppender[CONSOLE] - Attempted to append to non started appender [CONSOLE].

I have added it as bug, but only because I haven't seen any discussion on this topic.
I am not sure what the solution would be (maybe replacing the listener instead of closing it, I don't know if this is even possible).

The warning is added by ch.qos.logback.core.UnsynchronizedAppenderBase.
Image

The warning is printed here (because a listener is present in BasicStatusManager#fireStatusAddEvent, listener added by Spring Boot):
Image

The StatusListener is added here:

https://github.com/spring-projects/spring-boot/blob/9ab0d28cebb925661c0c1b7e2198cfc31d2d8ef3/core/spring-boot/src/main/java/org/springframework/boot/logging/logback/SystemStatusListener.java#L89

To Reproduce
Run the test test while keeping the line removeSystemListener(); commented.
If you enable the call removeSystemListener(); the warning goes away.

@SpringBootTest
public class TestCaptor {

    private static final Logger log = LoggerFactory.getLogger(TestCaptor.class);
    private LogCaptor logCaptor;

    @BeforeEach
    void setUp() {
        logCaptor = LogCaptor.forClass(TestCaptor.class);
        logCaptor.disableConsoleOutput();
//        removeSystemListener();
    }

    @AfterEach
    void tearDown() {
        logCaptor.close();
    }

    @Test
    void test() {
        log.info("Test abc");
    }

    private void removeSystemListener() {
        var logger = ((ch.qos.logback.classic.Logger) LoggerFactory.getLogger(this.getClass()));
        var sm = logger.getLoggerContext().getStatusManager();
        var listeners = sm.getCopyOfStatusListenerList();
        for (var listener : listeners) {
            if (listener instanceof OnConsoleStatusListener) {
                sm.remove(listener);
            }
        }
    }
}

Expected behavior
No warning should be visible on the console.

Environmental Data:

  • Java Version 21
  • Maven Version 3.9.12
  • OS Linux
  • Spring Boot 4.0.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions