16
16
17
17
package org .springframework .boot .logging .log4j2 ;
18
18
19
+ import java .io .File ;
20
+ import java .io .FileNotFoundException ;
19
21
import java .io .IOException ;
20
- import java .io . InputStream ;
22
+ import java .net . URISyntaxException ;
21
23
import java .net .URL ;
24
+ import java .net .URLConnection ;
22
25
import java .util .ArrayList ;
23
26
import java .util .Collections ;
24
27
import java .util .LinkedHashMap ;
43
46
import org .apache .logging .log4j .core .config .LoggerConfig ;
44
47
import org .apache .logging .log4j .core .config .composite .CompositeConfiguration ;
45
48
import org .apache .logging .log4j .core .filter .AbstractFilter ;
49
+ import org .apache .logging .log4j .core .net .UrlConnectionFactory ;
50
+ import org .apache .logging .log4j .core .net .ssl .SslConfiguration ;
51
+ import org .apache .logging .log4j .core .net .ssl .SslConfigurationFactory ;
52
+ import org .apache .logging .log4j .core .util .AuthorizationProvider ;
53
+ import org .apache .logging .log4j .core .util .FileUtils ;
46
54
import org .apache .logging .log4j .core .util .NameUtil ;
47
55
import org .apache .logging .log4j .jul .Log4jBridgeHandler ;
48
56
import org .apache .logging .log4j .message .Message ;
57
+ import org .apache .logging .log4j .status .StatusLogger ;
49
58
import org .apache .logging .log4j .util .PropertiesUtil ;
50
59
51
60
import org .springframework .boot .context .properties .bind .BindResult ;
@@ -82,13 +91,17 @@ public class Log4J2LoggingSystem extends AbstractLoggingSystem {
82
91
83
92
private static final String FILE_PROTOCOL = "file" ;
84
93
94
+ private static final String HTTPS = "https" ;
95
+
85
96
private static final String LOG4J_BRIDGE_HANDLER = "org.apache.logging.log4j.jul.Log4jBridgeHandler" ;
86
97
87
98
private static final String LOG4J_LOG_MANAGER = "org.apache.logging.log4j.jul.LogManager" ;
88
99
89
100
static final String ENVIRONMENT_KEY = Conventions .getQualifiedAttributeName (Log4J2LoggingSystem .class ,
90
101
"environment" );
91
102
103
+ private static org .apache .logging .log4j .Logger LOGGER = StatusLogger .getLogger ();
104
+
92
105
private static final LogLevels <Level > LEVELS = new LogLevels <>();
93
106
94
107
static {
@@ -280,11 +293,20 @@ protected void loadConfiguration(String location, LogFile logFile, List<String>
280
293
try {
281
294
List <Configuration > configurations = new ArrayList <>();
282
295
LoggerContext context = getLoggerContext ();
283
- configurations .add (load (location , context ));
296
+ Configuration configuration = load (location , context );
297
+ if (configuration != null ) {
298
+ configurations .add (load (location , context ));
299
+ }
300
+ else {
301
+ throw new FileNotFoundException ("Cannot locate file: " + location );
302
+ }
284
303
for (String override : overrides ) {
285
- configurations .add (load (override , context ));
304
+ configuration = load (override , context );
305
+ if (configuration != null ) {
306
+ configurations .add (configuration );
307
+ }
286
308
}
287
- Configuration configuration = (configurations .size () > 1 ) ? createComposite (configurations )
309
+ configuration = (configurations .size () > 1 ) ? createComposite (configurations )
288
310
: configurations .iterator ().next ();
289
311
context .start (configuration );
290
312
}
@@ -293,18 +315,29 @@ protected void loadConfiguration(String location, LogFile logFile, List<String>
293
315
}
294
316
}
295
317
296
- private Configuration load (String location , LoggerContext context ) throws IOException {
318
+ private Configuration load (String location , LoggerContext context ) throws IOException , URISyntaxException {
297
319
URL url = ResourceUtils .getURL (location );
298
320
ConfigurationSource source = getConfigurationSource (url );
299
- return ConfigurationFactory .getInstance ().getConfiguration (context , source );
321
+ return ( source != null ) ? ConfigurationFactory .getInstance ().getConfiguration (context , source ) : null ;
300
322
}
301
323
302
- private ConfigurationSource getConfigurationSource (URL url ) throws IOException {
303
- InputStream stream = url .openStream ();
304
- if (FILE_PROTOCOL .equals (url .getProtocol ())) {
305
- return new ConfigurationSource (stream , ResourceUtils .getFile (url ));
324
+ private ConfigurationSource getConfigurationSource (URL url ) throws IOException , URISyntaxException {
325
+ AuthorizationProvider provider = ConfigurationFactory .authorizationProvider (PropertiesUtil .getProperties ());
326
+ SslConfiguration sslConfiguration = url .getProtocol ().equals (HTTPS )
327
+ ? SslConfigurationFactory .getSslConfiguration () : null ;
328
+ URLConnection urlConnection = UrlConnectionFactory .createConnection (url , 0 , sslConfiguration , provider );
329
+
330
+ File file = FileUtils .fileFromUri (url .toURI ());
331
+ try {
332
+ if (file != null ) {
333
+ return new ConfigurationSource (urlConnection .getInputStream (), FileUtils .fileFromUri (url .toURI ()));
334
+ }
335
+ return new ConfigurationSource (urlConnection .getInputStream (), url , urlConnection .getLastModified ());
336
+ }
337
+ catch (FileNotFoundException ex ) {
338
+ LOGGER .info ("Unable to locate file {}, ignoring." , url .toString ());
339
+ return null ;
306
340
}
307
- return new ConfigurationSource (stream , url );
308
341
}
309
342
310
343
private CompositeConfiguration createComposite (List <Configuration > configurations ) {
@@ -332,7 +365,7 @@ private void reinitializeWithOverrides(List<String> overrides) {
332
365
try {
333
366
configurations .add ((AbstractConfiguration ) load (override , context ));
334
367
}
335
- catch (IOException ex ) {
368
+ catch (Exception ex ) {
336
369
throw new RuntimeException ("Failed to load overriding configuration from '" + override + "'" , ex );
337
370
}
338
371
}
0 commit comments