Core: Remove some logging constructors (#32513)

Remove a few of the logger constructors that aren't widely used or
aren't used at all and deprecate a few more logger constructors in favor
of log4j2's `LogManager`.
This commit is contained in:
Nik Everett 2018-08-09 16:11:48 -04:00 committed by GitHub
parent 5bbed5ed9a
commit 294ab7ee96
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 39 additions and 31 deletions

View File

@ -27,6 +27,7 @@ import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.core.appender.ConsoleAppender;
import org.apache.logging.log4j.core.appender.CountingNoOpAppender;
import org.apache.logging.log4j.core.config.Configurator;
import org.apache.logging.log4j.spi.ExtendedLogger;
import org.apache.logging.log4j.message.ParameterizedMessage;
import org.apache.lucene.util.Constants;
import org.elasticsearch.cli.UserException;
@ -299,8 +300,8 @@ public class EvilLoggerTests extends ESTestCase {
public void testPrefixLogger() throws IOException, IllegalAccessException, UserException {
setupLogging("prefix");
final String prefix = randomBoolean() ? null : randomAlphaOfLength(16);
final Logger logger = Loggers.getLogger("prefix", prefix);
final String prefix = randomAlphaOfLength(16);
final Logger logger = new PrefixLogger((ExtendedLogger) LogManager.getLogger("prefix_test"), "prefix_test", prefix);
logger.info("test");
logger.info("{}", "test");
final Exception e = new Exception("exception");
@ -320,13 +321,8 @@ public class EvilLoggerTests extends ESTestCase {
final int expectedLogLines = 3;
assertThat(events.size(), equalTo(expectedLogLines + stackTraceLength));
for (int i = 0; i < expectedLogLines; i++) {
if (prefix == null) {
assertThat("Contents of [" + path + "] are wrong",
events.get(i), startsWith("[" + getTestName() + "] test"));
} else {
assertThat("Contents of [" + path + "] are wrong",
events.get(i), startsWith("[" + getTestName() + "][" + prefix + "] test"));
}
events.get(i), startsWith("[" + getTestName() + "]" + prefix + " test"));
}
}
@ -335,8 +331,8 @@ public class EvilLoggerTests extends ESTestCase {
final int prefixes = 1 << 19; // to ensure enough markers that the GC should collect some when we force a GC below
for (int i = 0; i < prefixes; i++) {
Loggers.getLogger("prefix" + i, "prefix" + i); // this has the side effect of caching a marker with this prefix
// this has the side effect of caching a marker with this prefix
new PrefixLogger((ExtendedLogger) LogManager.getLogger("prefix" + i), "prefix" + i, "prefix" + i);
}
System.gc(); // this will free the weakly referenced keys in the marker cache

View File

@ -345,10 +345,7 @@ final class Bootstrap {
if (foreground && maybeConsoleAppender != null) {
Loggers.removeAppender(rootLogger, maybeConsoleAppender);
}
Logger logger = Loggers.getLogger(Bootstrap.class);
if (INSTANCE.node != null) {
logger = Loggers.getLogger(Bootstrap.class, Node.NODE_NAME_SETTING.get(INSTANCE.node.settings()));
}
Logger logger = LogManager.getLogger(Bootstrap.class);
// HACK, it sucks to do this, but we will run users out of disk space otherwise
if (e instanceof CreationException) {
// guice: log the shortened exc to the log file

View File

@ -62,14 +62,29 @@ public final class ESLoggerFactory {
return new PrefixLogger((ExtendedLogger)logger, logger.getName(), prefix);
}
/**
* Get or build a logger.
* @deprecated Prefer {@link LogManager#getLogger}
*/
@Deprecated
public static Logger getLogger(Class<?> clazz) {
return getLogger(null, clazz);
}
/**
* Get or build a logger.
* @deprecated Prefer {@link LogManager#getLogger}
*/
@Deprecated
public static Logger getLogger(String name) {
return getLogger(null, name);
}
/**
* Get the root logger.
* @deprecated Prefer {@link LogManager#getRootLogger}
*/
@Deprecated
public static Logger getRootLogger() {
return LogManager.getRootLogger();
}

View File

@ -67,11 +67,11 @@ public class Loggers {
}
public static Logger getLogger(Class<?> clazz, Settings settings, String... prefixes) {
return Loggers.getLogger(clazz, prefixes);
return ESLoggerFactory.getLogger(formatPrefix(prefixes), clazz);
}
public static Logger getLogger(String loggerName, Settings settings, String... prefixes) {
return Loggers.getLogger(loggerName, prefixes);
return ESLoggerFactory.getLogger(formatPrefix(prefixes), loggerName);
}
public static Logger getLogger(Logger parentLogger, String s) {
@ -82,22 +82,24 @@ public class Loggers {
return ESLoggerFactory.getLogger(prefix, parentLogger.getName() + s);
}
/**
* Get or build a logger.
* @deprecated Prefer {@link LogManager#getLogger}
*/
@Deprecated
public static Logger getLogger(String s) {
return ESLoggerFactory.getLogger(s);
}
/**
* Get or build a logger.
* @deprecated Prefer {@link LogManager#getLogger}
*/
@Deprecated
public static Logger getLogger(Class<?> clazz) {
return ESLoggerFactory.getLogger(clazz);
}
public static Logger getLogger(Class<?> clazz, String... prefixes) {
return ESLoggerFactory.getLogger(formatPrefix(prefixes), clazz);
}
public static Logger getLogger(String name, String... prefixes) {
return ESLoggerFactory.getLogger(formatPrefix(prefixes), name);
}
private static String formatPrefix(String... prefixes) {
String prefix = null;
if (prefixes != null && prefixes.length > 0) {

View File

@ -20,19 +20,18 @@
package org.elasticsearch.index.analysis;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.synonym.SolrSynonymParser;
import org.apache.lucene.util.CharsRef;
import org.apache.lucene.util.CharsRefBuilder;
import org.elasticsearch.common.logging.Loggers;
import java.io.IOException;
public class ESSolrSynonymParser extends SolrSynonymParser {
private static final Logger logger = LogManager.getLogger(ESSolrSynonymParser.class);
private final boolean lenient;
private static final Logger logger =
Loggers.getLogger(ESSolrSynonymParser.class, "ESSolrSynonymParser");
public ESSolrSynonymParser(boolean dedup, boolean expand, boolean lenient, Analyzer analyzer) {
super(dedup, expand, analyzer);

View File

@ -20,19 +20,18 @@
package org.elasticsearch.index.analysis;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.synonym.WordnetSynonymParser;
import org.apache.lucene.util.CharsRef;
import org.apache.lucene.util.CharsRefBuilder;
import org.elasticsearch.common.logging.Loggers;
import java.io.IOException;
public class ESWordnetSynonymParser extends WordnetSynonymParser {
private static final Logger logger = LogManager.getLogger(ESWordnetSynonymParser.class);
private final boolean lenient;
private static final Logger logger =
Loggers.getLogger(ESSolrSynonymParser.class, "ESWordnetSynonymParser");
public ESWordnetSynonymParser(boolean dedup, boolean expand, boolean lenient, Analyzer analyzer) {
super(dedup, expand, analyzer);