diff --git a/log4j-api-test/src/test/java/org/apache/logging/log4j/status/StatusLoggerTest.java b/log4j-api-test/src/test/java/org/apache/logging/log4j/status/StatusLoggerTest.java new file mode 100644 index 00000000000..85921e8071a --- /dev/null +++ b/log4j-api-test/src/test/java/org/apache/logging/log4j/status/StatusLoggerTest.java @@ -0,0 +1,56 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to you under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.logging.log4j.status; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.assertEquals; + +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +class StatusLoggerTest { + private final PrintStream origOut = System.out; + private final PrintStream origErr = System.err; + private ByteArrayOutputStream outBuf; + private ByteArrayOutputStream errBuf; + + @BeforeEach + void setupStreams() { + outBuf = new ByteArrayOutputStream(); + errBuf = new ByteArrayOutputStream(); + System.setOut(new PrintStream(outBuf)); + System.setErr(new PrintStream(errBuf)); + } + + @AfterEach + void resetStreams() { + System.setOut(origOut); + System.setErr(origErr); + } + + @Test + void status_logger_writes_to_stderr_by_default() { + StatusLogger statusLogger = new StatusLogger(); + statusLogger.error("Test message"); + + assertEquals("", outBuf.toString()); + assertThat(errBuf.toString()).contains("Test message"); + } +} diff --git a/log4j-api/src/main/java/org/apache/logging/log4j/status/StatusLogger.java b/log4j-api/src/main/java/org/apache/logging/log4j/status/StatusLogger.java index 6aec303cba4..699dc66c8a7 100644 --- a/log4j-api/src/main/java/org/apache/logging/log4j/status/StatusLogger.java +++ b/log4j-api/src/main/java/org/apache/logging/log4j/status/StatusLogger.java @@ -531,7 +531,7 @@ private static final class InstanceHolder { StatusLogger.class.getSimpleName(), ParameterizedNoReferenceMessageFactory.INSTANCE, Config.getInstance(), - new StatusConsoleListener(requireNonNull(Config.getInstance().fallbackListenerLevel))); + new StatusConsoleListener(requireNonNull(Config.getInstance().fallbackListenerLevel), System.err)); } /** diff --git a/src/changelog/.2.x.x/3665_fix_StatusLogger_writing_to_stdout.xml b/src/changelog/.2.x.x/3665_fix_StatusLogger_writing_to_stdout.xml new file mode 100644 index 00000000000..5d79432d459 --- /dev/null +++ b/src/changelog/.2.x.x/3665_fix_StatusLogger_writing_to_stdout.xml @@ -0,0 +1,10 @@ + + + + + StatusLogger now writes to standard error by default. This fixes a regression introduced in 2.23.0. + +