Skip to content

Commit e01c90b

Browse files
committed
Allow not configure logging without config
For CLI tools, we configure logging without reading the log4j2.properties file. This because any log statements in a CLI tool should dump to the console while reading from the log4j2.properties file would cause them to dump whereever the log configuration there indicates (e.g., possibly a remote machine). To do this, we added some code to the base implementation of all CLI tools to configure logging without a config file. This code is also executed when Elasticsearch starts up. In the past this was fine yet we previously added detection to Elasticsearch to find cases where we use logging before it is configured. Because of configuring logging without a config, this means we only catch uses of logging before the logging without config is performed. To correct this, we enable a CLI tool to skip enabling logging without a config and then in the Elasticsearch CLI we indeed utilize this to skip configuring logging without a config. Relates #26209
1 parent 5dcf18f commit e01c90b

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

core/src/main/java/org/elasticsearch/bootstrap/Elasticsearch.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,15 @@ static int main(final String[] args, final Elasticsearch elasticsearch, final Te
9292
return elasticsearch.main(args, terminal);
9393
}
9494

95+
@Override
96+
protected boolean shouldConfigureLoggingWithoutConfig() {
97+
/*
98+
* If we allow logging to be configured without a config before we ready to read the log4j2.properties file, then we will fail to
99+
* detect uses of logging before it is properly configured.
100+
*/
101+
return false;
102+
}
103+
95104
@Override
96105
protected void execute(Terminal terminal, OptionSet options, Environment env) throws UserException {
97106
if (options.nonOptionArguments().isEmpty() == false) {

core/src/main/java/org/elasticsearch/cli/Command.java

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,12 @@ public final int main(String[] args, Terminal terminal) throws Exception {
7979
Runtime.getRuntime().addShutdownHook(shutdownHookThread.get());
8080
}
8181

82-
// initialize default for es.logger.level because we will not read the log4j2.properties
83-
final String loggerLevel = System.getProperty("es.logger.level", Level.INFO.name());
84-
final Settings settings = Settings.builder().put("logger.level", loggerLevel).build();
85-
LogConfigurator.configureWithoutConfig(settings);
82+
if (shouldConfigureLoggingWithoutConfig()) {
83+
// initialize default for es.logger.level because we will not read the log4j2.properties
84+
final String loggerLevel = System.getProperty("es.logger.level", Level.INFO.name());
85+
final Settings settings = Settings.builder().put("logger.level", loggerLevel).build();
86+
LogConfigurator.configureWithoutConfig(settings);
87+
}
8688

8789
try {
8890
mainWithoutErrorHandling(args, terminal);
@@ -100,6 +102,16 @@ public final int main(String[] args, Terminal terminal) throws Exception {
100102
return ExitCodes.OK;
101103
}
102104

105+
/**
106+
* Indicate whether or not logging should be configured without reading a log4j2.properties. Most commands should do this because we do
107+
* not configure logging for CLI tools. Only commands that configure logging on their own should not do this.
108+
*
109+
* @return true if logging should be configured without reading a log4j2.properties file
110+
*/
111+
protected boolean shouldConfigureLoggingWithoutConfig() {
112+
return true;
113+
}
114+
103115
/**
104116
* Executes the command, but all errors are thrown.
105117
*/

0 commit comments

Comments
 (0)