Skip to content

Commit e3763b4

Browse files
committed
Logger: Merge ESLoggerFactory into Loggers
`ESLoggerFactory` is now not particularly interesting and simple enought to fold entirely into `Loggers. So let's do that. Closes elastic#32174
1 parent ca620ff commit e3763b4

File tree

4 files changed

+15
-79
lines changed

4 files changed

+15
-79
lines changed

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
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;
3130
import org.apache.logging.log4j.message.ParameterizedMessage;
3231
import org.apache.lucene.util.Constants;
3332
import org.elasticsearch.cli.UserException;
@@ -301,7 +300,7 @@ public void testPrefixLogger() throws IOException, IllegalAccessException, UserE
301300
setupLogging("prefix");
302301

303302
final String prefix = randomAlphaOfLength(16);
304-
final Logger logger = new PrefixLogger((ExtendedLogger) LogManager.getLogger("prefix_test"), "prefix_test", prefix);
303+
final Logger logger = new PrefixLogger(LogManager.getLogger("prefix_test"), prefix);
305304
logger.info("test");
306305
logger.info("{}", "test");
307306
final Exception e = new Exception("exception");
@@ -332,7 +331,7 @@ public void testPrefixLoggerMarkersCanBeCollected() throws IOException, UserExce
332331
final int prefixes = 1 << 19; // to ensure enough markers that the GC should collect some when we force a GC below
333332
for (int i = 0; i < prefixes; i++) {
334333
// this has the side effect of caching a marker with this prefix
335-
new PrefixLogger((ExtendedLogger) LogManager.getLogger("prefix" + i), "prefix" + i, "prefix" + i);
334+
new PrefixLogger(LogManager.getLogger("logger" + i), "prefix" + i);
336335
}
337336

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

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

Lines changed: 0 additions & 64 deletions
This file was deleted.

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,23 +57,24 @@ public static Logger getLogger(Class<?> clazz, ShardId shardId, String... prefix
5757
* Class and no extra prefixes.
5858
*/
5959
public static Logger getLogger(String loggerName, ShardId shardId) {
60-
return ESLoggerFactory.getLogger(formatPrefix(shardId.getIndexName(), Integer.toString(shardId.id())), loggerName);
60+
String prefix = formatPrefix(shardId.getIndexName(), Integer.toString(shardId.id()));
61+
return new PrefixLogger(LogManager.getLogger(loggerName), prefix);
6162
}
6263

6364
public static Logger getLogger(Class<?> clazz, Index index, String... prefixes) {
6465
return getLogger(clazz, asArrayList(Loggers.SPACE, index.getName(), prefixes).toArray(new String[0]));
6566
}
6667

6768
public static Logger getLogger(Class<?> clazz, String... prefixes) {
68-
return ESLoggerFactory.getLogger(formatPrefix(prefixes), clazz);
69+
return new PrefixLogger(LogManager.getLogger(clazz), formatPrefix(prefixes));
6970
}
7071

7172
public static Logger getLogger(Logger parentLogger, String s) {
72-
String prefix = null;
73+
Logger inner = LogManager.getLogger(parentLogger.getName() + s);
7374
if (parentLogger instanceof PrefixLogger) {
74-
prefix = ((PrefixLogger)parentLogger).prefix();
75+
return new PrefixLogger(inner, ((PrefixLogger)parentLogger).prefix());
7576
}
76-
return ESLoggerFactory.getLogger(prefix, parentLogger.getName() + s);
77+
return inner;
7778
}
7879

7980
private static String formatPrefix(String... prefixes) {

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
package org.elasticsearch.common.logging;
2121

2222
import org.apache.logging.log4j.Level;
23+
import org.apache.logging.log4j.Logger;
2324
import org.apache.logging.log4j.Marker;
2425
import org.apache.logging.log4j.MarkerManager;
2526
import org.apache.logging.log4j.message.Message;
@@ -70,26 +71,25 @@ public String prefix() {
7071
* Construct a prefix logger with the specified name and prefix.
7172
*
7273
* @param logger the extended logger to wrap
73-
* @param name the name of this prefix logger
7474
* @param prefix the prefix for this prefix logger
7575
*/
76-
PrefixLogger(final ExtendedLogger logger, final String name, final String prefix) {
77-
super(logger, name, null);
76+
PrefixLogger(final Logger logger, final String prefix) {
77+
super((ExtendedLogger) logger, logger.getName(), null);
7878

79-
final String actualPrefix = (prefix == null ? "" : prefix);
79+
assert prefix != null && false == prefix.isEmpty() : "don't use a prefix logger without a prefix";
8080
final Marker actualMarker;
8181
// markers is not thread-safe, so we synchronize access
8282
synchronized (markers) {
83-
final Marker maybeMarker = markers.get(actualPrefix);
83+
final Marker maybeMarker = markers.get(prefix);
8484
if (maybeMarker == null) {
85-
actualMarker = new MarkerManager.Log4jMarker(actualPrefix);
85+
actualMarker = new MarkerManager.Log4jMarker(prefix);
8686
/*
8787
* We must create a new instance here as otherwise the marker will hold a reference to the key in the weak hash map; as
8888
* those references are held strongly, this would give a strong reference back to the key preventing them from ever being
8989
* collected. This also guarantees that no other strong reference can be held to the prefix anywhere.
9090
*/
9191
// noinspection RedundantStringConstructorCall
92-
markers.put(new String(actualPrefix), actualMarker);
92+
markers.put(new String(prefix), actualMarker);
9393
} else {
9494
actualMarker = maybeMarker;
9595
}

0 commit comments

Comments
 (0)