From ac8c2e98ab687b7ad1a4d0dea576801cc1924a62 Mon Sep 17 00:00:00 2001 From: Jason Tedor Date: Wed, 31 Aug 2016 09:05:26 -0400 Subject: [PATCH] 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. --- .../elasticsearch/bootstrap/Bootstrap.java | 2 +- .../common/logging/LogConfigurator.java | 30 ++++++++++--------- .../index/translog/TranslogToolCli.java | 7 ++--- .../org/elasticsearch/plugins/PluginCli.java | 7 ++--- .../logging/EvilLoggerConfigurationTests.java | 6 ++-- .../common/logging/EvilLoggerTests.java | 2 +- 6 files changed, 27 insertions(+), 27 deletions(-) diff --git a/core/src/main/java/org/elasticsearch/bootstrap/Bootstrap.java b/core/src/main/java/org/elasticsearch/bootstrap/Bootstrap.java index f342cd28794..2a38a020fee 100644 --- a/core/src/main/java/org/elasticsearch/bootstrap/Bootstrap.java +++ b/core/src/main/java/org/elasticsearch/bootstrap/Bootstrap.java @@ -227,7 +227,7 @@ final class Bootstrap { INSTANCE = new Bootstrap(); Environment environment = initialEnvironment(foreground, pidFile, esSettings); - LogConfigurator.configure(environment); + LogConfigurator.configure(environment, true); checkForCustomConfFile(); if (environment.pidFile() != null) { diff --git a/core/src/main/java/org/elasticsearch/common/logging/LogConfigurator.java b/core/src/main/java/org/elasticsearch/common/logging/LogConfigurator.java index dbd31637aeb..1b58b686100 100644 --- a/core/src/main/java/org/elasticsearch/common/logging/LogConfigurator.java +++ b/core/src/main/java/org/elasticsearch/common/logging/LogConfigurator.java @@ -63,26 +63,28 @@ public class LogConfigurator { 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(); setLogConfigurationSystemProperty(environment, settings); final LoggerContext context = (LoggerContext) LogManager.getContext(false); - final Set options = EnumSet.of(FileVisitOption.FOLLOW_LINKS); - final List configurations = new ArrayList<>(); - final PropertiesConfigurationFactory factory = new PropertiesConfigurationFactory(); - Files.walkFileTree(environment.configFile(), options, Integer.MAX_VALUE, new SimpleFileVisitor() { - @Override - public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { - if (file.getFileName().toString().equals("log4j2.properties")) { - configurations.add((PropertiesConfiguration) factory.getConfiguration(file.toString(), file.toUri())); + if (resolveConfig) { + final Set options = EnumSet.of(FileVisitOption.FOLLOW_LINKS); + final List configurations = new ArrayList<>(); + final PropertiesConfigurationFactory factory = new PropertiesConfigurationFactory(); + Files.walkFileTree(environment.configFile(), options, Integer.MAX_VALUE, new SimpleFileVisitor() { + @Override + public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { + 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)) { 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) { System.setProperty("es.logs", environment.logsFile().resolve(ClusterName.CLUSTER_NAME_SETTING.get(settings).value()).toString()); } diff --git a/core/src/main/java/org/elasticsearch/index/translog/TranslogToolCli.java b/core/src/main/java/org/elasticsearch/index/translog/TranslogToolCli.java index a25e5d4cc74..124affefe1d 100644 --- a/core/src/main/java/org/elasticsearch/index/translog/TranslogToolCli.java +++ b/core/src/main/java/org/elasticsearch/index/translog/TranslogToolCli.java @@ -21,6 +21,7 @@ package org.elasticsearch.index.translog; import org.elasticsearch.cli.MultiCommand; import org.elasticsearch.cli.Terminal; +import org.elasticsearch.common.logging.LogConfigurator; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.env.Environment; import org.elasticsearch.node.internal.InternalSettingsPreparer; @@ -30,7 +31,7 @@ import org.elasticsearch.node.internal.InternalSettingsPreparer; */ public class TranslogToolCli extends MultiCommand { - public TranslogToolCli() { + private TranslogToolCli() { super("A CLI tool for various Elasticsearch translog actions"); subcommands.put("truncate", new TruncateTranslogCommand()); } @@ -43,11 +44,9 @@ public class TranslogToolCli extends MultiCommand { // same terminal. Environment loggingEnvironment = InternalSettingsPreparer.prepareEnvironment(Settings.builder() .put("path.home", pathHome) - .put("appender.terminal.type", "terminal") - .put("rootLogger", "${logger.level}, terminal") .put("logger.level", loggerLevel) .build(), Terminal.DEFAULT); - // LogConfigurator.configure(loggingEnvironment.settings(), false); + LogConfigurator.configure(loggingEnvironment, false); exit(new TranslogToolCli().main(args, Terminal.DEFAULT)); } diff --git a/core/src/main/java/org/elasticsearch/plugins/PluginCli.java b/core/src/main/java/org/elasticsearch/plugins/PluginCli.java index a49aab30036..0411c7b6e1c 100644 --- a/core/src/main/java/org/elasticsearch/plugins/PluginCli.java +++ b/core/src/main/java/org/elasticsearch/plugins/PluginCli.java @@ -21,6 +21,7 @@ package org.elasticsearch.plugins; import org.elasticsearch.cli.MultiCommand; import org.elasticsearch.cli.Terminal; +import org.elasticsearch.common.logging.LogConfigurator; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.env.Environment; import org.elasticsearch.node.internal.InternalSettingsPreparer; @@ -30,7 +31,7 @@ import org.elasticsearch.node.internal.InternalSettingsPreparer; */ public class PluginCli extends MultiCommand { - public PluginCli() { + private PluginCli() { super("A tool for managing installed elasticsearch plugins"); subcommands.put("list", new ListPluginsCommand()); subcommands.put("install", new InstallPluginCommand()); @@ -49,11 +50,9 @@ public class PluginCli extends MultiCommand { // Therefore we print to Terminal. Environment loggingEnvironment = InternalSettingsPreparer.prepareEnvironment(Settings.builder() .put("path.home", pathHome) - .put("appender.terminal.type", "terminal") - .put("rootLogger", "${logger.level}, terminal") .put("logger.level", loggerLevel) .build(), Terminal.DEFAULT); - // LogConfigurator.configure(loggingEnvironment.settings(), false); + LogConfigurator.configure(loggingEnvironment, false); exit(new PluginCli().main(args, Terminal.DEFAULT)); } diff --git a/qa/evil-tests/src/test/java/org/elasticsearch/common/logging/EvilLoggerConfigurationTests.java b/qa/evil-tests/src/test/java/org/elasticsearch/common/logging/EvilLoggerConfigurationTests.java index 4f32d83e7bd..4081eab1f2c 100644 --- a/qa/evil-tests/src/test/java/org/elasticsearch/common/logging/EvilLoggerConfigurationTests.java +++ b/qa/evil-tests/src/test/java/org/elasticsearch/common/logging/EvilLoggerConfigurationTests.java @@ -48,7 +48,7 @@ public class EvilLoggerConfigurationTests extends ESTestCase { .put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()) .build(); final Environment environment = new Environment(settings); - LogConfigurator.configure(environment); + LogConfigurator.configure(environment, true); { final LoggerContext ctx = (LoggerContext) LogManager.getContext(false); @@ -87,7 +87,7 @@ public class EvilLoggerConfigurationTests extends ESTestCase { .put("logger.level", level) .build(); final Environment environment = new Environment(settings); - LogConfigurator.configure(environment); + LogConfigurator.configure(environment, true); final String loggerName; 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") .build(); final Environment environment = new Environment(settings); - LogConfigurator.configure(environment); + LogConfigurator.configure(environment, true); // args should overwrite whatever is in the config final String loggerName; diff --git a/qa/evil-tests/src/test/java/org/elasticsearch/common/logging/EvilLoggerTests.java b/qa/evil-tests/src/test/java/org/elasticsearch/common/logging/EvilLoggerTests.java index 53a74656443..a14ee2282c4 100644 --- a/qa/evil-tests/src/test/java/org/elasticsearch/common/logging/EvilLoggerTests.java +++ b/qa/evil-tests/src/test/java/org/elasticsearch/common/logging/EvilLoggerTests.java @@ -52,7 +52,7 @@ public class EvilLoggerTests extends ESTestCase { .put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()) .build(); final Environment environment = new Environment(settings); - LogConfigurator.configure(environment); + LogConfigurator.configure(environment, true); testLogger = ESLoggerFactory.getLogger("test"); deprecationLogger = ESLoggerFactory.getDeprecationLogger("test");