28
28
import org .springframework .boot .actuate .logging .LoggersEndpoint .SingleLoggerLevels ;
29
29
import org .springframework .boot .logging .LogLevel ;
30
30
import org .springframework .boot .logging .LoggerConfiguration ;
31
+ import org .springframework .boot .logging .LoggingGroups ;
31
32
import org .springframework .boot .logging .LoggingSystem ;
32
33
33
34
import static org .assertj .core .api .Assertions .assertThat ;
40
41
*
41
42
* @author Ben Hale
42
43
* @author Andy Wilkinson
44
+ * @author HaiTao Zhang
43
45
*/
44
46
class LoggersEndpointTests {
45
47
46
48
private final LoggingSystem loggingSystem = mock (LoggingSystem .class );
47
49
50
+ private final LoggingGroups loggingGroups = mock (LoggingGroups .class );
51
+
48
52
@ Test
49
53
@ SuppressWarnings ("unchecked" )
50
54
void loggersShouldReturnLoggerConfigurationsWithNoLoggerGroups () {
51
55
given (this .loggingSystem .getLoggerConfigurations ())
52
56
.willReturn (Collections .singletonList (new LoggerConfiguration ("ROOT" , null , LogLevel .DEBUG )));
53
57
given (this .loggingSystem .getSupportedLogLevels ()).willReturn (EnumSet .allOf (LogLevel .class ));
54
- given (this .loggingSystem .getLoggerGroupNames ()).willReturn (null );
55
- Map <String , Object > result = new LoggersEndpoint (this .loggingSystem ).loggers ();
58
+ given (this .loggingGroups .getLoggerGroupNames ()).willReturn (null );
59
+ Map <String , Object > result = new LoggersEndpoint (this .loggingSystem , this . loggingGroups ).loggers ();
56
60
Map <String , LoggerLevels > loggers = (Map <String , LoggerLevels >) result .get ("loggers" );
57
61
Set <LogLevel > levels = (Set <LogLevel >) result .get ("levels" );
58
62
SingleLoggerLevels rootLevels = (SingleLoggerLevels ) loggers .get ("ROOT" );
@@ -69,10 +73,10 @@ void loggersShouldReturnLoggerConfigurationsWithLoggerGroups() {
69
73
given (this .loggingSystem .getLoggerConfigurations ())
70
74
.willReturn (Collections .singletonList (new LoggerConfiguration ("ROOT" , null , LogLevel .DEBUG )));
71
75
given (this .loggingSystem .getSupportedLogLevels ()).willReturn (EnumSet .allOf (LogLevel .class ));
72
- given (this .loggingSystem .getLoggerGroup ("test" )).willReturn (Collections .singletonList ("test.member" ));
73
- given (this .loggingSystem .getLoggerGroupNames ()).willReturn (Collections .singleton ("test" ));
74
- given (this .loggingSystem .getLoggerGroupConfiguredLevel ("test" )).willReturn (LogLevel .DEBUG );
75
- Map <String , Object > result = new LoggersEndpoint (this .loggingSystem ).loggers ();
76
+ given (this .loggingGroups .getLoggerGroup ("test" )).willReturn (Collections .singletonList ("test.member" ));
77
+ given (this .loggingGroups .getLoggerGroupNames ()).willReturn (Collections .singleton ("test" ));
78
+ given (this .loggingGroups .getLoggerGroupConfiguredLevel ("test" )).willReturn (LogLevel .DEBUG );
79
+ Map <String , Object > result = new LoggersEndpoint (this .loggingSystem , this . loggingGroups ).loggers ();
76
80
Map <String , LoggerLevels > loggerGroups = (Map <String , LoggerLevels >) result .get ("groups" );
77
81
GroupLoggerLevels testLoggerLevel = (GroupLoggerLevels ) loggerGroups .get ("test" );
78
82
Map <String , LoggerLevels > loggers = (Map <String , LoggerLevels >) result .get ("loggers" );
@@ -90,48 +94,54 @@ void loggersShouldReturnLoggerConfigurationsWithLoggerGroups() {
90
94
91
95
@ Test
92
96
void loggerLevelsWhenNameSpecifiedShouldReturnLevels () {
97
+ given (this .loggingGroups .isGroup ("ROOT" )).willReturn (false );
93
98
given (this .loggingSystem .getLoggerConfiguration ("ROOT" ))
94
99
.willReturn (new LoggerConfiguration ("ROOT" , null , LogLevel .DEBUG ));
95
- SingleLoggerLevels levels = (SingleLoggerLevels ) new LoggersEndpoint (this .loggingSystem ).loggerLevels ("ROOT" );
100
+ SingleLoggerLevels levels = (SingleLoggerLevels ) new LoggersEndpoint (this .loggingSystem , this .loggingGroups )
101
+ .loggerLevels ("ROOT" );
96
102
assertThat (levels .getConfiguredLevel ()).isNull ();
97
103
assertThat (levels .getEffectiveLevel ()).isEqualTo ("DEBUG" );
98
104
}
99
105
100
106
@ Test
101
107
void groupNameSpecifiedShouldReturnConfiguredLevelAndMembers () {
102
- given (this .loggingSystem .getLoggerGroup ("test" )).willReturn (Collections .singletonList ("test.member" ));
103
- given (this .loggingSystem .getLoggerGroupConfiguredLevel ("test" )).willReturn (LogLevel .DEBUG );
104
- GroupLoggerLevels levels = (GroupLoggerLevels ) new LoggersEndpoint (this .loggingSystem ).loggerLevels ("test" );
108
+ given (this .loggingGroups .isGroup ("test" )).willReturn (true );
109
+ given (this .loggingGroups .getLoggerGroup ("test" )).willReturn (Collections .singletonList ("test.member" ));
110
+ given (this .loggingGroups .getLoggerGroupConfiguredLevel ("test" )).willReturn (LogLevel .DEBUG );
111
+ GroupLoggerLevels levels = (GroupLoggerLevels ) new LoggersEndpoint (this .loggingSystem , this .loggingGroups )
112
+ .loggerLevels ("test" );
105
113
assertThat (levels .getConfiguredLevel ()).isEqualTo ("DEBUG" );
106
114
assertThat (levels .getMembers ()).isEqualTo (Collections .singletonList ("test.member" ));
107
115
}
108
116
109
117
@ Test
110
118
void configureLogLevelShouldSetLevelOnLoggingSystem () {
111
- given (this .loggingSystem .getLoggerGroup ("ROOT" )).willReturn (null );
112
- new LoggersEndpoint (this .loggingSystem ).configureLogLevel ("ROOT" , LogLevel .DEBUG );
119
+ given (this .loggingGroups .getLoggerGroup ("ROOT" )).willReturn (null );
120
+ new LoggersEndpoint (this .loggingSystem , this . loggingGroups ).configureLogLevel ("ROOT" , LogLevel .DEBUG );
113
121
verify (this .loggingSystem ).setLogLevel ("ROOT" , LogLevel .DEBUG );
114
122
}
115
123
116
124
@ Test
117
125
void configureLogLevelWithNullSetsLevelOnLoggingSystemToNull () {
118
- given (this .loggingSystem .getLoggerGroup ("ROOT" )).willReturn (null );
119
- new LoggersEndpoint (this .loggingSystem ).configureLogLevel ("ROOT" , null );
126
+ given (this .loggingGroups .getLoggerGroup ("ROOT" )).willReturn (null );
127
+ new LoggersEndpoint (this .loggingSystem , this . loggingGroups ).configureLogLevel ("ROOT" , null );
120
128
verify (this .loggingSystem ).setLogLevel ("ROOT" , null );
121
129
}
122
130
123
131
@ Test
124
132
void configureLogLevelInLoggerGroupShouldSetLevelOnLoggingSystem () {
125
- given (this .loggingSystem .getLoggerGroup ("test" )).willReturn (Collections .singletonList ("test.member" ));
126
- new LoggersEndpoint (this .loggingSystem ).configureLogLevel ("test" , LogLevel .DEBUG );
127
- verify (this .loggingSystem ).setLoggerGroupLevel ("test" , LogLevel .DEBUG );
133
+ given (this .loggingGroups .isGroup ("test" )).willReturn (true );
134
+ given (this .loggingGroups .getLoggerGroup ("test" )).willReturn (Collections .singletonList ("test.member" ));
135
+ new LoggersEndpoint (this .loggingSystem , this .loggingGroups ).configureLogLevel ("test" , LogLevel .DEBUG );
136
+ verify (this .loggingGroups ).setLoggerGroupLevel ("test" , LogLevel .DEBUG );
128
137
}
129
138
130
139
@ Test
131
140
void configureLogLevelWithNullInLoggerGroupShouldSetLevelOnLoggingSystem () {
132
- given (this .loggingSystem .getLoggerGroup ("test" )).willReturn (Collections .singletonList ("test.member" ));
133
- new LoggersEndpoint (this .loggingSystem ).configureLogLevel ("test" , null );
134
- verify (this .loggingSystem ).setLoggerGroupLevel ("test" , null );
141
+ given (this .loggingGroups .isGroup ("test" )).willReturn (true );
142
+ given (this .loggingGroups .getLoggerGroup ("test" )).willReturn (Collections .singletonList ("test.member" ));
143
+ new LoggersEndpoint (this .loggingSystem , this .loggingGroups ).configureLogLevel ("test" , null );
144
+ verify (this .loggingGroups ).setLoggerGroupLevel ("test" , null );
135
145
}
136
146
137
147
// @Test
0 commit comments