-
Notifications
You must be signed in to change notification settings - Fork 40.2k
/
LoggingSystemProperties.java
318 lines (281 loc) · 12.8 KB
/
LoggingSystemProperties.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
/*
* Copyright 2012-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.logging;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.function.BiConsumer;
import java.util.function.Function;
import org.springframework.boot.system.ApplicationPid;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.Environment;
import org.springframework.core.env.PropertyResolver;
import org.springframework.core.env.PropertySourcesPropertyResolver;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
/**
* Utility to set system properties that can later be used by log configuration files.
*
* @author Andy Wilkinson
* @author Phillip Webb
* @author Madhura Bhave
* @author Vedran Pavic
* @author Robert Thornton
* @author Eddú Meléndez
* @author Jonatan Ivanov
* @since 2.0.0
* @see LoggingSystemProperty
*/
public class LoggingSystemProperties {
/**
* The name of the System property that contains the process ID.
* @deprecated since 3.2.0 for removal in 3.4.0 in favor of calling
* {@link LoggingSystemProperty#getEnvironmentVariableName()} on
* {@link LoggingSystemProperty#PID}
*/
@Deprecated(since = "3.2.0", forRemoval = true)
public static final String PID_KEY = LoggingSystemProperty.PID.getEnvironmentVariableName();
/**
* The name of the System property that contains the exception conversion word.
* @deprecated since 3.2.0 for removal in 3.4.0 in favor of calling
* {@link LoggingSystemProperty#getEnvironmentVariableName()} on
* {@link LoggingSystemProperty#EXCEPTION_CONVERSION_WORD}
*/
@Deprecated(since = "3.2.0", forRemoval = true)
public static final String EXCEPTION_CONVERSION_WORD = LoggingSystemProperty.EXCEPTION_CONVERSION_WORD
.getEnvironmentVariableName();
/**
* The name of the System property that contains the log file.
* @deprecated since 3.2.0 for removal in 3.4.0 in favor of calling
* {@link LoggingSystemProperty#getEnvironmentVariableName()} on
* {@link LoggingSystemProperty#LOG_FILE}
*/
@Deprecated(since = "3.2.0", forRemoval = true)
public static final String LOG_FILE = LoggingSystemProperty.LOG_FILE.getEnvironmentVariableName();
/**
* The name of the System property that contains the log path.
* @deprecated since 3.2.0 for removal in 3.4.0 in favor of calling
* {@link LoggingSystemProperty#getEnvironmentVariableName()} on
* {@link LoggingSystemProperty#LOG_PATH}
*/
@Deprecated(since = "3.2.0", forRemoval = true)
public static final String LOG_PATH = LoggingSystemProperty.LOG_PATH.getEnvironmentVariableName();
/**
* The name of the System property that contains the console log pattern.
* @deprecated since 3.2.0 for removal in 3.4.0 in favor of calling
* {@link LoggingSystemProperty#getEnvironmentVariableName()} on
* {@link LoggingSystemProperty#CONSOLE_PATTERN}
*/
@Deprecated(since = "3.2.0", forRemoval = true)
public static final String CONSOLE_LOG_PATTERN = LoggingSystemProperty.CONSOLE_PATTERN.getEnvironmentVariableName();
/**
* The name of the System property that contains the console log charset.
* @deprecated since 3.2.0 for removal in 3.4.0 in favor of calling
* {@link LoggingSystemProperty#getEnvironmentVariableName()} on
* {@link LoggingSystemProperty#CONSOLE_CHARSET}
*/
@Deprecated(since = "3.2.0", forRemoval = true)
public static final String CONSOLE_LOG_CHARSET = LoggingSystemProperty.CONSOLE_CHARSET.getEnvironmentVariableName();
/**
* The log level threshold for console log.
* @deprecated since 3.2.0 for removal in 3.4.0 in favor of calling
* {@link LoggingSystemProperty#getEnvironmentVariableName()} on
* {@link LoggingSystemProperty#CONSOLE_THRESHOLD}
*/
@Deprecated(since = "3.2.0", forRemoval = true)
public static final String CONSOLE_LOG_THRESHOLD = LoggingSystemProperty.CONSOLE_THRESHOLD
.getEnvironmentVariableName();
/**
* The name of the System property that contains the file log pattern.
* @deprecated since 3.2.0 for removal in 3.4.0 in favor of calling
* {@link LoggingSystemProperty#getEnvironmentVariableName()} on
* {@link LoggingSystemProperty#FILE_PATTERN}
*/
@Deprecated(since = "3.2.0", forRemoval = true)
public static final String FILE_LOG_PATTERN = LoggingSystemProperty.FILE_PATTERN.getEnvironmentVariableName();
/**
* The name of the System property that contains the file log charset.
* @deprecated since 3.2.0 for removal in 3.4.0 in favor of calling
* {@link LoggingSystemProperty#getEnvironmentVariableName()} on
* {@link LoggingSystemProperty#FILE_CHARSET}
*/
@Deprecated(since = "3.2.0", forRemoval = true)
public static final String FILE_LOG_CHARSET = LoggingSystemProperty.FILE_CHARSET.getEnvironmentVariableName();
/**
* The log level threshold for file log.
* @deprecated since 3.2.0 for removal in 3.4.0 in favor of calling
* {@link LoggingSystemProperty#getEnvironmentVariableName()} on
* {@link LoggingSystemProperty#FILE_THRESHOLD}
*/
@Deprecated(since = "3.2.0", forRemoval = true)
public static final String FILE_LOG_THRESHOLD = LoggingSystemProperty.FILE_THRESHOLD.getEnvironmentVariableName();
/**
* The name of the System property that contains the log level pattern.
* @deprecated since 3.2.0 for removal in 3.4.0 in favor of calling
* {@link LoggingSystemProperty#getEnvironmentVariableName()} on
* {@link LoggingSystemProperty#LEVEL_PATTERN}
*/
@Deprecated(since = "3.2.0", forRemoval = true)
public static final String LOG_LEVEL_PATTERN = LoggingSystemProperty.LEVEL_PATTERN.getEnvironmentVariableName();
/**
* The name of the System property that contains the log date-format pattern.
* @deprecated since 3.2.0 for removal in 3.4.0 in favor of calling
* {@link LoggingSystemProperty#getEnvironmentVariableName()} on
* {@link LoggingSystemProperty#DATEFORMAT_PATTERN}
*/
@Deprecated(since = "3.2.0", forRemoval = true)
public static final String LOG_DATEFORMAT_PATTERN = LoggingSystemProperty.DATEFORMAT_PATTERN
.getEnvironmentVariableName();
private static final BiConsumer<String, String> systemPropertySetter = (name, value) -> {
if (System.getProperty(name) == null && value != null) {
System.setProperty(name, value);
}
};
private final Environment environment;
private final Function<String, String> defaultValueResolver;
private final BiConsumer<String, String> setter;
/**
* Create a new {@link LoggingSystemProperties} instance.
* @param environment the source environment
*/
public LoggingSystemProperties(Environment environment) {
this(environment, null);
}
/**
* Create a new {@link LoggingSystemProperties} instance.
* @param environment the source environment
* @param setter setter used to apply the property or {@code null} for system
* properties
* @since 2.4.2
*/
public LoggingSystemProperties(Environment environment, BiConsumer<String, String> setter) {
this(environment, null, setter);
}
/**
* Create a new {@link LoggingSystemProperties} instance.
* @param environment the source environment
* @param defaultValueResolver function used to resolve default values or {@code null}
* @param setter setter used to apply the property or {@code null} for system
* properties
* @since 3.2.0
*/
public LoggingSystemProperties(Environment environment, Function<String, String> defaultValueResolver,
BiConsumer<String, String> setter) {
Assert.notNull(environment, "Environment must not be null");
this.environment = environment;
this.defaultValueResolver = (defaultValueResolver != null) ? defaultValueResolver : (name) -> null;
this.setter = (setter != null) ? setter : systemPropertySetter;
}
protected Charset getDefaultCharset() {
return StandardCharsets.UTF_8;
}
public final void apply() {
apply(null);
}
public final void apply(LogFile logFile) {
PropertyResolver resolver = getPropertyResolver();
apply(logFile, resolver);
}
private PropertyResolver getPropertyResolver() {
if (this.environment instanceof ConfigurableEnvironment configurableEnvironment) {
PropertySourcesPropertyResolver resolver = new PropertySourcesPropertyResolver(
configurableEnvironment.getPropertySources());
resolver.setConversionService(((ConfigurableEnvironment) this.environment).getConversionService());
resolver.setIgnoreUnresolvableNestedPlaceholders(true);
return resolver;
}
return this.environment;
}
protected void apply(LogFile logFile, PropertyResolver resolver) {
String defaultCharsetName = getDefaultCharset().name();
setApplicationNameSystemProperty(resolver);
setSystemProperty(LoggingSystemProperty.PID, new ApplicationPid().toString());
setSystemProperty(LoggingSystemProperty.CONSOLE_CHARSET, resolver, defaultCharsetName);
setSystemProperty(LoggingSystemProperty.FILE_CHARSET, resolver, defaultCharsetName);
setSystemProperty(LoggingSystemProperty.CONSOLE_THRESHOLD, resolver);
setSystemProperty(LoggingSystemProperty.FILE_THRESHOLD, resolver);
setSystemProperty(LoggingSystemProperty.EXCEPTION_CONVERSION_WORD, resolver);
setSystemProperty(LoggingSystemProperty.CONSOLE_PATTERN, resolver);
setSystemProperty(LoggingSystemProperty.FILE_PATTERN, resolver);
setSystemProperty(LoggingSystemProperty.LEVEL_PATTERN, resolver);
setSystemProperty(LoggingSystemProperty.DATEFORMAT_PATTERN, resolver);
setCorrelationPatternSystemProperty(resolver);
if (logFile != null) {
logFile.applyToSystemProperties();
}
}
private void setApplicationNameSystemProperty(PropertyResolver resolver) {
if (resolver.getProperty("logging.include-application-name", Boolean.class, Boolean.TRUE)) {
String applicationName = resolver.getProperty("spring.application.name");
if (StringUtils.hasText(applicationName)) {
setSystemProperty(LoggingSystemProperty.APPLICATION_NAME.getEnvironmentVariableName(),
"[%s] ".formatted(applicationName));
}
}
}
private void setCorrelationPatternSystemProperty(PropertyResolver resolver) {
if (resolver.getProperty(LoggingSystem.EXPECT_CORRELATION_ID_PROPERTY, Boolean.class, Boolean.FALSE)) {
setSystemProperty(LoggingSystemProperty.CORRELATION_PATTERN, resolver);
}
}
private void setSystemProperty(LoggingSystemProperty property, PropertyResolver resolver) {
setSystemProperty(property, resolver, null);
}
private void setSystemProperty(LoggingSystemProperty property, PropertyResolver resolver, String defaultValue) {
String value = (property.getApplicationPropertyName() != null)
? resolver.getProperty(property.getApplicationPropertyName()) : null;
value = (value != null) ? value : this.defaultValueResolver.apply(property.getApplicationPropertyName());
value = (value != null) ? value : defaultValue;
setSystemProperty(property.getEnvironmentVariableName(), value);
}
private void setSystemProperty(LoggingSystemProperty property, String value) {
setSystemProperty(property.getEnvironmentVariableName(), value);
}
/**
* Set a system property.
* @param resolver the resolver used to get the property value
* @param systemPropertyName the system property name
* @param propertyName the application property name
* @deprecated since 3.2.0 for removal in 3.4.0 with no replacement
*/
@Deprecated(since = "3.2.0", forRemoval = true)
protected final void setSystemProperty(PropertyResolver resolver, String systemPropertyName, String propertyName) {
setSystemProperty(resolver, systemPropertyName, propertyName, null);
}
/**
* Set a system property.
* @param resolver the resolver used to get the property value
* @param systemPropertyName the system property name
* @param propertyName the application property name
* @param defaultValue the default value if none can be resolved
* @deprecated since 3.2.0 for removal in 3.4.0 with no replacement
*/
@Deprecated(since = "3.2.0", forRemoval = true)
protected final void setSystemProperty(PropertyResolver resolver, String systemPropertyName, String propertyName,
String defaultValue) {
String value = resolver.getProperty(propertyName);
value = (value != null) ? value : this.defaultValueResolver.apply(systemPropertyName);
value = (value != null) ? value : defaultValue;
setSystemProperty(systemPropertyName, value);
}
/**
* Set a system property.
* @param name the property name
* @param value the value
*/
protected final void setSystemProperty(String name, String value) {
this.setter.accept(name, value);
}
}