diff --git a/log4j-api/src/main/java/org/apache/logging/log4j/status/StatusLogger.java b/log4j-api/src/main/java/org/apache/logging/log4j/status/StatusLogger.java index 0bd91ce04cc..9ac9ca64244 100644 --- a/log4j-api/src/main/java/org/apache/logging/log4j/status/StatusLogger.java +++ b/log4j-api/src/main/java/org/apache/logging/log4j/status/StatusLogger.java @@ -293,7 +293,20 @@ private static Properties readPropertiesFile() { } } - private static volatile StatusLogger INSTANCE = new StatusLogger(); + /** + * Wrapper for the default instance for lazy initialization. + *
+ * The initialization will be performed when the JVM initializes the class. + * Since {@code InstanceHolder} has no other fields or methods, class initialization occurs when the {@code INSTANCE} field is first referenced. + *
+ * + * @see Double-checked locking: Clever, but broken + */ + private static final class InstanceHolder { + + private static volatile StatusLogger INSTANCE = new StatusLogger(); + + } private final Config config; @@ -351,7 +364,7 @@ public StatusLogger( * @return the singleton instance */ public static StatusLogger getLogger() { - return INSTANCE; + return InstanceHolder.INSTANCE; } /** @@ -363,7 +376,7 @@ public static StatusLogger getLogger() { * @since 2.23.0 */ public static void setLogger(final StatusLogger logger) { - INSTANCE = requireNonNull(logger, "logger"); + InstanceHolder.INSTANCE = requireNonNull(logger, "logger"); } /**