Enable console logging for CLI tools

This commit enables CLI tools to have console logging. For the CLI
tools, we skip configuring the logging infrastructure via the config
file, and instead set the level only via a system property.
This commit is contained in:
Jason Tedor 2016-08-31 09:05:26 -04:00
parent 21dbc5ba84
commit ac8c2e98ab
6 changed files with 27 additions and 27 deletions

View File

@ -227,7 +227,7 @@ final class Bootstrap {
INSTANCE = new Bootstrap(); INSTANCE = new Bootstrap();
Environment environment = initialEnvironment(foreground, pidFile, esSettings); Environment environment = initialEnvironment(foreground, pidFile, esSettings);
LogConfigurator.configure(environment); LogConfigurator.configure(environment, true);
checkForCustomConfFile(); checkForCustomConfFile();
if (environment.pidFile() != null) { if (environment.pidFile() != null) {

View File

@ -63,26 +63,28 @@ public class LogConfigurator {
public static void init() { public static void init() {
} }
public static void configure(final Environment environment) throws IOException { public static void configure(final Environment environment, final boolean resolveConfig) throws IOException {
final Settings settings = environment.settings(); final Settings settings = environment.settings();
setLogConfigurationSystemProperty(environment, settings); setLogConfigurationSystemProperty(environment, settings);
final LoggerContext context = (LoggerContext) LogManager.getContext(false); final LoggerContext context = (LoggerContext) LogManager.getContext(false);
final Set<FileVisitOption> options = EnumSet.of(FileVisitOption.FOLLOW_LINKS); if (resolveConfig) {
final List<AbstractConfiguration> configurations = new ArrayList<>(); final Set<FileVisitOption> options = EnumSet.of(FileVisitOption.FOLLOW_LINKS);
final PropertiesConfigurationFactory factory = new PropertiesConfigurationFactory(); final List<AbstractConfiguration> configurations = new ArrayList<>();
Files.walkFileTree(environment.configFile(), options, Integer.MAX_VALUE, new SimpleFileVisitor<Path>() { final PropertiesConfigurationFactory factory = new PropertiesConfigurationFactory();
@Override Files.walkFileTree(environment.configFile(), options, Integer.MAX_VALUE, new SimpleFileVisitor<Path>() {
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { @Override
if (file.getFileName().toString().equals("log4j2.properties")) { public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
configurations.add((PropertiesConfiguration) factory.getConfiguration(file.toString(), file.toUri())); if (file.getFileName().toString().equals("log4j2.properties")) {
configurations.add((PropertiesConfiguration) factory.getConfiguration(file.toString(), file.toUri()));
}
return FileVisitResult.CONTINUE;
} }
return FileVisitResult.CONTINUE; });
} context.start(new CompositeConfiguration(configurations));
}); }
context.start(new CompositeConfiguration(configurations));
if (ESLoggerFactory.LOG_DEFAULT_LEVEL_SETTING.exists(settings)) { if (ESLoggerFactory.LOG_DEFAULT_LEVEL_SETTING.exists(settings)) {
Loggers.setLevel(ESLoggerFactory.getRootLogger(), ESLoggerFactory.LOG_DEFAULT_LEVEL_SETTING.get(settings)); Loggers.setLevel(ESLoggerFactory.getRootLogger(), ESLoggerFactory.LOG_DEFAULT_LEVEL_SETTING.get(settings));
@ -95,7 +97,7 @@ public class LogConfigurator {
} }
} }
@SuppressForbidden(reason = "sets system property for logging configuraton") @SuppressForbidden(reason = "sets system property for logging configuration")
private static void setLogConfigurationSystemProperty(Environment environment, Settings settings) { private static void setLogConfigurationSystemProperty(Environment environment, Settings settings) {
System.setProperty("es.logs", environment.logsFile().resolve(ClusterName.CLUSTER_NAME_SETTING.get(settings).value()).toString()); System.setProperty("es.logs", environment.logsFile().resolve(ClusterName.CLUSTER_NAME_SETTING.get(settings).value()).toString());
} }

View File

@ -21,6 +21,7 @@ package org.elasticsearch.index.translog;
import org.elasticsearch.cli.MultiCommand; import org.elasticsearch.cli.MultiCommand;
import org.elasticsearch.cli.Terminal; import org.elasticsearch.cli.Terminal;
import org.elasticsearch.common.logging.LogConfigurator;
import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.env.Environment; import org.elasticsearch.env.Environment;
import org.elasticsearch.node.internal.InternalSettingsPreparer; import org.elasticsearch.node.internal.InternalSettingsPreparer;
@ -30,7 +31,7 @@ import org.elasticsearch.node.internal.InternalSettingsPreparer;
*/ */
public class TranslogToolCli extends MultiCommand { public class TranslogToolCli extends MultiCommand {
public TranslogToolCli() { private TranslogToolCli() {
super("A CLI tool for various Elasticsearch translog actions"); super("A CLI tool for various Elasticsearch translog actions");
subcommands.put("truncate", new TruncateTranslogCommand()); subcommands.put("truncate", new TruncateTranslogCommand());
} }
@ -43,11 +44,9 @@ public class TranslogToolCli extends MultiCommand {
// same terminal. // same terminal.
Environment loggingEnvironment = InternalSettingsPreparer.prepareEnvironment(Settings.builder() Environment loggingEnvironment = InternalSettingsPreparer.prepareEnvironment(Settings.builder()
.put("path.home", pathHome) .put("path.home", pathHome)
.put("appender.terminal.type", "terminal")
.put("rootLogger", "${logger.level}, terminal")
.put("logger.level", loggerLevel) .put("logger.level", loggerLevel)
.build(), Terminal.DEFAULT); .build(), Terminal.DEFAULT);
// LogConfigurator.configure(loggingEnvironment.settings(), false); LogConfigurator.configure(loggingEnvironment, false);
exit(new TranslogToolCli().main(args, Terminal.DEFAULT)); exit(new TranslogToolCli().main(args, Terminal.DEFAULT));
} }

View File

@ -21,6 +21,7 @@ package org.elasticsearch.plugins;
import org.elasticsearch.cli.MultiCommand; import org.elasticsearch.cli.MultiCommand;
import org.elasticsearch.cli.Terminal; import org.elasticsearch.cli.Terminal;
import org.elasticsearch.common.logging.LogConfigurator;
import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.env.Environment; import org.elasticsearch.env.Environment;
import org.elasticsearch.node.internal.InternalSettingsPreparer; import org.elasticsearch.node.internal.InternalSettingsPreparer;
@ -30,7 +31,7 @@ import org.elasticsearch.node.internal.InternalSettingsPreparer;
*/ */
public class PluginCli extends MultiCommand { public class PluginCli extends MultiCommand {
public PluginCli() { private PluginCli() {
super("A tool for managing installed elasticsearch plugins"); super("A tool for managing installed elasticsearch plugins");
subcommands.put("list", new ListPluginsCommand()); subcommands.put("list", new ListPluginsCommand());
subcommands.put("install", new InstallPluginCommand()); subcommands.put("install", new InstallPluginCommand());
@ -49,11 +50,9 @@ public class PluginCli extends MultiCommand {
// Therefore we print to Terminal. // Therefore we print to Terminal.
Environment loggingEnvironment = InternalSettingsPreparer.prepareEnvironment(Settings.builder() Environment loggingEnvironment = InternalSettingsPreparer.prepareEnvironment(Settings.builder()
.put("path.home", pathHome) .put("path.home", pathHome)
.put("appender.terminal.type", "terminal")
.put("rootLogger", "${logger.level}, terminal")
.put("logger.level", loggerLevel) .put("logger.level", loggerLevel)
.build(), Terminal.DEFAULT); .build(), Terminal.DEFAULT);
// LogConfigurator.configure(loggingEnvironment.settings(), false); LogConfigurator.configure(loggingEnvironment, false);
exit(new PluginCli().main(args, Terminal.DEFAULT)); exit(new PluginCli().main(args, Terminal.DEFAULT));
} }

View File

@ -48,7 +48,7 @@ public class EvilLoggerConfigurationTests extends ESTestCase {
.put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()) .put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString())
.build(); .build();
final Environment environment = new Environment(settings); final Environment environment = new Environment(settings);
LogConfigurator.configure(environment); LogConfigurator.configure(environment, true);
{ {
final LoggerContext ctx = (LoggerContext) LogManager.getContext(false); final LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
@ -87,7 +87,7 @@ public class EvilLoggerConfigurationTests extends ESTestCase {
.put("logger.level", level) .put("logger.level", level)
.build(); .build();
final Environment environment = new Environment(settings); final Environment environment = new Environment(settings);
LogConfigurator.configure(environment); LogConfigurator.configure(environment, true);
final String loggerName; final String loggerName;
if (LogManager.getContext(false).hasLogger("org.elasticsearch.test", new PrefixMessageFactory())) { if (LogManager.getContext(false).hasLogger("org.elasticsearch.test", new PrefixMessageFactory())) {
@ -109,7 +109,7 @@ public class EvilLoggerConfigurationTests extends ESTestCase {
.put("logger.test_resolve_order", "TRACE") .put("logger.test_resolve_order", "TRACE")
.build(); .build();
final Environment environment = new Environment(settings); final Environment environment = new Environment(settings);
LogConfigurator.configure(environment); LogConfigurator.configure(environment, true);
// args should overwrite whatever is in the config // args should overwrite whatever is in the config
final String loggerName; final String loggerName;

View File

@ -52,7 +52,7 @@ public class EvilLoggerTests extends ESTestCase {
.put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()) .put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString())
.build(); .build();
final Environment environment = new Environment(settings); final Environment environment = new Environment(settings);
LogConfigurator.configure(environment); LogConfigurator.configure(environment, true);
testLogger = ESLoggerFactory.getLogger("test"); testLogger = ESLoggerFactory.getLogger("test");
deprecationLogger = ESLoggerFactory.getDeprecationLogger("test"); deprecationLogger = ESLoggerFactory.getDeprecationLogger("test");