Skip to content

Commit 3684a2e

Browse files
committed
Initialize the default StatusLogger instance lazy
1 parent c4c941e commit 3684a2e

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

Diff for: log4j-api/src/main/java/org/apache/logging/log4j/status/StatusLogger.java

+16-3
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,20 @@ private static Properties readPropertiesFile() {
293293
}
294294
}
295295

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+
}
297310

298311
private final Config config;
299312

@@ -351,7 +364,7 @@ public StatusLogger(
351364
* @return the singleton instance
352365
*/
353366
public static StatusLogger getLogger() {
354-
return INSTANCE;
367+
return InstanceHolder.INSTANCE;
355368
}
356369

357370
/**
@@ -363,7 +376,7 @@ public static StatusLogger getLogger() {
363376
* @since 2.23.0
364377
*/
365378
public static void setLogger(final StatusLogger logger) {
366-
INSTANCE = requireNonNull(logger, "logger");
379+
InstanceHolder.INSTANCE = requireNonNull(logger, "logger");
367380
}
368381

369382
/**

0 commit comments

Comments
 (0)