@ -21,22 +21,27 @@ import java.util.ArrayList;
import java.util.Collections ;
import java.util.List ;
import com.fasterxml.jackson.databind.ObjectMapper ;
import org.apache.logging.log4j.LogManager ;
import org.apache.logging.log4j.Logger ;
import org.apache.logging.log4j.core.config.Configuration ;
import org.apache.logging.log4j.core.config.FileConfigurationMonitor ;
import org.junit.Before ;
import org.junit.Ignore ;
import org.junit.Rule ;
import org.junit.Test ;
import org.springframework.boot.logging.AbstractLoggingSystemTests ;
import org.springframework.boot.logging.LogLevel ;
import org.springframework.boot.test.OutputCapture ;
import org.springframework.util.StringUtils ;
import com.fasterxml.jackson.databind.ObjectMapper ;
import static org.hamcrest.Matchers.arrayContaining ;
import static org.hamcrest.Matchers.equalTo ;
import static org.hamcrest.Matchers.instanceOf ;
import static org.hamcrest.Matchers.is ;
import static org.hamcrest.Matchers.notNullValue ;
import static org.hamcrest.core.StringContains.containsString ;
import static org.junit.Assert.assertFalse ;
import static org.junit.Assert.assertThat ;
import static org.junit.Assert.assertTrue ;
@ -72,6 +77,8 @@ public class Log4J2LoggingSystemTests extends AbstractLoggingSystemTests {
assertTrue ( "Wrong output:\n" + output , output . contains ( "Hello world" ) ) ;
assertFalse ( "Output not hidden:\n" + output , output . contains ( "Hidden" ) ) ;
assertFalse ( new File ( tmpDir ( ) + "/spring.log" ) . exists ( ) ) ;
assertThat ( this . loggingSystem . getConfiguration ( ) . getConfigurationSource ( )
. getFile ( ) , is ( notNullValue ( ) ) ) ;
}
@Test
@ -84,6 +91,8 @@ public class Log4J2LoggingSystemTests extends AbstractLoggingSystemTests {
assertTrue ( "Wrong output:\n" + output , output . contains ( "Hello world" ) ) ;
assertFalse ( "Output not hidden:\n" + output , output . contains ( "Hidden" ) ) ;
assertTrue ( new File ( tmpDir ( ) + "/spring.log" ) . exists ( ) ) ;
assertThat ( this . loggingSystem . getConfiguration ( ) . getConfigurationSource ( )
. getFile ( ) , is ( notNullValue ( ) ) ) ;
}
@Test
@ -96,6 +105,12 @@ public class Log4J2LoggingSystemTests extends AbstractLoggingSystemTests {
assertTrue ( "Wrong output:\n" + output , output . contains ( "Hello world" ) ) ;
assertTrue ( "Wrong output:\n" + output , output . contains ( tmpDir ( ) + "/tmp.log" ) ) ;
assertFalse ( new File ( tmpDir ( ) + "/tmp.log" ) . exists ( ) ) ;
assertThat ( this . loggingSystem . getConfiguration ( ) . getConfigurationSource ( )
. getFile ( ) . getAbsolutePath ( ) , containsString ( "log4j2-nondefault.xml" ) ) ;
// we assume that "log4j2-nondefault.xml" contains the 'monitorInterval'
// attribute, so we check that a monitor is created
assertThat ( this . loggingSystem . getConfiguration ( ) . getConfigurationMonitor ( ) ,
is ( instanceOf ( FileConfigurationMonitor . class ) ) ) ;
}
@Test ( expected = IllegalStateException . class )
@ -167,6 +182,10 @@ public class Log4J2LoggingSystemTests extends AbstractLoggingSystemTests {
super ( TestLog4J2LoggingSystem . class . getClassLoader ( ) ) ;
}
public Configuration getConfiguration ( ) {
return ( ( org . apache . logging . log4j . core . LoggerContext ) LogManager . getContext ( false ) ) . getConfiguration ( ) ;
}
@Override
protected boolean isClassAvailable ( String className ) {
return this . availableClasses . contains ( className ) ;