Skip to content

Commit 294ab7e

Browse files
authored
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`.
1 parent 5bbed5e commit 294ab7e

File tree

6 files changed

+39
-31
lines changed

6 files changed

+39
-31
lines changed

qa/evil-tests/src/test/java/org/elasticsearch/common/logging/EvilLoggerTests.java

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.apache.logging.log4j.core.appender.ConsoleAppender;
2828
import org.apache.logging.log4j.core.appender.CountingNoOpAppender;
2929
import org.apache.logging.log4j.core.config.Configurator;
30+
import org.apache.logging.log4j.spi.ExtendedLogger;
3031
import org.apache.logging.log4j.message.ParameterizedMessage;
3132
import org.apache.lucene.util.Constants;
3233
import org.elasticsearch.cli.UserException;
@@ -299,8 +300,8 @@ public void testFindAppender() throws IOException, UserException {
299300
public void testPrefixLogger() throws IOException, IllegalAccessException, UserException {
300301
setupLogging("prefix");
301302

302-
final String prefix = randomBoolean() ? null : randomAlphaOfLength(16);
303-
final Logger logger = Loggers.getLogger("prefix", prefix);
303+
final String prefix = randomAlphaOfLength(16);
304+
final Logger logger = new PrefixLogger((ExtendedLogger) LogManager.getLogger("prefix_test"), "prefix_test", prefix);
304305
logger.info("test");
305306
logger.info("{}", "test");
306307
final Exception e = new Exception("exception");
@@ -320,13 +321,8 @@ public void testPrefixLogger() throws IOException, IllegalAccessException, UserE
320321
final int expectedLogLines = 3;
321322
assertThat(events.size(), equalTo(expectedLogLines + stackTraceLength));
322323
for (int i = 0; i < expectedLogLines; i++) {
323-
if (prefix == null) {
324-
assertThat("Contents of [" + path + "] are wrong",
325-
events.get(i), startsWith("[" + getTestName() + "] test"));
326-
} else {
327-
assertThat("Contents of [" + path + "] are wrong",
328-
events.get(i), startsWith("[" + getTestName() + "][" + prefix + "] test"));
329-
}
324+
assertThat("Contents of [" + path + "] are wrong",
325+
events.get(i), startsWith("[" + getTestName() + "]" + prefix + " test"));
330326
}
331327
}
332328

@@ -335,8 +331,8 @@ public void testPrefixLoggerMarkersCanBeCollected() throws IOException, UserExce
335331

336332
final int prefixes = 1 << 19; // to ensure enough markers that the GC should collect some when we force a GC below
337333
for (int i = 0; i < prefixes; i++) {
338-
Loggers.getLogger("prefix" + i, "prefix" + i); // this has the side effect of caching a marker with this prefix
339-
334+
// this has the side effect of caching a marker with this prefix
335+
new PrefixLogger((ExtendedLogger) LogManager.getLogger("prefix" + i), "prefix" + i, "prefix" + i);
340336
}
341337

342338
System.gc(); // this will free the weakly referenced keys in the marker cache

server/src/main/java/org/elasticsearch/bootstrap/Bootstrap.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -345,10 +345,7 @@ static void init(
345345
if (foreground && maybeConsoleAppender != null) {
346346
Loggers.removeAppender(rootLogger, maybeConsoleAppender);
347347
}
348-
Logger logger = Loggers.getLogger(Bootstrap.class);
349-
if (INSTANCE.node != null) {
350-
logger = Loggers.getLogger(Bootstrap.class, Node.NODE_NAME_SETTING.get(INSTANCE.node.settings()));
351-
}
348+
Logger logger = LogManager.getLogger(Bootstrap.class);
352349
// HACK, it sucks to do this, but we will run users out of disk space otherwise
353350
if (e instanceof CreationException) {
354351
// guice: log the shortened exc to the log file

server/src/main/java/org/elasticsearch/common/logging/ESLoggerFactory.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,29 @@ public static Logger getLogger(String prefix, Logger logger) {
6262
return new PrefixLogger((ExtendedLogger)logger, logger.getName(), prefix);
6363
}
6464

65+
/**
66+
* Get or build a logger.
67+
* @deprecated Prefer {@link LogManager#getLogger}
68+
*/
69+
@Deprecated
6570
public static Logger getLogger(Class<?> clazz) {
6671
return getLogger(null, clazz);
6772
}
6873

74+
/**
75+
* Get or build a logger.
76+
* @deprecated Prefer {@link LogManager#getLogger}
77+
*/
78+
@Deprecated
6979
public static Logger getLogger(String name) {
7080
return getLogger(null, name);
7181
}
7282

83+
/**
84+
* Get the root logger.
85+
* @deprecated Prefer {@link LogManager#getRootLogger}
86+
*/
87+
@Deprecated
7388
public static Logger getRootLogger() {
7489
return LogManager.getRootLogger();
7590
}

server/src/main/java/org/elasticsearch/common/logging/Loggers.java

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,11 @@ public static Logger getLogger(Class<?> clazz, Settings settings, Index index, S
6767
}
6868

6969
public static Logger getLogger(Class<?> clazz, Settings settings, String... prefixes) {
70-
return Loggers.getLogger(clazz, prefixes);
70+
return ESLoggerFactory.getLogger(formatPrefix(prefixes), clazz);
7171
}
7272

7373
public static Logger getLogger(String loggerName, Settings settings, String... prefixes) {
74-
return Loggers.getLogger(loggerName, prefixes);
74+
return ESLoggerFactory.getLogger(formatPrefix(prefixes), loggerName);
7575
}
7676

7777
public static Logger getLogger(Logger parentLogger, String s) {
@@ -82,22 +82,24 @@ public static Logger getLogger(Logger parentLogger, String s) {
8282
return ESLoggerFactory.getLogger(prefix, parentLogger.getName() + s);
8383
}
8484

85+
/**
86+
* Get or build a logger.
87+
* @deprecated Prefer {@link LogManager#getLogger}
88+
*/
89+
@Deprecated
8590
public static Logger getLogger(String s) {
8691
return ESLoggerFactory.getLogger(s);
8792
}
8893

94+
/**
95+
* Get or build a logger.
96+
* @deprecated Prefer {@link LogManager#getLogger}
97+
*/
98+
@Deprecated
8999
public static Logger getLogger(Class<?> clazz) {
90100
return ESLoggerFactory.getLogger(clazz);
91101
}
92102

93-
public static Logger getLogger(Class<?> clazz, String... prefixes) {
94-
return ESLoggerFactory.getLogger(formatPrefix(prefixes), clazz);
95-
}
96-
97-
public static Logger getLogger(String name, String... prefixes) {
98-
return ESLoggerFactory.getLogger(formatPrefix(prefixes), name);
99-
}
100-
101103
private static String formatPrefix(String... prefixes) {
102104
String prefix = null;
103105
if (prefixes != null && prefixes.length > 0) {

server/src/main/java/org/elasticsearch/index/analysis/ESSolrSynonymParser.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,18 @@
2020
package org.elasticsearch.index.analysis;
2121

2222
import org.apache.logging.log4j.Logger;
23+
import org.apache.logging.log4j.LogManager;
2324
import org.apache.lucene.analysis.Analyzer;
2425
import org.apache.lucene.analysis.synonym.SolrSynonymParser;
2526
import org.apache.lucene.util.CharsRef;
2627
import org.apache.lucene.util.CharsRefBuilder;
27-
import org.elasticsearch.common.logging.Loggers;
2828

2929
import java.io.IOException;
3030

3131
public class ESSolrSynonymParser extends SolrSynonymParser {
32+
private static final Logger logger = LogManager.getLogger(ESSolrSynonymParser.class);
3233

3334
private final boolean lenient;
34-
private static final Logger logger =
35-
Loggers.getLogger(ESSolrSynonymParser.class, "ESSolrSynonymParser");
3635

3736
public ESSolrSynonymParser(boolean dedup, boolean expand, boolean lenient, Analyzer analyzer) {
3837
super(dedup, expand, analyzer);

server/src/main/java/org/elasticsearch/index/analysis/ESWordnetSynonymParser.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,18 @@
2020
package org.elasticsearch.index.analysis;
2121

2222
import org.apache.logging.log4j.Logger;
23+
import org.apache.logging.log4j.LogManager;
2324
import org.apache.lucene.analysis.Analyzer;
2425
import org.apache.lucene.analysis.synonym.WordnetSynonymParser;
2526
import org.apache.lucene.util.CharsRef;
2627
import org.apache.lucene.util.CharsRefBuilder;
27-
import org.elasticsearch.common.logging.Loggers;
2828

2929
import java.io.IOException;
3030

3131
public class ESWordnetSynonymParser extends WordnetSynonymParser {
32+
private static final Logger logger = LogManager.getLogger(ESWordnetSynonymParser.class);
3233

3334
private final boolean lenient;
34-
private static final Logger logger =
35-
Loggers.getLogger(ESSolrSynonymParser.class, "ESWordnetSynonymParser");
3635

3736
public ESWordnetSynonymParser(boolean dedup, boolean expand, boolean lenient, Analyzer analyzer) {
3837
super(dedup, expand, analyzer);

0 commit comments

Comments
 (0)