Skip to content

Allow to disable structured logging for Log4j2 #45508

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
<Appenders>
<Console name="Console" target="SYSTEM_OUT" follow="true">
<Select>
<SystemPropertyArbiter propertyName="CONSOLE_LOG_STRUCTURED_FORMAT" propertyValue="">
<PatternLayout pattern="${sys:CONSOLE_LOG_PATTERN}" charset="${sys:CONSOLE_LOG_CHARSET}"/>
</SystemPropertyArbiter>
<SystemPropertyArbiter propertyName="CONSOLE_LOG_STRUCTURED_FORMAT">
<StructuredLogLayout format="${sys:CONSOLE_LOG_STRUCTURED_FORMAT}" charset="${sys:CONSOLE_LOG_CHARSET}"/>
</SystemPropertyArbiter>
Expand All @@ -23,6 +26,9 @@
</Console>
<RollingFile name="File" fileName="${sys:LOG_FILE}" filePattern="${sys:LOG_PATH}/$${date:yyyy-MM}/app-%d{yyyy-MM-dd-HH}-%i.log.gz">
<Select>
<SystemPropertyArbiter propertyName="FILE_LOG_STRUCTURED_FORMAT" propertyValue="">
<PatternLayout pattern="${sys:FILE_LOG_PATTERN}" charset="${sys:FILE_LOG_CHARSET}"/>
</SystemPropertyArbiter>
<SystemPropertyArbiter propertyName="FILE_LOG_STRUCTURED_FORMAT">
<StructuredLogLayout format="${sys:FILE_LOG_STRUCTURED_FORMAT}" charset="${sys:FILE_LOG_CHARSET}"/>
</SystemPropertyArbiter>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
<Appenders>
<Console name="Console" target="SYSTEM_OUT" follow="true">
<Select>
<SystemPropertyArbiter propertyName="CONSOLE_LOG_STRUCTURED_FORMAT" propertyValue="">
<PatternLayout pattern="${sys:CONSOLE_LOG_PATTERN}" charset="${sys:CONSOLE_LOG_CHARSET}"/>
</SystemPropertyArbiter>
<SystemPropertyArbiter propertyName="CONSOLE_LOG_STRUCTURED_FORMAT">
<StructuredLogLayout format="${sys:CONSOLE_LOG_STRUCTURED_FORMAT}" charset="${sys:CONSOLE_LOG_CHARSET}"/>
</SystemPropertyArbiter>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2023 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.
Expand Down Expand Up @@ -32,6 +32,7 @@
*
* @author Andy Wilkinson
* @author Scott Frederick
* @author Yanming Zhou
*/
class Log4j2FileXmlTests extends Log4j2XmlTests {

Expand Down Expand Up @@ -80,6 +81,12 @@ void whenLogDateformatPatternIsSetThenFileAppenderUsesIt() {
() -> assertThat(fileAppenderPattern()).contains("dd-MM-yyyy"));
}

@Test
void whenFileLogStructuredFormatIsEmptyThenFallbackToPatternLayout() {
withSystemProperty(LoggingSystemProperty.FILE_STRUCTURED_FORMAT.getEnvironmentVariableName(), "",
() -> assertThat(fileAppenderPattern()).contains("yyyy-MM-dd'T'HH:mm:ss.SSSXXX"));
}

@Override
protected String getConfigFileName() {
return "log4j2-file.xml";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2023 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.
Expand Down Expand Up @@ -36,6 +36,7 @@
*
* @author Andy Wilkinson
* @author Scott Frederick
* @author Yanming Zhou
*/
class Log4j2XmlTests {

Expand Down Expand Up @@ -79,6 +80,12 @@ void whenLogDateformatPatternIsSetThenConsoleUsesIt() {
() -> assertThat(consolePattern()).contains("dd-MM-yyyy"));
}

@Test
void whenConsoleStructuredFormatIsEmptyThenFallbackToPatternLayout() {
withSystemProperty(LoggingSystemProperty.CONSOLE_STRUCTURED_FORMAT.getEnvironmentVariableName(), "",
() -> assertThat(consolePattern()).contains("yyyy-MM-dd'T'HH:mm:ss.SSSXXX"));
}

protected void withSystemProperty(String name, String value, Runnable action) {
String previous = System.setProperty(name, value);
action.run();
Expand Down
Loading