Reset StatusLogger fallback listener stream on initialization

Update `Log4J2LoggingSystem` so that the `StatusLogger` fallback
listener has its print stream reset on each initialization. This
allows output capture to work with the status listener.

Fixes gh-43578

Co-authored-by: Phillip Webb <phil.webb@broadcom.com>
This commit is contained in:
Dmytro Nosan
2024-12-19 17:25:10 +02:00
committed by Phillip Webb
parent ec75474abf
commit b6b9237f2c
5 changed files with 62 additions and 4 deletions

View File

@@ -0,0 +1,32 @@
/*
* Copyright 2012-2025 the original author or authors.
*
* Licensed 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
*
* https://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 smoketest.structuredlogging.log4j2;
import java.util.Objects;
import org.springframework.boot.json.JsonWriter.Members;
import org.springframework.boot.logging.structured.StructuredLoggingJsonMembersCustomizer;
public class DuplicateJsonMembersCustomizer implements StructuredLoggingJsonMembersCustomizer<Object> {
@Override
public void customize(Members<Object> members) {
members.add("test").as(Objects::toString);
members.add("test").as(Objects::toString);
}
}

View File

@@ -2,3 +2,6 @@ logging.structured.format.console=ecs
#---
spring.config.activate.on-profile=custom
logging.structured.format.console=smoketest.structuredlogging.log4j2.CustomStructuredLogFormatter
#---
spring.config.activate.on-profile=on-error
logging.structured.json.customizer=smoketest.structuredlogging.log4j2.DuplicateJsonMembersCustomizer

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2024 the original author or authors.
* Copyright 2012-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -62,4 +62,10 @@ class SampleLog4j2StructuredLoggingApplicationTests {
assertThat(output).contains("epoch=").contains("msg=\"Starting SampleLog4j2StructuredLoggingApplication");
}
@Test
void shouldCaptureCustomizerError(CapturedOutput output) {
SampleLog4j2StructuredLoggingApplication.main(new String[] { "--spring.profiles.active=on-error" });
assertThat(output).contains("The name 'test' has already been written");
}
}