@ -40,6 +40,7 @@ import org.slf4j.ILoggerFactory;
import org.slf4j.bridge.SLF4JBridgeHandler ;
import org.slf4j.impl.StaticLoggerBinder ;
import org.springframework.boot.convert.ApplicationConversionService ;
import org.springframework.boot.logging.AbstractLoggingSystemTests ;
import org.springframework.boot.logging.LogFile ;
import org.springframework.boot.logging.LogLevel ;
@ -49,6 +50,9 @@ import org.springframework.boot.logging.LoggingSystem;
import org.springframework.boot.logging.LoggingSystemProperties ;
import org.springframework.boot.testsupport.system.CapturedOutput ;
import org.springframework.boot.testsupport.system.OutputCaptureExtension ;
import org.springframework.core.convert.ConversionService ;
import org.springframework.core.convert.support.ConfigurableConversionService ;
import org.springframework.core.env.ConfigurableEnvironment ;
import org.springframework.mock.env.MockEnvironment ;
import org.springframework.test.util.ReflectionTestUtils ;
import org.springframework.util.StringUtils ;
@ -79,6 +83,8 @@ class LogbackLoggingSystemTests extends AbstractLoggingSystemTests {
private Logger logger ;
private MockEnvironment environment ;
private LoggingInitializationContext initializationContext ;
private Set < Object > systemPropertyNames ;
@ -88,8 +94,10 @@ class LogbackLoggingSystemTests extends AbstractLoggingSystemTests {
this . systemPropertyNames = new HashSet < > ( System . getProperties ( ) . keySet ( ) ) ;
this . loggingSystem . cleanUp ( ) ;
this . logger = ( ( LoggerContext ) StaticLoggerBinder . getSingleton ( ) . getLoggerFactory ( ) ) . getLogger ( getClass ( ) ) ;
MockEnvironment environment = new MockEnvironment ( ) ;
this . initializationContext = new LoggingInitializationContext ( environment ) ;
this . environment = new MockEnvironment ( ) ;
ConversionService conversionService = ApplicationConversionService . getSharedInstance ( ) ;
this . environment . setConversionService ( ( ConfigurableConversionService ) conversionService ) ;
this . initializationContext = new LoggingInitializationContext ( this . environment ) ;
}
@AfterEach
@ -103,7 +111,7 @@ class LogbackLoggingSystemTests extends AbstractLoggingSystemTests {
void noFile ( CapturedOutput output ) {
this . loggingSystem . beforeInitialize ( ) ;
this . logger . info ( "Hidden" ) ;
this . loggingSystem . initialize( this . initializationContext , null , null ) ;
initialize( this . initializationContext , null , null ) ;
this . logger . info ( "Hello world" ) ;
assertThat ( output ) . contains ( "Hello world" ) . doesNotContain ( "Hidden" ) ;
assertThat ( getLineWithText ( output , "Hello world" ) ) . contains ( "INFO" ) ;
@ -114,7 +122,7 @@ class LogbackLoggingSystemTests extends AbstractLoggingSystemTests {
void withFile ( CapturedOutput output ) {
this . loggingSystem . beforeInitialize ( ) ;
this . logger . info ( "Hidden" ) ;
this . loggingSystem . initialize( this . initializationContext , null , getLogFile ( null , tmpDir ( ) ) ) ;
initialize( this . initializationContext , null , getLogFile ( null , tmpDir ( ) ) ) ;
this . logger . info ( "Hello world" ) ;
File file = new File ( tmpDir ( ) + "/spring.log" ) ;
assertThat ( output ) . doesNotContain ( "LOGBACK:" ) ;
@ -129,14 +137,14 @@ class LogbackLoggingSystemTests extends AbstractLoggingSystemTests {
@Test
void defaultConfigConfiguresAConsoleAppender ( ) {
this . loggingSystem . beforeInitialize ( ) ;
this . loggingSystem . initialize( this . initializationContext , null , null ) ;
initialize( this . initializationContext , null , null ) ;
assertThat ( getConsoleAppender ( ) ) . isNotNull ( ) ;
}
@Test
void testNonDefaultConfigLocation ( CapturedOutput output ) {
this . loggingSystem . beforeInitialize ( ) ;
this . loggingSystem . initialize( this . initializationContext , "classpath:logback-nondefault.xml" ,
initialize( this . initializationContext , "classpath:logback-nondefault.xml" ,
getLogFile ( tmpDir ( ) + "/tmp.log" , null ) ) ;
this . logger . info ( "Hello world" ) ;
assertThat ( output ) . doesNotContain ( "DEBUG" ) . contains ( "Hello world" ) . contains ( tmpDir ( ) + "/tmp.log" )
@ -149,7 +157,7 @@ class LogbackLoggingSystemTests extends AbstractLoggingSystemTests {
System . setProperty ( "logback.configurationFile" , "/foo/my-file.xml" ) ;
try {
this . loggingSystem . beforeInitialize ( ) ;
this . loggingSystem . initialize( this . initializationContext , null , null ) ;
initialize( this . initializationContext , null , null ) ;
assertThat ( output ) . contains (
"Ignoring 'logback.configurationFile' system property. Please use 'logging.config' instead." ) ;
}
@ -161,8 +169,8 @@ class LogbackLoggingSystemTests extends AbstractLoggingSystemTests {
@Test
void testNonexistentConfigLocation ( ) {
this . loggingSystem . beforeInitialize ( ) ;
assertThatIllegalStateException ( ) . isThrownBy ( ( ) - > this . loggingSystem . initialize ( this . initializationContext ,
"classpath:logback-nonexistent.xml" , null ) ) ;
assertThatIllegalStateException ( )
. isThrownBy ( ( ) - > initialize ( this . initializationContext , "classpath:logback-nonexistent.xml" , null ) ) ;
}
@Test
@ -174,7 +182,7 @@ class LogbackLoggingSystemTests extends AbstractLoggingSystemTests {
@Test
void setLevel ( CapturedOutput output ) {
this . loggingSystem . beforeInitialize ( ) ;
this . loggingSystem . initialize( this . initializationContext , null , null ) ;
initialize( this . initializationContext , null , null ) ;
this . logger . debug ( "Hello" ) ;
this . loggingSystem . setLogLevel ( "org.springframework.boot" , LogLevel . DEBUG ) ;
this . logger . debug ( "Hello" ) ;
@ -184,7 +192,7 @@ class LogbackLoggingSystemTests extends AbstractLoggingSystemTests {
@Test
void setLevelToNull ( CapturedOutput output ) {
this . loggingSystem . beforeInitialize ( ) ;
this . loggingSystem . initialize( this . initializationContext , null , null ) ;
initialize( this . initializationContext , null , null ) ;
this . logger . debug ( "Hello" ) ;
this . loggingSystem . setLogLevel ( "org.springframework.boot" , LogLevel . DEBUG ) ;
this . logger . debug ( "Hello" ) ;
@ -196,7 +204,7 @@ class LogbackLoggingSystemTests extends AbstractLoggingSystemTests {
@Test
void getLoggingConfigurations ( ) {
this . loggingSystem . beforeInitialize ( ) ;
this . loggingSystem . initialize( this . initializationContext , null , null ) ;
initialize( this . initializationContext , null , null ) ;
this . loggingSystem . setLogLevel ( getClass ( ) . getName ( ) , LogLevel . DEBUG ) ;
List < LoggerConfiguration > configurations = this . loggingSystem . getLoggerConfigurations ( ) ;
assertThat ( configurations ) . isNotEmpty ( ) ;
@ -206,7 +214,7 @@ class LogbackLoggingSystemTests extends AbstractLoggingSystemTests {
@Test
void getLoggingConfiguration ( ) {
this . loggingSystem . beforeInitialize ( ) ;
this . loggingSystem . initialize( this . initializationContext , null , null ) ;
initialize( this . initializationContext , null , null ) ;
this . loggingSystem . setLogLevel ( getClass ( ) . getName ( ) , LogLevel . DEBUG ) ;
LoggerConfiguration configuration = this . loggingSystem . getLoggerConfiguration ( getClass ( ) . getName ( ) ) ;
assertThat ( configuration )
@ -216,7 +224,7 @@ class LogbackLoggingSystemTests extends AbstractLoggingSystemTests {
@Test
void getLoggingConfigurationForLoggerThatDoesNotExistShouldReturnNull ( ) {
this . loggingSystem . beforeInitialize ( ) ;
this . loggingSystem . initialize( this . initializationContext , null , null ) ;
initialize( this . initializationContext , null , null ) ;
LoggerConfiguration configuration = this . loggingSystem . getLoggerConfiguration ( "doesnotexist" ) ;
assertThat ( configuration ) . isNull ( ) ;
}
@ -224,7 +232,7 @@ class LogbackLoggingSystemTests extends AbstractLoggingSystemTests {
@Test
void getLoggingConfigurationForALL ( ) {
this . loggingSystem . beforeInitialize ( ) ;
this . loggingSystem . initialize( this . initializationContext , null , null ) ;
initialize( this . initializationContext , null , null ) ;
Logger logger = ( Logger ) StaticLoggerBinder . getSingleton ( ) . getLoggerFactory ( ) . getLogger ( getClass ( ) . getName ( ) ) ;
logger . setLevel ( Level . ALL ) ;
LoggerConfiguration configuration = this . loggingSystem . getLoggerConfiguration ( getClass ( ) . getName ( ) ) ;
@ -235,7 +243,7 @@ class LogbackLoggingSystemTests extends AbstractLoggingSystemTests {
@Test
void systemLevelTraceShouldReturnNativeLevelTraceNotAll ( ) {
this . loggingSystem . beforeInitialize ( ) ;
this . loggingSystem . initialize( this . initializationContext , null , null ) ;
initialize( this . initializationContext , null , null ) ;
this . loggingSystem . setLogLevel ( getClass ( ) . getName ( ) , LogLevel . TRACE ) ;
Logger logger = ( Logger ) StaticLoggerBinder . getSingleton ( ) . getLoggerFactory ( ) . getLogger ( getClass ( ) . getName ( ) ) ;
assertThat ( logger . getLevel ( ) ) . isEqualTo ( Level . TRACE ) ;
@ -244,7 +252,7 @@ class LogbackLoggingSystemTests extends AbstractLoggingSystemTests {
@Test
void loggingThatUsesJulIsCaptured ( CapturedOutput output ) {
this . loggingSystem . beforeInitialize ( ) ;
this . loggingSystem . initialize( this . initializationContext , null , null ) ;
initialize( this . initializationContext , null , null ) ;
java . util . logging . Logger julLogger = java . util . logging . Logger . getLogger ( getClass ( ) . getName ( ) ) ;
julLogger . info ( "Hello world" ) ;
assertThat ( output ) . contains ( "Hello world" ) ;
@ -253,7 +261,7 @@ class LogbackLoggingSystemTests extends AbstractLoggingSystemTests {
@Test
void loggingLevelIsPropagatedToJul ( CapturedOutput output ) {
this . loggingSystem . beforeInitialize ( ) ;
this . loggingSystem . initialize( this . initializationContext , null , null ) ;
initialize( this . initializationContext , null , null ) ;
this . loggingSystem . setLogLevel ( getClass ( ) . getName ( ) , LogLevel . DEBUG ) ;
java . util . logging . Logger julLogger = java . util . logging . Logger . getLogger ( getClass ( ) . getName ( ) ) ;
julLogger . fine ( "Hello debug world" ) ;
@ -296,33 +304,30 @@ class LogbackLoggingSystemTests extends AbstractLoggingSystemTests {
@Test
void testConsolePatternProperty ( CapturedOutput output ) {
MockEnvironment environment = new MockEnvironment ( ) ;
environment . setProperty ( "logging.pattern.console" , "%logger %msg" ) ;
LoggingInitializationContext loggingInitializationContext = new LoggingInitializationContext ( environment ) ;
this . loggingSystem . initialize ( loggingInitializationContext , null , null ) ;
this . environment . setProperty ( "logging.pattern.console" , "%logger %msg" ) ;
LoggingInitializationContext loggingInitializationContext = new LoggingInitializationContext ( this . environment ) ;
initialize ( loggingInitializationContext , null , null ) ;
this . logger . info ( "Hello world" ) ;
assertThat ( getLineWithText ( output , "Hello world" ) ) . doesNotContain ( "INFO" ) ;
}
@Test
void testLevelPatternProperty ( CapturedOutput output ) {
MockEnvironment environment = new MockEnvironment ( ) ;
environment . setProperty ( "logging.pattern.level" , "X%clr(%p)X" ) ;
new LoggingSystemProperties ( environment ) . apply ( ) ;
LoggingInitializationContext loggingInitializationContext = new LoggingInitializationContext ( environment ) ;
this . loggingSystem . initialize ( loggingInitializationContext , null , null ) ;
this . environment . setProperty ( "logging.pattern.level" , "X%clr(%p)X" ) ;
new LoggingSystemProperties ( this . environment ) . apply ( ) ;
LoggingInitializationContext loggingInitializationContext = new LoggingInitializationContext ( this . environment ) ;
initialize ( loggingInitializationContext , null , null ) ;
this . logger . info ( "Hello world" ) ;
assertThat ( getLineWithText ( output , "Hello world" ) ) . contains ( "XINFOX" ) ;
}
@Test
void testFilePatternProperty ( CapturedOutput output ) {
MockEnvironment environment = new MockEnvironment ( ) ;
environment . setProperty ( "logging.pattern.file" , "%logger %msg" ) ;
LoggingInitializationContext loggingInitializationContext = new LoggingInitializationContext ( environment ) ;
this . environment . setProperty ( "logging.pattern.file" , "%logger %msg" ) ;
LoggingInitializationContext loggingInitializationContext = new LoggingInitializationContext ( this . environment ) ;
File file = new File ( tmpDir ( ) , "logback-test.log" ) ;
LogFile logFile = getLogFile ( file . getPath ( ) , null ) ;
this . loggingSystem . initialize( loggingInitializationContext , null , logFile ) ;
initialize( loggingInitializationContext , null , logFile ) ;
this . logger . info ( "Hello world" ) ;
assertThat ( getLineWithText ( output , "Hello world" ) ) . contains ( "INFO" ) ;
assertThat ( getLineWithText ( file , "Hello world" ) ) . doesNotContain ( "INFO" ) ;
@ -330,12 +335,11 @@ class LogbackLoggingSystemTests extends AbstractLoggingSystemTests {
@Test
void testCleanHistoryOnStartProperty ( ) {
MockEnvironment environment = new MockEnvironment ( ) ;
environment . setProperty ( "logging.file.clean-history-on-start" , "true" ) ;
LoggingInitializationContext loggingInitializationContext = new LoggingInitializationContext ( environment ) ;
this . environment . setProperty ( "logging.file.clean-history-on-start" , "true" ) ;
LoggingInitializationContext loggingInitializationContext = new LoggingInitializationContext ( this . environment ) ;
File file = new File ( tmpDir ( ) , "logback-test.log" ) ;
LogFile logFile = getLogFile ( file . getPath ( ) , null ) ;
this . loggingSystem . initialize( loggingInitializationContext , null , logFile ) ;
initialize( loggingInitializationContext , null , logFile ) ;
this . logger . info ( "Hello world" ) ;
assertThat ( getLineWithText ( file , "Hello world" ) ) . contains ( "INFO" ) ;
assertThat ( getRollingPolicy ( ) . isCleanHistoryOnStart ( ) ) . isTrue ( ) ;
@ -343,12 +347,11 @@ class LogbackLoggingSystemTests extends AbstractLoggingSystemTests {
@Test
void testCleanHistoryOnStartPropertyWithXmlConfiguration ( ) {
MockEnvironment environment = new MockEnvironment ( ) ;
environment . setProperty ( "logging.file.clean-history-on-start" , "true" ) ;
LoggingInitializationContext loggingInitializationContext = new LoggingInitializationContext ( environment ) ;
this . environment . setProperty ( "logging.file.clean-history-on-start" , "true" ) ;
LoggingInitializationContext loggingInitializationContext = new LoggingInitializationContext ( this . environment ) ;
File file = new File ( tmpDir ( ) , "logback-test.log" ) ;
LogFile logFile = getLogFile ( file . getPath ( ) , null ) ;
this . loggingSystem . initialize( loggingInitializationContext , "classpath:logback-include-base.xml" , logFile ) ;
initialize( loggingInitializationContext , "classpath:logback-include-base.xml" , logFile ) ;
this . logger . info ( "Hello world" ) ;
assertThat ( getLineWithText ( file , "Hello world" ) ) . contains ( "INFO" ) ;
assertThat ( getRollingPolicy ( ) . isCleanHistoryOnStart ( ) ) . isTrue ( ) ;
@ -370,12 +373,11 @@ class LogbackLoggingSystemTests extends AbstractLoggingSystemTests {
}
private void testMaxFileSizeProperty ( String sizeValue , String expectedFileSize ) {
MockEnvironment environment = new MockEnvironment ( ) ;
environment . setProperty ( "logging.file.max-size" , sizeValue ) ;
LoggingInitializationContext loggingInitializationContext = new LoggingInitializationContext ( environment ) ;
this . environment . setProperty ( "logging.file.max-size" , sizeValue ) ;
LoggingInitializationContext loggingInitializationContext = new LoggingInitializationContext ( this . environment ) ;
File file = new File ( tmpDir ( ) , "logback-test.log" ) ;
LogFile logFile = getLogFile ( file . getPath ( ) , null ) ;
this . loggingSystem . initialize( loggingInitializationContext , null , logFile ) ;
initialize( loggingInitializationContext , null , logFile ) ;
this . logger . info ( "Hello world" ) ;
assertThat ( getLineWithText ( file , "Hello world" ) ) . contains ( "INFO" ) ;
assertThat ( ReflectionTestUtils . getField ( getRollingPolicy ( ) , "maxFileSize" ) . toString ( ) )
@ -384,12 +386,11 @@ class LogbackLoggingSystemTests extends AbstractLoggingSystemTests {
@Test
void testMaxFileSizePropertyWithXmlConfiguration ( ) {
MockEnvironment environment = new MockEnvironment ( ) ;
environment . setProperty ( "logging.file.max-size" , "100MB" ) ;
LoggingInitializationContext loggingInitializationContext = new LoggingInitializationContext ( environment ) ;
this . environment . setProperty ( "logging.file.max-size" , "100MB" ) ;
LoggingInitializationContext loggingInitializationContext = new LoggingInitializationContext ( this . environment ) ;
File file = new File ( tmpDir ( ) , "logback-test.log" ) ;
LogFile logFile = getLogFile ( file . getPath ( ) , null ) ;
this . loggingSystem . initialize( loggingInitializationContext , "classpath:logback-include-base.xml" , logFile ) ;
initialize( loggingInitializationContext , "classpath:logback-include-base.xml" , logFile ) ;
this . logger . info ( "Hello world" ) ;
assertThat ( getLineWithText ( file , "Hello world" ) ) . contains ( "INFO" ) ;
assertThat ( ReflectionTestUtils . getField ( getRollingPolicy ( ) , "maxFileSize" ) . toString ( ) ) . isEqualTo ( "100 MB" ) ;
@ -397,12 +398,11 @@ class LogbackLoggingSystemTests extends AbstractLoggingSystemTests {
@Test
void testMaxHistoryProperty ( ) {
MockEnvironment environment = new MockEnvironment ( ) ;
environment . setProperty ( "logging.file.max-history" , "30" ) ;
LoggingInitializationContext loggingInitializationContext = new LoggingInitializationContext ( environment ) ;
this . environment . setProperty ( "logging.file.max-history" , "30" ) ;
LoggingInitializationContext loggingInitializationContext = new LoggingInitializationContext ( this . environment ) ;
File file = new File ( tmpDir ( ) , "logback-test.log" ) ;
LogFile logFile = getLogFile ( file . getPath ( ) , null ) ;
this . loggingSystem . initialize( loggingInitializationContext , null , logFile ) ;
initialize( loggingInitializationContext , null , logFile ) ;
this . logger . info ( "Hello world" ) ;
assertThat ( getLineWithText ( file , "Hello world" ) ) . contains ( "INFO" ) ;
assertThat ( getRollingPolicy ( ) . getMaxHistory ( ) ) . isEqualTo ( 30 ) ;
@ -410,12 +410,11 @@ class LogbackLoggingSystemTests extends AbstractLoggingSystemTests {
@Test
void testMaxHistoryPropertyWithXmlConfiguration ( ) {
MockEnvironment environment = new MockEnvironment ( ) ;
environment . setProperty ( "logging.file.max-history" , "30" ) ;
LoggingInitializationContext loggingInitializationContext = new LoggingInitializationContext ( environment ) ;
this . environment . setProperty ( "logging.file.max-history" , "30" ) ;
LoggingInitializationContext loggingInitializationContext = new LoggingInitializationContext ( this . environment ) ;
File file = new File ( tmpDir ( ) , "logback-test.log" ) ;
LogFile logFile = getLogFile ( file . getPath ( ) , null ) ;
this . loggingSystem . initialize( loggingInitializationContext , "classpath:logback-include-base.xml" , logFile ) ;
initialize( loggingInitializationContext , "classpath:logback-include-base.xml" , logFile ) ;
this . logger . info ( "Hello world" ) ;
assertThat ( getLineWithText ( file , "Hello world" ) ) . contains ( "INFO" ) ;
assertThat ( getRollingPolicy ( ) . getMaxHistory ( ) ) . isEqualTo ( 30 ) ;
@ -437,12 +436,11 @@ class LogbackLoggingSystemTests extends AbstractLoggingSystemTests {
}
private void testTotalSizeCapProperty ( String sizeValue , String expectedFileSize ) {
MockEnvironment environment = new MockEnvironment ( ) ;
environment . setProperty ( "logging.file.total-size-cap" , sizeValue ) ;
LoggingInitializationContext loggingInitializationContext = new LoggingInitializationContext ( environment ) ;
this . environment . setProperty ( "logging.file.total-size-cap" , sizeValue ) ;
LoggingInitializationContext loggingInitializationContext = new LoggingInitializationContext ( this . environment ) ;
File file = new File ( tmpDir ( ) , "logback-test.log" ) ;
LogFile logFile = getLogFile ( file . getPath ( ) , null ) ;
this . loggingSystem . initialize( loggingInitializationContext , null , logFile ) ;
initialize( loggingInitializationContext , null , logFile ) ;
this . logger . info ( "Hello world" ) ;
assertThat ( getLineWithText ( file , "Hello world" ) ) . contains ( "INFO" ) ;
assertThat ( ReflectionTestUtils . getField ( getRollingPolicy ( ) , "totalSizeCap" ) . toString ( ) )
@ -452,12 +450,11 @@ class LogbackLoggingSystemTests extends AbstractLoggingSystemTests {
@Test
void testTotalSizeCapPropertyWithXmlConfiguration ( ) {
String expectedSize = "101 MB" ;
MockEnvironment environment = new MockEnvironment ( ) ;
environment . setProperty ( "logging.file.total-size-cap" , expectedSize ) ;
LoggingInitializationContext loggingInitializationContext = new LoggingInitializationContext ( environment ) ;
this . environment . setProperty ( "logging.file.total-size-cap" , expectedSize ) ;
LoggingInitializationContext loggingInitializationContext = new LoggingInitializationContext ( this . environment ) ;
File file = new File ( tmpDir ( ) , "logback-test.log" ) ;
LogFile logFile = getLogFile ( file . getPath ( ) , null ) ;
this . loggingSystem . initialize( loggingInitializationContext , "classpath:logback-include-base.xml" , logFile ) ;
initialize( loggingInitializationContext , "classpath:logback-include-base.xml" , logFile ) ;
this . logger . info ( "Hello world" ) ;
assertThat ( getLineWithText ( file , "Hello world" ) ) . contains ( "INFO" ) ;
assertThat ( ReflectionTestUtils . getField ( getRollingPolicy ( ) , "totalSizeCap" ) . toString ( ) ) . isEqualTo ( expectedSize ) ;
@ -466,7 +463,7 @@ class LogbackLoggingSystemTests extends AbstractLoggingSystemTests {
@Test
void exceptionsIncludeClassPackaging ( CapturedOutput output ) {
this . loggingSystem . beforeInitialize ( ) ;
this . loggingSystem . initialize( this . initializationContext , null , getLogFile ( null , tmpDir ( ) ) ) ;
initialize( this . initializationContext , null , getLogFile ( null , tmpDir ( ) ) ) ;
this . logger . warn ( "Expected exception" , new RuntimeException ( "Expected" ) ) ;
String fileContents = contentOf ( new File ( tmpDir ( ) + "/spring.log" ) ) ;
assertThat ( fileContents ) . contains ( "[junit-" ) ;
@ -479,7 +476,7 @@ class LogbackLoggingSystemTests extends AbstractLoggingSystemTests {
try {
this . loggingSystem . beforeInitialize ( ) ;
this . logger . info ( "Hidden" ) ;
this . loggingSystem . initialize( this . initializationContext , null , getLogFile ( null , tmpDir ( ) ) ) ;
initialize( this . initializationContext , null , getLogFile ( null , tmpDir ( ) ) ) ;
this . logger . warn ( "Expected exception" , new RuntimeException ( "Expected" , new RuntimeException ( "Cause" ) ) ) ;
String fileContents = contentOf ( new File ( tmpDir ( ) + "/spring.log" ) ) ;
assertThat ( fileContents ) . contains ( "java.lang.RuntimeException: Expected" ) . doesNotContain ( "Wrapped by:" ) ;
@ -496,7 +493,7 @@ class LogbackLoggingSystemTests extends AbstractLoggingSystemTests {
this . loggingSystem . beforeInitialize ( ) ;
this . logger . info ( "Hidden" ) ;
LogFile logFile = getLogFile ( tmpDir ( ) + "/example.log" , null , false ) ;
this . loggingSystem . initialize( this . initializationContext , "classpath:logback-nondefault.xml" , logFile ) ;
initialize( this . initializationContext , "classpath:logback-nondefault.xml" , logFile ) ;
assertThat ( System . getProperty ( LoggingSystemProperties . LOG_FILE ) ) . endsWith ( "example.log" ) ;
}
@ -506,24 +503,23 @@ class LogbackLoggingSystemTests extends AbstractLoggingSystemTests {
LoggerContextListener listener = mock ( LoggerContextListener . class ) ;
loggerContext . addListener ( listener ) ;
this . loggingSystem . beforeInitialize ( ) ;
this . loggingSystem . initialize( this . initializationContext , null , null ) ;
initialize( this . initializationContext , null , null ) ;
this . loggingSystem . beforeInitialize ( ) ;
this . loggingSystem . initialize( this . initializationContext , null , null ) ;
initialize( this . initializationContext , null , null ) ;
verify ( listener , times ( 1 ) ) . onReset ( loggerContext ) ;
this . loggingSystem . cleanUp ( ) ;
loggerContext . addListener ( listener ) ;
this . loggingSystem . beforeInitialize ( ) ;
this . loggingSystem . initialize( this . initializationContext , null , null ) ;
initialize( this . initializationContext , null , null ) ;
verify ( listener , times ( 2 ) ) . onReset ( loggerContext ) ;
}
@Test
void testDateformatPatternProperty ( CapturedOutput output ) {
MockEnvironment environment = new MockEnvironment ( ) ;
environment . setProperty ( "logging.pattern.dateformat" , "yyyy-MM-dd'T'hh:mm:ss.SSSZ" ) ;
new LoggingSystemProperties ( environment ) . apply ( ) ;
LoggingInitializationContext loggingInitializationContext = new LoggingInitializationContext ( environment ) ;
this . loggingSystem . initialize ( loggingInitializationContext , null , null ) ;
this . environment . setProperty ( "logging.pattern.dateformat" , "yyyy-MM-dd'T'hh:mm:ss.SSSZ" ) ;
new LoggingSystemProperties ( this . environment ) . apply ( ) ;
LoggingInitializationContext loggingInitializationContext = new LoggingInitializationContext ( this . environment ) ;
initialize ( loggingInitializationContext , null , null ) ;
this . logger . info ( "Hello world" ) ;
assertThat ( getLineWithText ( output , "Hello world" ) )
. containsPattern ( "\\d{4}-\\d{2}\\-\\d{2}T\\d{2}:\\d{2}:\\d{2}" ) ;
@ -535,7 +531,7 @@ class LogbackLoggingSystemTests extends AbstractLoggingSystemTests {
this . loggingSystem . beforeInitialize ( ) ;
File file = new File ( tmpDir ( ) , "logback-test.log" ) ;
LogFile logFile = getLogFile ( file . getPath ( ) , null ) ;
this . loggingSystem . initialize( this . initializationContext , null , logFile ) ;
initialize( this . initializationContext , null , logFile ) ;
assertThat ( output ) . doesNotContain ( "LevelChangePropagator" ) . doesNotContain ( "SizeAndTimeBasedFNATP" ) ;
}
@ -546,7 +542,7 @@ class LogbackLoggingSystemTests extends AbstractLoggingSystemTests {
this . loggingSystem . beforeInitialize ( ) ;
File file = new File ( tmpDir ( ) , "logback-test.log" ) ;
LogFile logFile = getLogFile ( file . getPath ( ) , null ) ;
this . loggingSystem . initialize( this . initializationContext , null , logFile ) ;
initialize( this . initializationContext , null , logFile ) ;
assertThat ( output ) . contains ( "LevelChangePropagator" ) . contains ( "SizeAndTimeBasedFNATP" )
. contains ( "DebugLogbackConfigurator" ) ;
}
@ -557,18 +553,22 @@ class LogbackLoggingSystemTests extends AbstractLoggingSystemTests {
@Test
void testRollingFileNameProperty ( ) {
MockEnvironment environment = new MockEnvironment ( ) ;
String rollingFile = "my.log.%d{yyyyMMdd}.%i.gz" ;
environment. setProperty ( "logging.pattern.rolling-file-name" , rollingFile ) ;
LoggingInitializationContext loggingInitializationContext = new LoggingInitializationContext ( environment) ;
this . environment. setProperty ( "logging.pattern.rolling-file-name" , rollingFile ) ;
LoggingInitializationContext loggingInitializationContext = new LoggingInitializationContext ( this . environment) ;
File file = new File ( tmpDir ( ) , "my.log" ) ;
LogFile logFile = getLogFile ( file . getPath ( ) , null ) ;
this . loggingSystem . initialize( loggingInitializationContext , null , logFile ) ;
initialize( loggingInitializationContext , null , logFile ) ;
this . logger . info ( "Hello world" ) ;
assertThat ( getLineWithText ( file , "Hello world" ) ) . contains ( "INFO" ) ;
assertThat ( getRollingPolicy ( ) . getFileNamePattern ( ) ) . isEqualTo ( rollingFile ) ;
}
private void initialize ( LoggingInitializationContext context , String configLocation , LogFile logFile ) {
this . loggingSystem . getSystemProperties ( ( ConfigurableEnvironment ) context . getEnvironment ( ) ) . apply ( logFile ) ;
this . loggingSystem . initialize ( context , configLocation , logFile ) ;
}
private static Logger getRootLogger ( ) {
ILoggerFactory factory = StaticLoggerBinder . getSingleton ( ) . getLoggerFactory ( ) ;
LoggerContext context = ( LoggerContext ) factory ;