File tree 1 file changed +16
-3
lines changed
log4j-api/src/main/java/org/apache/logging/log4j/status
1 file changed +16
-3
lines changed Original file line number Diff line number Diff line change @@ -293,7 +293,20 @@ private static Properties readPropertiesFile() {
293
293
}
294
294
}
295
295
296
- private static volatile StatusLogger INSTANCE = new StatusLogger ();
296
+ /**
297
+ * Wrapper for the default instance for lazy initialization.
298
+ * <p>
299
+ * The initialization will be performed when the JVM initializes the class.
300
+ * Since {@code InstanceHolder} has no other fields or methods, class initialization occurs when the {@code INSTANCE} field is first referenced.
301
+ * </p>
302
+ *
303
+ * @see <a href="https://www.infoworld.com/article/2074979/double-checked-locking--clever--but-broken.html?page=2">Double-checked locking: Clever, but broken</a>
304
+ */
305
+ private static final class InstanceHolder {
306
+
307
+ private static volatile StatusLogger INSTANCE = new StatusLogger ();
308
+
309
+ }
297
310
298
311
private final Config config ;
299
312
@@ -351,7 +364,7 @@ public StatusLogger(
351
364
* @return the singleton instance
352
365
*/
353
366
public static StatusLogger getLogger () {
354
- return INSTANCE ;
367
+ return InstanceHolder . INSTANCE ;
355
368
}
356
369
357
370
/**
@@ -363,7 +376,7 @@ public static StatusLogger getLogger() {
363
376
* @since 2.23.0
364
377
*/
365
378
public static void setLogger (final StatusLogger logger ) {
366
- INSTANCE = requireNonNull (logger , "logger" );
379
+ InstanceHolder . INSTANCE = requireNonNull (logger , "logger" );
367
380
}
368
381
369
382
/**
You can’t perform that action at this time.
0 commit comments