|
| 1 | +/* |
| 2 | + * Copyright 2012-2020 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.boot.logging.log4j2; |
| 18 | + |
| 19 | +import java.io.IOException; |
| 20 | +import java.io.InputStream; |
| 21 | + |
| 22 | +import org.apache.logging.log4j.core.LoggerContext; |
| 23 | +import org.apache.logging.log4j.core.config.Configuration; |
| 24 | +import org.apache.logging.log4j.core.config.ConfigurationFactory; |
| 25 | +import org.apache.logging.log4j.core.config.ConfigurationSource; |
| 26 | +import org.apache.logging.log4j.core.layout.PatternLayout; |
| 27 | +import org.junit.jupiter.api.Test; |
| 28 | + |
| 29 | +import org.springframework.boot.logging.LoggingSystemProperties; |
| 30 | + |
| 31 | +import static org.assertj.core.api.Assertions.assertThat; |
| 32 | + |
| 33 | +/** |
| 34 | + * Tests for {@code log4j2.xml}. |
| 35 | + * |
| 36 | + * @author Andy Wilkinson |
| 37 | + */ |
| 38 | +class Log4j2XmlTests { |
| 39 | + |
| 40 | + @Test |
| 41 | + void whenLogExceptionConversionWordIsNotConfiguredThenConsoleUsesDefault() { |
| 42 | + assertThat(consolePattern()).contains("%xwEx"); |
| 43 | + } |
| 44 | + |
| 45 | + @Test |
| 46 | + void whenLogExceptionConversionWordIsSetThenConsoleUsesIt() { |
| 47 | + withSystemProperty(LoggingSystemProperties.EXCEPTION_CONVERSION_WORD, "custom", |
| 48 | + () -> assertThat(consolePattern()).contains("custom")); |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + void whenLogLevelPatternIsNotConfiguredThenConsoleUsesDefault() { |
| 53 | + assertThat(consolePattern()).contains("%5p"); |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + void whenLogLevelPatternIsSetThenConsoleUsesIt() { |
| 58 | + withSystemProperty(LoggingSystemProperties.LOG_LEVEL_PATTERN, "custom", |
| 59 | + () -> assertThat(consolePattern()).contains("custom")); |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + void whenLogLDateformatPatternIsNotConfiguredThenConsoleUsesDefault() { |
| 64 | + assertThat(consolePattern()).contains("yyyy-MM-dd HH:mm:ss.SSS"); |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + void whenLogDateformatPatternIsSetThenConsoleUsesIt() { |
| 69 | + withSystemProperty(LoggingSystemProperties.LOG_DATEFORMAT_PATTERN, "custom", |
| 70 | + () -> assertThat(consolePattern()).contains("custom")); |
| 71 | + } |
| 72 | + |
| 73 | + protected void withSystemProperty(String name, String value, Runnable action) { |
| 74 | + String previous = System.setProperty(name, value); |
| 75 | + action.run(); |
| 76 | + if (previous == null) { |
| 77 | + System.clearProperty(name); |
| 78 | + } |
| 79 | + else { |
| 80 | + System.setProperty(name, previous); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + private String consolePattern() { |
| 85 | + Configuration configuration = initializeConfiguration(); |
| 86 | + return ((PatternLayout) configuration.getAppender("Console").getLayout()).getConversionPattern(); |
| 87 | + } |
| 88 | + |
| 89 | + protected Configuration initializeConfiguration() { |
| 90 | + LoggerContext context = new LoggerContext("test"); |
| 91 | + Configuration configuration = ConfigurationFactory.getInstance().getConfiguration(context, |
| 92 | + configurationSource()); |
| 93 | + configuration.initialize(); |
| 94 | + return configuration; |
| 95 | + } |
| 96 | + |
| 97 | + private ConfigurationSource configurationSource() { |
| 98 | + try (InputStream in = getClass().getResourceAsStream(getConfigFileName())) { |
| 99 | + return new ConfigurationSource(in); |
| 100 | + } |
| 101 | + catch (IOException ex) { |
| 102 | + throw new RuntimeException(ex); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + protected String getConfigFileName() { |
| 107 | + return "log4j2.xml"; |
| 108 | + } |
| 109 | + |
| 110 | +} |
0 commit comments