Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b2076d8

Browse files
committed
Make it easier to override FILE_LOG_PATTERN
1 parent 2e1988f commit b2076d8

File tree

3 files changed

+21
-1
lines changed

3 files changed

+21
-1
lines changed

spring-boot/src/main/resources/org/springframework/boot/logging/logback/defaults.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ initialization performed by Boot
1010
<conversionRule conversionWord="wex" converterClass="org.springframework.boot.logging.logback.WhitespaceThrowableProxyConverter" />
1111
<conversionRule conversionWord="wEx" converterClass="org.springframework.boot.logging.logback.ExtendedWhitespaceThrowableProxyConverter" />
1212
<property name="CONSOLE_LOG_PATTERN" value="${CONSOLE_LOG_PATTERN:-%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr(${LOG_LEVEL_PATTERN:-%5p}) %clr(${PID:- }){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}}"/>
13-
<property name="FILE_LOG_PATTERN" value="%d{yyyy-MM-dd HH:mm:ss.SSS} ${LOG_LEVEL_PATTERN:-%5p} ${PID:- } --- [%t] %-40.40logger{39} : %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}"/>
13+
<property name="FILE_LOG_PATTERN" value="${FILE_LOG_PATTERN:-%d{yyyy-MM-dd HH:mm:ss.SSS} ${LOG_LEVEL_PATTERN:-%5p} ${PID:- } --- [%t] %-40.40logger{39} : %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}}"/>
1414

1515
<appender name="DEBUG_LEVEL_REMAPPER" class="org.springframework.boot.logging.logback.LevelRemappingAppender">
1616
<destinationLogger>org.springframework.boot</destinationLogger>

spring-boot/src/test/java/org/springframework/boot/logging/logback/LogbackConfigurationTests.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import ch.qos.logback.classic.spi.ILoggingEvent;
2525
import ch.qos.logback.core.Appender;
2626
import ch.qos.logback.core.ConsoleAppender;
27+
import ch.qos.logback.core.FileAppender;
2728
import ch.qos.logback.core.encoder.Encoder;
2829
import ch.qos.logback.core.joran.spi.JoranException;
2930
import org.junit.Test;
@@ -52,4 +53,19 @@ public void consolePatternCanBeOverridden() throws JoranException {
5253
assertThat(((PatternLayoutEncoder) encoder).getPattern()).isEqualTo("foo");
5354
}
5455

56+
@Test
57+
public void filePatternCanBeOverridden() throws JoranException {
58+
JoranConfigurator configurator = new JoranConfigurator();
59+
LoggerContext context = new LoggerContext();
60+
configurator.setContext(context);
61+
configurator.doConfigure(
62+
new File("src/test/resources/custom-file-log-pattern.xml"));
63+
Appender<ILoggingEvent> appender = context.getLogger("ROOT")
64+
.getAppender("FILE");
65+
assertThat(appender).isInstanceOf(FileAppender.class);
66+
Encoder<?> encoder = ((FileAppender<?>) appender).getEncoder();
67+
assertThat(encoder).isInstanceOf(PatternLayoutEncoder.class);
68+
assertThat(((PatternLayoutEncoder) encoder).getPattern()).isEqualTo("bar");
69+
}
70+
5571
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<configuration>
2+
<property name="FILE_LOG_PATTERN" value="bar" />
3+
<include resource="org/springframework/boot/logging/logback/base.xml" />
4+
</configuration>

0 commit comments

Comments
 (0)