Merge branch '2.3.x'

Closes gh-23430
This commit is contained in:
Andy Wilkinson 2020-09-21 15:21:17 +01:00
commit 5a3232d681
4 changed files with 205 additions and 3 deletions

View File

@ -4,8 +4,8 @@
<Property name="LOG_EXCEPTION_CONVERSION_WORD">%xwEx</Property>
<Property name="LOG_LEVEL_PATTERN">%5p</Property>
<Property name="LOG_DATEFORMAT_PATTERN">yyyy-MM-dd HH:mm:ss.SSS</Property>
<Property name="CONSOLE_LOG_PATTERN">%clr{%d{${LOG_DATEFORMAT_PATTERN}}}{faint} %clr{${LOG_LEVEL_PATTERN}} %clr{%pid}{magenta} %clr{---}{faint} %clr{[%15.15t]}{faint} %clr{%-40.40c{1.}}{cyan} %clr{:}{faint} %m%n${sys:LOG_EXCEPTION_CONVERSION_WORD}</Property>
<Property name="FILE_LOG_PATTERN">%d{${LOG_DATEFORMAT_PATTERN}} ${LOG_LEVEL_PATTERN} %pid --- [%t] %-40.40c{1.} : %m%n${sys:LOG_EXCEPTION_CONVERSION_WORD}</Property>
<Property name="CONSOLE_LOG_PATTERN">%clr{%d{${sys:LOG_DATEFORMAT_PATTERN}}}{faint} %clr{${sys:LOG_LEVEL_PATTERN}} %clr{%pid}{magenta} %clr{---}{faint} %clr{[%15.15t]}{faint} %clr{%-40.40c{1.}}{cyan} %clr{:}{faint} %m%n${sys:LOG_EXCEPTION_CONVERSION_WORD}</Property>
<Property name="FILE_LOG_PATTERN">%d{${sys:LOG_DATEFORMAT_PATTERN}} ${sys:LOG_LEVEL_PATTERN} %pid --- [%t] %-40.40c{1.} : %m%n${sys:LOG_EXCEPTION_CONVERSION_WORD}</Property>
</Properties>
<Appenders>
<Console name="Console" target="SYSTEM_OUT" follow="true">

View File

@ -4,7 +4,7 @@
<Property name="LOG_EXCEPTION_CONVERSION_WORD">%xwEx</Property>
<Property name="LOG_LEVEL_PATTERN">%5p</Property>
<Property name="LOG_DATEFORMAT_PATTERN">yyyy-MM-dd HH:mm:ss.SSS</Property>
<Property name="CONSOLE_LOG_PATTERN">%clr{%d{${LOG_DATEFORMAT_PATTERN}}}{faint} %clr{${LOG_LEVEL_PATTERN}} %clr{%pid}{magenta} %clr{---}{faint} %clr{[%15.15t]}{faint} %clr{%-40.40c{1.}}{cyan} %clr{:}{faint} %m%n${sys:LOG_EXCEPTION_CONVERSION_WORD}</Property>
<Property name="CONSOLE_LOG_PATTERN">%clr{%d{${sys:LOG_DATEFORMAT_PATTERN}}}{faint} %clr{${sys:LOG_LEVEL_PATTERN}} %clr{%pid}{magenta} %clr{---}{faint} %clr{[%15.15t]}{faint} %clr{%-40.40c{1.}}{cyan} %clr{:}{faint} %m%n${sys:LOG_EXCEPTION_CONVERSION_WORD}</Property>
<Property name="FILE_LOG_PATTERN">%d{${LOG_DATEFORMAT_PATTERN}} ${LOG_LEVEL_PATTERN} %pid --- [%t] %-40.40c{1.} : %m%n${sys:LOG_EXCEPTION_CONVERSION_WORD}</Property>
</Properties>
<Appenders>

View File

@ -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();
}
}

View File

@ -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";
}
}