47
47
import org .apache .log4j .spi .RendererSupport ;
48
48
import org .apache .log4j .spi .ThrowableRenderer ;
49
49
import org .apache .log4j .spi .ThrowableRendererSupport ;
50
- import org .apache .logging .log4j .core .config .Configuration ;
50
+ import org .apache .logging .log4j .core .config .ConfigurationFactory ;
51
51
import org .apache .logging .log4j .core .net .UrlConnectionFactory ;
52
+ import org .apache .logging .log4j .util .PropertiesUtil ;
52
53
import org .apache .logging .log4j .util .StackLocatorUtil ;
53
54
54
55
/**
@@ -119,15 +120,12 @@ public Object nextElement() {
119
120
}
120
121
}
121
122
122
- static final String CATEGORY_PREFIX = "log4j.category." ;
123
- static final String LOGGER_PREFIX = "log4j.logger." ;
124
- static final String FACTORY_PREFIX = "log4j.factory" ;
125
- static final String ADDITIVITY_PREFIX = "log4j.additivity." ;
126
- static final String ROOT_CATEGORY_PREFIX = "log4j.rootCategory" ;
127
- static final String ROOT_LOGGER_PREFIX = "log4j.rootLogger" ;
128
- static final String APPENDER_PREFIX = "log4j.appender." ;
129
- static final String RENDERER_PREFIX = "log4j.renderer." ;
130
- static final String THRESHOLD_PREFIX = "log4j.threshold" ;
123
+ private static final String CATEGORY_PREFIX = "log4j.category." ;
124
+ private static final String LOGGER_PREFIX = "log4j.logger." ;
125
+ private static final String FACTORY_PREFIX = "log4j.factory" ;
126
+ private static final String ADDITIVITY_PREFIX = "log4j.additivity." ;
127
+ private static final String APPENDER_PREFIX = "log4j.appender." ;
128
+ private static final String RENDERER_PREFIX = "log4j.renderer." ;
131
129
132
130
private static final String THROWABLE_RENDERER_PREFIX = "log4j.throwableRenderer" ;
133
131
private static final String LOGGER_REF = "logger-ref" ;
@@ -147,6 +145,16 @@ public Object nextElement() {
147
145
148
146
private static final String INTERNAL_ROOT_NAME = "root" ;
149
147
148
+ private static boolean isFullCompatibilityEnabled () {
149
+ return PropertiesUtil .getProperties ().getBooleanProperty (ConfigurationFactory .LOG4J1_EXPERIMENTAL );
150
+ }
151
+
152
+ private static void warnFullCompatibilityDisabled () {
153
+ LogLog .warn (
154
+ "Ignoring `PropertyConfigurator` call, since `log4j1.compatibility` is not enabled.\n "
155
+ + "See https://logging.staged.apache.org/log4j/2.x/migrate-from-log4j1.html#log4j1.compatibility for details." );
156
+ }
157
+
150
158
/**
151
159
* Reads configuration options from an InputStream.
152
160
*
@@ -212,15 +220,15 @@ public static void configureAndWatch(final String configFilename, final long del
212
220
configureAndWatch (configFilename , delayMillis , StackLocatorUtil .getCallerClassLoader (2 ));
213
221
}
214
222
215
- static void configureAndWatch (final String configFilename , final long delay , final ClassLoader classLoader ) {
216
- final PropertyWatchdog watchdog = new PropertyWatchdog ( configFilename , classLoader );
217
- watchdog . setDelay ( delay );
218
- watchdog . start ( );
219
- }
220
-
221
- private static Configuration reconfigure ( final Configuration configuration ) {
222
- org . apache . logging . log4j . core . config . Configurator . reconfigure ( configuration );
223
- return configuration ;
223
+ private static void configureAndWatch (
224
+ final String configFilename , final long delay , final ClassLoader classLoader ) {
225
+ if ( isFullCompatibilityEnabled ()) {
226
+ final PropertyWatchdog watchdog = new PropertyWatchdog ( configFilename , classLoader );
227
+ watchdog . setDelay ( delay );
228
+ watchdog . start ();
229
+ } else {
230
+ warnFullCompatibilityDisabled ( );
231
+ }
224
232
}
225
233
226
234
/**
@@ -240,31 +248,16 @@ private static Configuration reconfigure(final Configuration configuration) {
240
248
* @see #parseCatsAndRenderers
241
249
*/
242
250
protected void configureLoggerFactory (final Properties properties ) {
243
- final String factoryClassName = OptionConverter .findAndSubst (LOGGER_FACTORY_KEY , properties );
244
- if (factoryClassName != null ) {
245
- LogLog .debug ("Setting category factory to [" + factoryClassName + "]." );
246
- loggerFactory = (LoggerFactory )
247
- OptionConverter .instantiateByClassName (factoryClassName , LoggerFactory .class , loggerFactory );
248
- PropertySetter .setProperties (loggerFactory , properties , FACTORY_PREFIX + "." );
249
- }
250
- }
251
-
252
- void configureRootCategory (final Properties properties , final LoggerRepository loggerRepository ) {
253
- String effectiveFrefix = ROOT_LOGGER_PREFIX ;
254
- String value = OptionConverter .findAndSubst (ROOT_LOGGER_PREFIX , properties );
255
-
256
- if (value == null ) {
257
- value = OptionConverter .findAndSubst (ROOT_CATEGORY_PREFIX , properties );
258
- effectiveFrefix = ROOT_CATEGORY_PREFIX ;
259
- }
260
-
261
- if (value == null ) {
262
- LogLog .debug ("Could not find root logger information. Is this OK?" );
263
- } else {
264
- final Logger root = loggerRepository .getRootLogger ();
265
- synchronized (root ) {
266
- parseCategory (properties , root , effectiveFrefix , INTERNAL_ROOT_NAME , value );
251
+ if (isFullCompatibilityEnabled ()) {
252
+ final String factoryClassName = OptionConverter .findAndSubst (LOGGER_FACTORY_KEY , properties );
253
+ if (factoryClassName != null ) {
254
+ LogLog .debug ("Setting category factory to [" + factoryClassName + "]." );
255
+ loggerFactory = (LoggerFactory )
256
+ OptionConverter .instantiateByClassName (factoryClassName , LoggerFactory .class , loggerFactory );
257
+ PropertySetter .setProperties (loggerFactory , properties , FACTORY_PREFIX + "." );
267
258
}
259
+ } else {
260
+ warnFullCompatibilityDisabled ();
268
261
}
269
262
}
270
263
@@ -279,9 +272,9 @@ public void doConfigure(final InputStream inputStream, final LoggerRepository lo
279
272
doConfigure (inputStream , loggerRepository , StackLocatorUtil .getCallerClassLoader (2 ));
280
273
}
281
274
282
- Configuration doConfigure (
275
+ private void doConfigure (
283
276
final InputStream inputStream , final LoggerRepository loggerRepository , final ClassLoader classLoader ) {
284
- return doConfigure (loadProperties (inputStream ), loggerRepository , classLoader );
277
+ doConfigure (loadProperties (inputStream ), loggerRepository , classLoader );
285
278
}
286
279
287
280
/**
@@ -298,54 +291,29 @@ public void doConfigure(final Properties properties, final LoggerRepository logg
298
291
299
292
/**
300
293
* Reads configuration options from <code>properties</code>.
301
- *
294
+ * <p>
302
295
* See {@link #doConfigure(String, LoggerRepository)} for the expected format.
303
296
*
304
- * @param properties The properties
297
+ * @param properties The properties
305
298
* @param loggerRepository The hierarchy
306
299
*/
307
- Configuration doConfigure (
300
+ private void doConfigure (
308
301
final Properties properties , final LoggerRepository loggerRepository , final ClassLoader classLoader ) {
309
- final PropertiesConfiguration configuration =
310
- new PropertiesConfiguration (LogManager .getContext (classLoader ), properties );
311
- configuration .doConfigure ();
312
-
313
- repository = loggerRepository ;
314
- // String value = properties.getProperty(LogLog.DEBUG_KEY);
315
- // if (value == null) {
316
- // value = properties.getProperty("log4j.configDebug");
317
- // if (value != null) {
318
- // LogLog.warn("[log4j.configDebug] is deprecated. Use [log4j.debug] instead.");
319
- // }
320
- // }
321
- //
322
- // if (value != null) {
323
- // LogLog.setInternalDebugging(OptionConverter.toBoolean(value, true));
324
- // }
325
- //
326
- // //
327
- // // if log4j.reset=true then
328
- // // reset hierarchy
329
- // final String reset = properties.getProperty(RESET_KEY);
330
- // if (reset != null && OptionConverter.toBoolean(reset, false)) {
331
- // hierarchy.resetConfiguration();
332
- // }
333
- //
334
- // final String thresholdStr = OptionConverter.findAndSubst(THRESHOLD_PREFIX, properties);
335
- // if (thresholdStr != null) {
336
- // hierarchy.setThreshold(OptionConverter.toLevel(thresholdStr, (Level) Level.ALL));
337
- // LogLog.debug("Hierarchy threshold set to [" + hierarchy.getThreshold() + "].");
338
- // }
339
- //
340
- // configureRootCategory(properties, hierarchy);
341
- // configureLoggerFactory(properties);
342
- // parseCatsAndRenderers(properties, hierarchy);
343
- //
344
- // We don't want to hold references to appenders preventing their
345
- // garbage collection.
346
- registry .clear ();
347
-
348
- return reconfigure (configuration );
302
+ if (isFullCompatibilityEnabled ()) {
303
+ final PropertiesConfiguration configuration =
304
+ new PropertiesConfiguration (LogManager .getContext (classLoader ), properties );
305
+ configuration .doConfigure ();
306
+
307
+ repository = loggerRepository ;
308
+
309
+ // We don't want to hold references to appenders preventing their
310
+ // garbage collection.
311
+ registry .clear ();
312
+
313
+ org .apache .logging .log4j .core .config .Configurator .reconfigure (configuration );
314
+ } else {
315
+ warnFullCompatibilityDisabled ();
316
+ }
349
317
}
350
318
351
319
/**
@@ -365,17 +333,20 @@ public void doConfigure(final String fileName, final LoggerRepository loggerRepo
365
333
* @param loggerRepository The hierarchy
366
334
*/
367
335
@ SuppressFBWarnings (value = "PATH_TRAVERSAL_IN" , justification = "The filename comes from a system property." )
368
- Configuration doConfigure (
336
+ private void doConfigure (
369
337
final String fileName , final LoggerRepository loggerRepository , final ClassLoader classLoader ) {
370
- try (final InputStream inputStream = Files .newInputStream (Paths .get (fileName ))) {
371
- return doConfigure (inputStream , loggerRepository , classLoader );
372
- } catch (final Exception e ) {
373
- if (e instanceof InterruptedIOException || e instanceof InterruptedException ) {
374
- Thread .currentThread ().interrupt ();
338
+ if (isFullCompatibilityEnabled ()) {
339
+ try (final InputStream inputStream = Files .newInputStream (Paths .get (fileName ))) {
340
+ doConfigure (inputStream , loggerRepository , classLoader );
341
+ } catch (final Exception e ) {
342
+ if (e instanceof InterruptedIOException ) {
343
+ Thread .currentThread ().interrupt ();
344
+ }
345
+ LogLog .error ("Could not read configuration file [" + fileName + "]." , e );
346
+ LogLog .error ("Ignoring configuration file [" + fileName + "]." );
375
347
}
376
- LogLog .error ("Could not read configuration file [" + fileName + "]." , e );
377
- LogLog .error ("Ignoring configuration file [" + fileName + "]." );
378
- return null ;
348
+ } else {
349
+ warnFullCompatibilityDisabled ();
379
350
}
380
351
}
381
352
@@ -390,17 +361,20 @@ public void doConfigure(final URL url, final LoggerRepository loggerRepository)
390
361
doConfigure (url , loggerRepository , StackLocatorUtil .getCallerClassLoader (2 ));
391
362
}
392
363
393
- Configuration doConfigure (final URL url , final LoggerRepository loggerRepository , final ClassLoader classLoader ) {
394
- LogLog .debug ("Reading configuration from URL " + url );
395
- try {
396
- final URLConnection urlConnection = UrlConnectionFactory .createConnection (url );
397
- try (final InputStream inputStream = urlConnection .getInputStream ()) {
398
- return doConfigure (inputStream , loggerRepository , classLoader );
364
+ private void doConfigure (final URL url , final LoggerRepository loggerRepository , final ClassLoader classLoader ) {
365
+ if (isFullCompatibilityEnabled ()) {
366
+ LogLog .debug ("Reading configuration from URL " + url );
367
+ try {
368
+ final URLConnection urlConnection = UrlConnectionFactory .createConnection (url );
369
+ try (final InputStream inputStream = urlConnection .getInputStream ()) {
370
+ doConfigure (inputStream , loggerRepository , classLoader );
371
+ }
372
+ } catch (final IOException e ) {
373
+ LogLog .error ("Could not read configuration file from URL [" + url + "]." , e );
374
+ LogLog .error ("Ignoring configuration file [" + url + "]." );
399
375
}
400
- } catch (final IOException e ) {
401
- LogLog .error ("Could not read configuration file from URL [" + url + "]." , e );
402
- LogLog .error ("Ignoring configuration file [" + url + "]." );
403
- return null ;
376
+ } else {
377
+ warnFullCompatibilityDisabled ();
404
378
}
405
379
}
406
380
@@ -422,7 +396,7 @@ private Properties loadProperties(final InputStream inputStream) {
422
396
/**
423
397
* Parse the additivity option for a non-root category.
424
398
*/
425
- void parseAdditivityForLogger (final Properties properties , final Logger logger , final String loggerName ) {
399
+ private void parseAdditivityForLogger (final Properties properties , final Logger logger , final String loggerName ) {
426
400
final String value = OptionConverter .findAndSubst (ADDITIVITY_PREFIX + loggerName , properties );
427
401
LogLog .debug ("Handling " + ADDITIVITY_PREFIX + loggerName + "=[" + value + "]" );
428
402
// touch additivity only if necessary
@@ -433,8 +407,8 @@ void parseAdditivityForLogger(final Properties properties, final Logger logger,
433
407
}
434
408
}
435
409
436
- Appender parseAppender (final Properties properties , final String appenderName ) {
437
- Appender appender = registryGet (appenderName );
410
+ private Appender parseAppender (final Properties properties , final String appenderName ) {
411
+ Appender appender = ( Appender ) registry . get (appenderName );
438
412
if ((appender != null )) {
439
413
LogLog .debug ("Appender \" " + appenderName + "\" was already parsed." );
440
414
return appender ;
@@ -499,11 +473,11 @@ Appender parseAppender(final Properties properties, final String appenderName) {
499
473
LogLog .debug ("Parsed \" " + appenderName + "\" options." );
500
474
}
501
475
parseAppenderFilters (properties , appenderName , appender );
502
- registryPut ( appender );
476
+ registry . put ( appender . getName (), appender );
503
477
return appender ;
504
478
}
505
479
506
- void parseAppenderFilters (final Properties properties , final String appenderName , final Appender appender ) {
480
+ private void parseAppenderFilters (final Properties properties , final String appenderName , final Appender appender ) {
507
481
// extract filters and filter options from props into a hashtable mapping
508
482
// the property name defining the filter class to a list of pre-parsed
509
483
// name-value pairs associated to that filter
@@ -567,7 +541,7 @@ void parseAppenderFilters(final Properties properties, final String appenderName
567
541
/**
568
542
* This method must work for the root category as well.
569
543
*/
570
- void parseCategory (
544
+ private void parseCategory (
571
545
final Properties properties ,
572
546
final Logger logger ,
573
547
final String optionKey ,
@@ -628,6 +602,10 @@ void parseCategory(
628
602
* Parse non-root elements, such non-root categories and renderers.
629
603
*/
630
604
protected void parseCatsAndRenderers (final Properties properties , final LoggerRepository loggerRepository ) {
605
+ if (!isFullCompatibilityEnabled ()) {
606
+ warnFullCompatibilityDisabled ();
607
+ return ;
608
+ }
631
609
final Enumeration enumeration = properties .propertyNames ();
632
610
while (enumeration .hasMoreElements ()) {
633
611
final String key = (String ) enumeration .nextElement ();
@@ -693,12 +671,4 @@ private void parseErrorHandler(
693
671
}
694
672
}
695
673
}
696
-
697
- Appender registryGet (final String name ) {
698
- return (Appender ) registry .get (name );
699
- }
700
-
701
- void registryPut (final Appender appender ) {
702
- registry .put (appender .getName (), appender );
703
- }
704
674
}
0 commit comments