1
1
/*
2
- * Copyright 2012-2022 the original author or authors.
2
+ * Copyright 2012-2023 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
16
16
17
17
package org .springframework .boot .logging ;
18
18
19
+ import java .util .Objects ;
20
+
19
21
import org .springframework .util .Assert ;
20
22
import org .springframework .util .ObjectUtils ;
21
23
22
24
/**
23
25
* Immutable class that represents the configuration of a {@link LoggingSystem}'s logger.
24
26
*
25
27
* @author Ben Hale
28
+ * @author Phillip Webb
26
29
* @since 1.5.0
27
30
*/
28
31
public final class LoggerConfiguration {
29
32
30
33
private final String name ;
31
34
32
- private final LogLevel configuredLevel ;
35
+ private final LevelConfiguration levelConfiguration ;
33
36
34
- private final LogLevel effectiveLevel ;
37
+ private final LevelConfiguration inheritedLevelConfiguration ;
35
38
36
39
/**
37
40
* Create a new {@link LoggerConfiguration instance}.
@@ -43,66 +46,204 @@ public LoggerConfiguration(String name, LogLevel configuredLevel, LogLevel effec
43
46
Assert .notNull (name , "Name must not be null" );
44
47
Assert .notNull (effectiveLevel , "EffectiveLevel must not be null" );
45
48
this .name = name ;
46
- this .configuredLevel = configuredLevel ;
47
- this .effectiveLevel = effectiveLevel ;
49
+ this .levelConfiguration = (configuredLevel != null ) ? LevelConfiguration .of (configuredLevel ) : null ;
50
+ this .inheritedLevelConfiguration = LevelConfiguration .of (effectiveLevel );
51
+ }
52
+
53
+ /**
54
+ * Create a new {@link LoggerConfiguration instance}.
55
+ * @param name the name of the logger
56
+ * @param levelConfiguration the level configuration
57
+ * @param inheritedLevelConfiguration the inherited level configuration
58
+ * @since 2.7.13
59
+ */
60
+ public LoggerConfiguration (String name , LevelConfiguration levelConfiguration ,
61
+ LevelConfiguration inheritedLevelConfiguration ) {
62
+ Assert .notNull (name , "Name must not be null" );
63
+ Assert .notNull (inheritedLevelConfiguration , "EffectiveLevelConfiguration must not be null" );
64
+ this .name = name ;
65
+ this .levelConfiguration = levelConfiguration ;
66
+ this .inheritedLevelConfiguration = inheritedLevelConfiguration ;
67
+ }
68
+
69
+ /**
70
+ * Returns the name of the logger.
71
+ * @return the name of the logger
72
+ */
73
+ public String getName () {
74
+ return this .name ;
48
75
}
49
76
50
77
/**
51
78
* Returns the configured level of the logger.
52
79
* @return the configured level of the logger
80
+ * @see #getLevelConfiguration(ConfigurationScope)
53
81
*/
54
82
public LogLevel getConfiguredLevel () {
55
- return this .configuredLevel ;
83
+ LevelConfiguration configuration = getLevelConfiguration (ConfigurationScope .DIRECT );
84
+ return (configuration != null ) ? configuration .getLevel () : null ;
56
85
}
57
86
58
87
/**
59
88
* Returns the effective level of the logger.
60
89
* @return the effective level of the logger
90
+ * @see #getLevelConfiguration(ConfigurationScope)
61
91
*/
62
92
public LogLevel getEffectiveLevel () {
63
- return this . effectiveLevel ;
93
+ return getLevelConfiguration (). getLevel () ;
64
94
}
65
95
66
96
/**
67
- * Returns the name of the logger.
68
- * @return the name of the logger
97
+ * Return the level configuration, considering inherited loggers.
98
+ * @return the level configuration
99
+ * @since 2.7.13
69
100
*/
70
- public String getName () {
71
- return this .name ;
101
+ public LevelConfiguration getLevelConfiguration () {
102
+ return getLevelConfiguration (ConfigurationScope .INHERITED );
103
+ }
104
+
105
+ /**
106
+ * Return the level configuration for the given scope.
107
+ * @param scope the configuration scope
108
+ * @return the level configuration or {@code null} for
109
+ * {@link ConfigurationScope#DIRECT direct scope} results without applied
110
+ * configuration
111
+ * @since 2.7.13
112
+ */
113
+ public LevelConfiguration getLevelConfiguration (ConfigurationScope scope ) {
114
+ return (scope != ConfigurationScope .DIRECT ) ? this .inheritedLevelConfiguration : this .levelConfiguration ;
72
115
}
73
116
74
117
@ Override
75
118
public boolean equals (Object obj ) {
76
119
if (this == obj ) {
77
120
return true ;
78
121
}
79
- if (obj == null ) {
122
+ if (obj == null || getClass () != obj . getClass () ) {
80
123
return false ;
81
124
}
82
- if (obj instanceof LoggerConfiguration other ) {
83
- boolean rtn = true ;
84
- rtn = rtn && ObjectUtils .nullSafeEquals (this .name , other .name );
85
- rtn = rtn && ObjectUtils .nullSafeEquals (this .configuredLevel , other .configuredLevel );
86
- rtn = rtn && ObjectUtils .nullSafeEquals (this .effectiveLevel , other .effectiveLevel );
87
- return rtn ;
88
- }
89
- return super .equals (obj );
125
+ LoggerConfiguration other = (LoggerConfiguration ) obj ;
126
+ return ObjectUtils .nullSafeEquals (this .name , other .name )
127
+ && ObjectUtils .nullSafeEquals (this .levelConfiguration , other .levelConfiguration )
128
+ && ObjectUtils .nullSafeEquals (this .inheritedLevelConfiguration , other .inheritedLevelConfiguration );
90
129
}
91
130
92
131
@ Override
93
132
public int hashCode () {
94
- final int prime = 31 ;
95
- int result = 1 ;
96
- result = prime * result + ObjectUtils .nullSafeHashCode (this .name );
97
- result = prime * result + ObjectUtils .nullSafeHashCode (this .configuredLevel );
98
- result = prime * result + ObjectUtils .nullSafeHashCode (this .effectiveLevel );
99
- return result ;
133
+ return Objects .hash (this .name , this .levelConfiguration , this .inheritedLevelConfiguration );
100
134
}
101
135
102
136
@ Override
103
137
public String toString () {
104
- return "LoggerConfiguration [name=" + this .name + ", configuredLevel=" + this .configuredLevel
105
- + ", effectiveLevel=" + this .effectiveLevel + "]" ;
138
+ return "LoggerConfiguration [name=" + this .name + ", levelConfiguration=" + this .levelConfiguration
139
+ + ", inheritedLevelConfiguration=" + this .inheritedLevelConfiguration + "]" ;
140
+ }
141
+
142
+ /**
143
+ * Supported logger configurations scopes.
144
+ *
145
+ * @since 2.7.13
146
+ */
147
+ public enum ConfigurationScope {
148
+
149
+ /**
150
+ * Only return configuration that has been applied directly applied. Often
151
+ * referred to as 'configured' or 'assigned' configuration.
152
+ */
153
+ DIRECT ,
154
+
155
+ /**
156
+ * May return configuration that has been applied to a parent logger. Often
157
+ * referred to as 'effective' configuration.
158
+ */
159
+ INHERITED
160
+
161
+ }
162
+
163
+ /**
164
+ * Logger level configuration.
165
+ *
166
+ * @since 2.7.13
167
+ */
168
+ public static final class LevelConfiguration {
169
+
170
+ private final String name ;
171
+
172
+ private final LogLevel logLevel ;
173
+
174
+ private LevelConfiguration (String name , LogLevel logLevel ) {
175
+ this .name = name ;
176
+ this .logLevel = logLevel ;
177
+ }
178
+
179
+ /**
180
+ * Return the name of the level.
181
+ * @return the level name
182
+ */
183
+ public String getName () {
184
+ return this .name ;
185
+ }
186
+
187
+ /**
188
+ * Return the actual level value if possible.
189
+ * @return the level value
190
+ * @throws IllegalStateException if this is a {@link #isCustom() custom} level
191
+ */
192
+ public LogLevel getLevel () {
193
+ Assert .state (this .logLevel != null , "Unable to provide LogLevel for '" + this .name + "'" );
194
+ return this .logLevel ;
195
+ }
196
+
197
+ /**
198
+ * Return if this is a custom level and cannot be represented by {@link LogLevel}.
199
+ * @return if this is a custom level
200
+ */
201
+ public boolean isCustom () {
202
+ return this .logLevel == null ;
203
+ }
204
+
205
+ @ Override
206
+ public boolean equals (Object obj ) {
207
+ if (this == obj ) {
208
+ return true ;
209
+ }
210
+ if (obj == null || getClass () != obj .getClass ()) {
211
+ return false ;
212
+ }
213
+ LevelConfiguration other = (LevelConfiguration ) obj ;
214
+ return this .logLevel == other .logLevel && ObjectUtils .nullSafeEquals (this .name , other .name );
215
+ }
216
+
217
+ @ Override
218
+ public int hashCode () {
219
+ return Objects .hash (this .logLevel , this .name );
220
+ }
221
+
222
+ @ Override
223
+ public String toString () {
224
+ return "LevelConfiguration [name=" + this .name + ", logLevel=" + this .logLevel + "]" ;
225
+ }
226
+
227
+ /**
228
+ * Create a new {@link LevelConfiguration} instance of the given {@link LogLevel}.
229
+ * @param logLevel the log level
230
+ * @return a new {@link LevelConfiguration} instance
231
+ */
232
+ public static LevelConfiguration of (LogLevel logLevel ) {
233
+ Assert .notNull (logLevel , "LogLevel must not be null" );
234
+ return new LevelConfiguration (logLevel .name (), logLevel );
235
+ }
236
+
237
+ /**
238
+ * Create a new {@link LevelConfiguration} instance for a custom level name.
239
+ * @param name the log level name
240
+ * @return a new {@link LevelConfiguration} instance
241
+ */
242
+ public static LevelConfiguration ofCustom (String name ) {
243
+ Assert .hasText (name , "Name must not be empty" );
244
+ return new LevelConfiguration (name , null );
245
+ }
246
+
106
247
}
107
248
108
249
}
0 commit comments