Browse Source
Previously LOG_LEVEL_PATTERN and LOG_DATEFORMAT_PATTERN were not consumed as system properties in log4j2.xml and log4j2-file.xml. As a result, the logging.pattern.level and logging.pattern.dateformat configuration properties, which are translated into the LOG_LEVEL_PATTERN and LOG_DATEFORMAT_PATTERN system properties respectively had no effect. This commit updates the log4j2.xml and log4j2-file.xml config files to consume LOG_LEVEL_PATTERN and LOG_DATEFORMAT_PATTERN as system properties. When the system property is not set, the configuation falls back to the default values specified in the config files. Tests for both log4j2.xml and log4j2-file.xml to verify the behaviour have also bean added. Fixes gh-22983pull/23445/head
4 changed files with 205 additions and 3 deletions
@ -0,0 +1,92 @@
@@ -0,0 +1,92 @@
|
||||
/* |
||||
* Copyright 2012-2020 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 org.springframework.boot.logging.log4j2; |
||||
|
||||
import java.io.File; |
||||
|
||||
import org.apache.logging.log4j.core.config.Configuration; |
||||
import org.apache.logging.log4j.core.layout.PatternLayout; |
||||
import org.junit.jupiter.api.AfterEach; |
||||
import org.junit.jupiter.api.BeforeEach; |
||||
import org.junit.jupiter.api.Test; |
||||
import org.junit.jupiter.api.io.TempDir; |
||||
|
||||
import org.springframework.boot.logging.LoggingSystemProperties; |
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat; |
||||
|
||||
/** |
||||
* Tests for {@code log4j2-file.xml}. |
||||
* |
||||
* @author Andy Wilkinson |
||||
*/ |
||||
class Log4j2FileXmlTests extends Log4j2XmlTests { |
||||
|
||||
@BeforeEach |
||||
void configureLogFile(@TempDir File temp) { |
||||
System.setProperty(LoggingSystemProperties.LOG_FILE, new File(temp, "test.log").getAbsolutePath()); |
||||
} |
||||
|
||||
@AfterEach |
||||
void clearLogFile() { |
||||
System.clearProperty(LoggingSystemProperties.LOG_FILE); |
||||
} |
||||
|
||||
@Test |
||||
void whenLogExceptionConversionWordIsNotConfiguredThenFileAppenderUsesDefault() { |
||||
assertThat(fileAppenderPattern()).contains("%xwEx"); |
||||
} |
||||
|
||||
@Test |
||||
void whenLogExceptionConversionWordIsSetThenFileAppenderUsesIt() { |
||||
withSystemProperty(LoggingSystemProperties.EXCEPTION_CONVERSION_WORD, "custom", |
||||
() -> assertThat(fileAppenderPattern()).contains("custom")); |
||||
} |
||||
|
||||
@Test |
||||
void whenLogLevelPatternIsNotConfiguredThenFileAppenderUsesDefault() { |
||||
assertThat(fileAppenderPattern()).contains("%5p"); |
||||
} |
||||
|
||||
@Test |
||||
void whenLogLevelPatternIsSetThenFileAppenderUsesIt() { |
||||
withSystemProperty(LoggingSystemProperties.LOG_LEVEL_PATTERN, "custom", |
||||
() -> assertThat(fileAppenderPattern()).contains("custom")); |
||||
} |
||||
|
||||
@Test |
||||
void whenLogLDateformatPatternIsNotConfiguredThenFileAppenderUsesDefault() { |
||||
assertThat(fileAppenderPattern()).contains("yyyy-MM-dd HH:mm:ss.SSS"); |
||||
} |
||||
|
||||
@Test |
||||
void whenLogDateformatPatternIsSetThenFileAppenderUsesIt() { |
||||
withSystemProperty(LoggingSystemProperties.LOG_DATEFORMAT_PATTERN, "custom", |
||||
() -> assertThat(fileAppenderPattern()).contains("custom")); |
||||
} |
||||
|
||||
@Override |
||||
protected String getConfigFileName() { |
||||
return "log4j2-file.xml"; |
||||
} |
||||
|
||||
private String fileAppenderPattern() { |
||||
Configuration configuration = initializeConfiguration(); |
||||
return ((PatternLayout) configuration.getAppender("File").getLayout()).getConversionPattern(); |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,110 @@
@@ -0,0 +1,110 @@
|
||||
/* |
||||
* Copyright 2012-2020 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 org.springframework.boot.logging.log4j2; |
||||
|
||||
import java.io.IOException; |
||||
import java.io.InputStream; |
||||
|
||||
import org.apache.logging.log4j.core.LoggerContext; |
||||
import org.apache.logging.log4j.core.config.Configuration; |
||||
import org.apache.logging.log4j.core.config.ConfigurationFactory; |
||||
import org.apache.logging.log4j.core.config.ConfigurationSource; |
||||
import org.apache.logging.log4j.core.layout.PatternLayout; |
||||
import org.junit.jupiter.api.Test; |
||||
|
||||
import org.springframework.boot.logging.LoggingSystemProperties; |
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat; |
||||
|
||||
/** |
||||
* Tests for {@code log4j2.xml}. |
||||
* |
||||
* @author Andy Wilkinson |
||||
*/ |
||||
class Log4j2XmlTests { |
||||
|
||||
@Test |
||||
void whenLogExceptionConversionWordIsNotConfiguredThenConsoleUsesDefault() { |
||||
assertThat(consolePattern()).contains("%xwEx"); |
||||
} |
||||
|
||||
@Test |
||||
void whenLogExceptionConversionWordIsSetThenConsoleUsesIt() { |
||||
withSystemProperty(LoggingSystemProperties.EXCEPTION_CONVERSION_WORD, "custom", |
||||
() -> assertThat(consolePattern()).contains("custom")); |
||||
} |
||||
|
||||
@Test |
||||
void whenLogLevelPatternIsNotConfiguredThenConsoleUsesDefault() { |
||||
assertThat(consolePattern()).contains("%5p"); |
||||
} |
||||
|
||||
@Test |
||||
void whenLogLevelPatternIsSetThenConsoleUsesIt() { |
||||
withSystemProperty(LoggingSystemProperties.LOG_LEVEL_PATTERN, "custom", |
||||
() -> assertThat(consolePattern()).contains("custom")); |
||||
} |
||||
|
||||
@Test |
||||
void whenLogLDateformatPatternIsNotConfiguredThenConsoleUsesDefault() { |
||||
assertThat(consolePattern()).contains("yyyy-MM-dd HH:mm:ss.SSS"); |
||||
} |
||||
|
||||
@Test |
||||
void whenLogDateformatPatternIsSetThenConsoleUsesIt() { |
||||
withSystemProperty(LoggingSystemProperties.LOG_DATEFORMAT_PATTERN, "custom", |
||||
() -> assertThat(consolePattern()).contains("custom")); |
||||
} |
||||
|
||||
protected void withSystemProperty(String name, String value, Runnable action) { |
||||
String previous = System.setProperty(name, value); |
||||
action.run(); |
||||
if (previous == null) { |
||||
System.clearProperty(name); |
||||
} |
||||
else { |
||||
System.setProperty(name, previous); |
||||
} |
||||
} |
||||
|
||||
private String consolePattern() { |
||||
Configuration configuration = initializeConfiguration(); |
||||
return ((PatternLayout) configuration.getAppender("Console").getLayout()).getConversionPattern(); |
||||
} |
||||
|
||||
protected Configuration initializeConfiguration() { |
||||
LoggerContext context = new LoggerContext("test"); |
||||
Configuration configuration = ConfigurationFactory.getInstance().getConfiguration(context, |
||||
configurationSource()); |
||||
configuration.initialize(); |
||||
return configuration; |
||||
} |
||||
|
||||
private ConfigurationSource configurationSource() { |
||||
try (InputStream in = getClass().getResourceAsStream(getConfigFileName())) { |
||||
return new ConfigurationSource(in); |
||||
} |
||||
catch (IOException ex) { |
||||
throw new RuntimeException(ex); |
||||
} |
||||
} |
||||
|
||||
protected String getConfigFileName() { |
||||
return "log4j2.xml"; |
||||
} |
||||
|
||||
} |
||||
Loading…
Reference in new issue