Skip to content

Commit e23f59a

Browse files
author
Andrey Ershov
committed
Move logging initialization to NodeToolCli
1 parent f3d78c4 commit e23f59a

3 files changed

Lines changed: 15 additions & 8 deletions

File tree

server/src/main/java/org/elasticsearch/cli/CommandLoggingConfigurator.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,13 @@
2727
* Holder class for method to configure logging without Elasticsearch configuration files for use in CLI tools that will not read such
2828
* files.
2929
*/
30-
final class CommandLoggingConfigurator {
30+
public final class CommandLoggingConfigurator {
3131

3232
/**
3333
* Configures logging without Elasticsearch configuration files based on the system property "es.logger.level" only. As such, any
3434
* logging will be written to the console.
3535
*/
36-
static void configureLoggingWithoutConfig() {
36+
public static void configureLoggingWithoutConfig() {
3737
// initialize default for es.logger.level because we will not read the log4j2.properties
3838
final String loggerLevel = System.getProperty("es.logger.level", Level.INFO.name());
3939
final Settings settings = Settings.builder().put("logger.level", loggerLevel).build();

server/src/main/java/org/elasticsearch/cluster/coordination/NodeToolCli.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,22 @@
1818
*/
1919
package org.elasticsearch.cluster.coordination;
2020

21-
import org.elasticsearch.cli.LoggingAwareMultiCommand;
21+
import org.elasticsearch.cli.CommandLoggingConfigurator;
22+
import org.elasticsearch.cli.MultiCommand;
2223
import org.elasticsearch.cli.Terminal;
2324

24-
public class NodeToolCli extends LoggingAwareMultiCommand {
25+
// NodeToolCli does not extend LoggingAwareCommand, because LoggingAwareCommand performs logging initialization
26+
// after LoggingAwareCommand instance is constructed.
27+
// It's too late for us, because before UnsafeBootstrapMasterCommand is added to the list of subcommands
28+
// log4j2 initialization will happen, because it has static reference to Logger class.
29+
// Even if we avoid making a static reference to Logger class, there is no nice way to avoid declaring
30+
// UNSAFE_BOOTSTRAP, which depends on ClusterService, which in turn has static Logger.
31+
// TODO execute CommandLoggingConfigurator.configureLoggingWithoutConfig() in the constructor of commands, not in beforeMain
32+
public class NodeToolCli extends MultiCommand {
2533

2634
public NodeToolCli() {
27-
super("A CLI tool to unsafely recover a cluster after the permanent loss of too many master-eligible nodes");
35+
super("A CLI tool to unsafely recover a cluster after the permanent loss of too many master-eligible nodes", ()->{});
36+
CommandLoggingConfigurator.configureLoggingWithoutConfig();
2837
subcommands.put("unsafe-bootstrap", new UnsafeBootstrapMasterCommand());
2938
}
3039

server/src/main/java/org/elasticsearch/cluster/coordination/UnsafeBootstrapMasterCommand.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646

4747
public class UnsafeBootstrapMasterCommand extends EnvironmentAwareCommand {
4848

49+
private static final Logger logger = LogManager.getLogger(UnsafeBootstrapMasterCommand.class);
4950
private final NamedXContentRegistry namedXContentRegistry;
5051

5152
static final String WARNING_MSG =
@@ -81,9 +82,6 @@ public class UnsafeBootstrapMasterCommand extends EnvironmentAwareCommand {
8182

8283
@Override
8384
protected void execute(Terminal terminal, OptionSet options, Environment env) throws Exception {
84-
// We don't use classical "private static final Logger", because log4j2 initialization happens
85-
// after UnsafeBootstrapMasterCommand instance creation.
86-
final Logger logger = LogManager.getLogger(UnsafeBootstrapMasterCommand.class);
8785
showWarning(terminal);
8886
String text = terminal.readText("Confirm [y/N] ");
8987
if (text.equalsIgnoreCase("y") == false) {

0 commit comments

Comments
 (0)