@ -22,9 +22,13 @@ import java.io.File;
@@ -22,9 +22,13 @@ import java.io.File;
import java.util.ArrayList ;
import java.util.Collections ;
import java.util.EnumSet ;
import java.util.LinkedHashMap ;
import java.util.List ;
import java.util.Map ;
import com.fasterxml.jackson.databind.ObjectMapper ;
import org.apache.commons.logging.Log ;
import org.apache.commons.logging.LogFactory ;
import org.apache.logging.log4j.LogManager ;
import org.apache.logging.log4j.Logger ;
import org.apache.logging.log4j.core.LoggerContext ;
@ -169,6 +173,28 @@ class Log4J2LoggingSystemTests extends AbstractLoggingSystemTests {
@@ -169,6 +173,28 @@ class Log4J2LoggingSystemTests extends AbstractLoggingSystemTests {
assertThat ( configurations . get ( 0 ) . getName ( ) ) . isEqualTo ( LoggingSystem . ROOT_LOGGER_NAME ) ;
}
@Test
void getLoggingConfigurationsShouldReturnAllLoggers ( ) {
LogManager . getLogger ( "org.springframework.boot.logging.log4j2.Log4J2LoggingSystemTests$Nested" ) ;
this . loggingSystem . beforeInitialize ( ) ;
this . loggingSystem . initialize ( null , null , null ) ;
this . loggingSystem . setLogLevel ( getClass ( ) . getName ( ) , LogLevel . DEBUG ) ;
List < LoggerConfiguration > configurations = this . loggingSystem . getLoggerConfigurations ( ) ;
assertThat ( configurations ) . isNotEmpty ( ) ;
assertThat ( configurations . get ( 0 ) . getName ( ) ) . isEqualTo ( LoggingSystem . ROOT_LOGGER_NAME ) ;
Map < String , LogLevel > loggers = new LinkedHashMap < > ( ) ;
configurations . forEach ( ( logger ) - > loggers . put ( logger . getName ( ) , logger . getConfiguredLevel ( ) ) ) ;
assertIsPresent ( "org" , loggers , null ) ;
assertIsPresent ( "org.springframework.boot.logging.log4j2" , loggers , null ) ;
assertIsPresent ( "org.springframework.boot.logging.log4j2.Log4J2LoggingSystemTests" , loggers , LogLevel . DEBUG ) ;
assertIsPresent ( "org.springframework.boot.logging.log4j2.Log4J2LoggingSystemTests$Nested" , loggers , null ) ;
}
private void assertIsPresent ( String loggerName , Map < String , LogLevel > loggers , LogLevel logLevel ) {
assertThat ( loggers . containsKey ( loggerName ) ) . isTrue ( ) ;
assertThat ( loggers . get ( loggerName ) ) . isEqualTo ( logLevel ) ;
}
@Test
void getLoggingConfiguration ( ) {
this . loggingSystem . beforeInitialize ( ) ;
@ -179,6 +205,24 @@ class Log4J2LoggingSystemTests extends AbstractLoggingSystemTests {
@@ -179,6 +205,24 @@ class Log4J2LoggingSystemTests extends AbstractLoggingSystemTests {
. isEqualTo ( new LoggerConfiguration ( getClass ( ) . getName ( ) , LogLevel . DEBUG , LogLevel . DEBUG ) ) ;
}
@Test
void getLoggingConfigurationShouldReturnLoggerWithNullConfiguredLevel ( ) {
this . loggingSystem . beforeInitialize ( ) ;
this . loggingSystem . initialize ( null , null , null ) ;
this . loggingSystem . setLogLevel ( getClass ( ) . getName ( ) , LogLevel . DEBUG ) ;
LoggerConfiguration configuration = this . loggingSystem . getLoggerConfiguration ( "org" ) ;
assertThat ( configuration ) . isEqualTo ( new LoggerConfiguration ( "org" , null , LogLevel . INFO ) ) ;
}
@Test
void getLoggingConfigurationForNonExistentLoggerShouldReturnNull ( ) {
this . loggingSystem . beforeInitialize ( ) ;
this . loggingSystem . initialize ( null , null , null ) ;
this . loggingSystem . setLogLevel ( getClass ( ) . getName ( ) , LogLevel . DEBUG ) ;
LoggerConfiguration configuration = this . loggingSystem . getLoggerConfiguration ( "doesnotexist" ) ;
assertThat ( configuration ) . isEqualTo ( null ) ;
}
@Test
void setLevelOfUnconfiguredLoggerDoesNotAffectRootConfiguration ( CapturedOutput output ) {
this . loggingSystem . beforeInitialize ( ) ;
@ -321,4 +365,14 @@ class Log4J2LoggingSystemTests extends AbstractLoggingSystemTests {
@@ -321,4 +365,14 @@ class Log4J2LoggingSystemTests extends AbstractLoggingSystemTests {
}
/ * *
* Used for testing that loggers in nested classes are returned by
* { @link Log4J2LoggingSystem # getLoggerConfigurations ( ) } .
* /
static class Nested {
private static final Log logger = LogFactory . getLog ( Nested . class ) ;
}
}