40
40
import ch .qos .logback .core .joran .spi .JoranException ;
41
41
import ch .qos .logback .core .rolling .RollingFileAppender ;
42
42
import ch .qos .logback .core .rolling .SizeAndTimeBasedRollingPolicy ;
43
+ import ch .qos .logback .core .status .ErrorStatus ;
44
+ import ch .qos .logback .core .status .InfoStatus ;
45
+ import ch .qos .logback .core .status .StatusManager ;
46
+ import ch .qos .logback .core .status .WarnStatus ;
43
47
import ch .qos .logback .core .util .DynamicClassLoadingException ;
44
48
import org .junit .jupiter .api .AfterEach ;
45
49
import org .junit .jupiter .api .BeforeEach ;
@@ -645,13 +649,20 @@ void logbackDebugPropertyIsHonored(CapturedOutput output) {
645
649
System .setProperty ("logback.debug" , "true" );
646
650
try {
647
651
this .loggingSystem .beforeInitialize ();
652
+ LoggerContext loggerContext = this .logger .getLoggerContext ();
653
+ StatusManager statusManager = loggerContext .getStatusManager ();
654
+ statusManager .add (new InfoStatus ("INFO STATUS MESSAGE" , getClass ()));
655
+ statusManager .add (new WarnStatus ("WARN STATUS MESSAGE" , getClass ()));
656
+ statusManager .add (new ErrorStatus ("ERROR STATUS MESSAGE" , getClass ()));
648
657
File file = new File (tmpDir (), "logback-test.log" );
649
658
LogFile logFile = getLogFile (file .getPath (), null );
650
659
initialize (this .initializationContext , null , logFile );
651
660
assertThat (output ).contains ("LevelChangePropagator" )
652
661
.contains ("SizeAndTimeBasedFileNamingAndTriggeringPolicy" )
653
- .contains ("DebugLogbackConfigurator" );
654
- LoggerContext loggerContext = this .logger .getLoggerContext ();
662
+ .contains ("DebugLogbackConfigurator" )
663
+ .contains ("INFO STATUS MESSAGE" )
664
+ .contains ("WARN STATUS MESSAGE" )
665
+ .contains ("ERROR STATUS MESSAGE" );
655
666
assertThat (loggerContext .getStatusManager ().getCopyOfStatusListenerList ()).allSatisfy ((listener ) -> {
656
667
assertThat (listener ).isInstanceOf (SystemStatusListener .class );
657
668
assertThat (listener ).hasFieldOrPropertyWithValue ("debug" , true );
@@ -663,7 +674,7 @@ void logbackDebugPropertyIsHonored(CapturedOutput output) {
663
674
}
664
675
665
676
@ Test
666
- void logbackErrorStatusListenerShouldBeRegistered (CapturedOutput output ) {
677
+ void logbackSystemStatusListenerShouldBeRegistered (CapturedOutput output ) {
667
678
this .loggingSystem .beforeInitialize ();
668
679
initialize (this .initializationContext , null , getLogFile (tmpDir () + "/tmp.log" , null ));
669
680
LoggerContext loggerContext = this .logger .getLoggerContext ();
@@ -680,7 +691,40 @@ void logbackErrorStatusListenerShouldBeRegistered(CapturedOutput output) {
680
691
}
681
692
682
693
@ Test
683
- void logbackErrorStatusListenerShouldBeRegisteredWhenUsingCustomLogbackXml (CapturedOutput output ) {
694
+ void logbackSystemStatusListenerShouldBeRegisteredOnlyOnce () {
695
+ this .loggingSystem .beforeInitialize ();
696
+ initialize (this .initializationContext , null , getLogFile (tmpDir () + "/tmp.log" , null ));
697
+ LoggerContext loggerContext = this .logger .getLoggerContext ();
698
+ SystemStatusListener .addTo (loggerContext );
699
+ SystemStatusListener .addTo (loggerContext , true );
700
+ assertThat (loggerContext .getStatusManager ().getCopyOfStatusListenerList ()).satisfiesOnlyOnce ((listener ) -> {
701
+ assertThat (listener ).isInstanceOf (SystemStatusListener .class );
702
+ assertThat (listener ).hasFieldOrPropertyWithValue ("debug" , false );
703
+ });
704
+ }
705
+
706
+ @ Test
707
+ void logbackSystemStatusListenerShouldBeRegisteredAndFilterStatusByLevelIfDebugDisabled (CapturedOutput output ) {
708
+ this .loggingSystem .beforeInitialize ();
709
+ LoggerContext loggerContext = this .logger .getLoggerContext ();
710
+ StatusManager statusManager = loggerContext .getStatusManager ();
711
+ statusManager .add (new InfoStatus ("INFO STATUS MESSAGE" , getClass ()));
712
+ statusManager .add (new WarnStatus ("WARN STATUS MESSAGE" , getClass ()));
713
+ statusManager .add (new ErrorStatus ("ERROR STATUS MESSAGE" , getClass ()));
714
+ initialize (this .initializationContext , null , getLogFile (tmpDir () + "/tmp.log" , null ));
715
+ assertThat (statusManager .getCopyOfStatusListenerList ()).allSatisfy ((listener ) -> {
716
+ assertThat (listener ).isInstanceOf (SystemStatusListener .class );
717
+ assertThat (listener ).hasFieldOrPropertyWithValue ("debug" , false );
718
+ });
719
+ this .logger .info ("Hello world" );
720
+ assertThat (output ).doesNotContain ("INFO STATUS MESSAGE" );
721
+ assertThat (output ).contains ("WARN STATUS MESSAGE" );
722
+ assertThat (output ).contains ("ERROR STATUS MESSAGE" );
723
+ assertThat (output ).contains ("Hello world" );
724
+ }
725
+
726
+ @ Test
727
+ void logbackSystemStatusListenerShouldBeRegisteredWhenUsingCustomLogbackXml (CapturedOutput output ) {
684
728
this .loggingSystem .beforeInitialize ();
685
729
initialize (this .initializationContext , "classpath:logback-include-defaults.xml" , null );
686
730
LoggerContext loggerContext = this .logger .getLoggerContext ();
0 commit comments