Skip to content

Commit 9b47ac7

Browse files
committed
Use deprecation logger holder in byte size value (#53928)
If a setting is touched during bootstrap before logging is configured, and that setting uses a byte size value, the deprecation logger for ByteSizeValue will be initialized. However, this means a logger will be configured before log4j is initialized, which we reject at startup. This commit puts this deprecation logger in a holder pattern so that it is not initialized until first use, which will happen after logging is configured.
1 parent 756a297 commit 9b47ac7

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

server/src/main/java/org/elasticsearch/common/unit/ByteSizeValue.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
import org.elasticsearch.common.io.stream.StreamOutput;
2727
import org.elasticsearch.common.io.stream.Writeable;
2828
import org.elasticsearch.common.logging.DeprecationLogger;
29+
import org.elasticsearch.common.logging.LogConfigurator;
30+
import org.elasticsearch.common.settings.Setting;
2931
import org.elasticsearch.common.xcontent.ToXContentFragment;
3032
import org.elasticsearch.common.xcontent.XContentBuilder;
3133

@@ -35,7 +37,14 @@
3537

3638
public class ByteSizeValue implements Writeable, Comparable<ByteSizeValue>, ToXContentFragment {
3739

38-
private static final DeprecationLogger deprecationLogger = new DeprecationLogger(LogManager.getLogger(ByteSizeValue.class));
40+
/**
41+
* We have to lazy initialize the deprecation logger as otherwise a static logger here would be constructed before logging is configured
42+
* leading to a runtime failure (see {@link LogConfigurator#checkErrorListener()} ). The premature construction would come from any
43+
* {@link Setting} object constructed in, for example, settings in {@link org.elasticsearch.common.network.NetworkService}.
44+
*/
45+
static class DeprecationLoggerHolder {
46+
static DeprecationLogger deprecationLogger = new DeprecationLogger(LogManager.getLogger(ByteSizeValue.class));
47+
}
3948

4049
public static final ByteSizeValue ZERO = new ByteSizeValue(0, ByteSizeUnit.BYTES);
4150

@@ -227,7 +236,7 @@ private static ByteSizeValue parse(final String initialInput, final String norma
227236
} catch (final NumberFormatException e) {
228237
try {
229238
final double doubleValue = Double.parseDouble(s);
230-
deprecationLogger.deprecated(
239+
DeprecationLoggerHolder.deprecationLogger.deprecated(
231240
"Fractional bytes values are deprecated. Use non-fractional bytes values instead: [{}] found for setting [{}]",
232241
initialInput, settingName);
233242
return new ByteSizeValue((long) (doubleValue * unit.toBytes(1)));

0 commit comments

Comments
 (0)