Skip to content

Commit 348c28d

Browse files
authored
Logger: Merge ESLoggerFactory into Loggers (#35146)
`ESLoggerFactory` is now not particularly interesting and simple enought to fold entirely into `Loggers. So let's do that. Closes #32174
1 parent 3ee37b4 commit 348c28d

File tree

6 files changed

+53
-80
lines changed

6 files changed

+53
-80
lines changed

client/rest-high-level/src/main/resources/forbidden/rest-high-level-signatures.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ org.apache.http.entity.ContentType#create(java.lang.String,org.apache.http.NameV
2222

2323
@defaultMessage ES's logging infrastructure uses log4j2 which we don't want to force on high level rest client users
2424
org.elasticsearch.common.logging.DeprecationLogger
25-
org.elasticsearch.common.logging.ESLoggerFactory
2625
org.elasticsearch.common.logging.LogConfigurator
2726
org.elasticsearch.common.logging.LoggerMessageFormat
2827
org.elasticsearch.common.logging.Loggers

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: 9 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,27 @@ 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+
if (prefix == null || prefix.isEmpty()) {
80+
throw new IllegalArgumentException("if you don't need a prefix then use a regular logger");
81+
}
8082
final Marker actualMarker;
8183
// markers is not thread-safe, so we synchronize access
8284
synchronized (markers) {
83-
final Marker maybeMarker = markers.get(actualPrefix);
85+
final Marker maybeMarker = markers.get(prefix);
8486
if (maybeMarker == null) {
85-
actualMarker = new MarkerManager.Log4jMarker(actualPrefix);
87+
actualMarker = new MarkerManager.Log4jMarker(prefix);
8688
/*
8789
* We must create a new instance here as otherwise the marker will hold a reference to the key in the weak hash map; as
8890
* those references are held strongly, this would give a strong reference back to the key preventing them from ever being
8991
* collected. This also guarantees that no other strong reference can be held to the prefix anywhere.
9092
*/
9193
// noinspection RedundantStringConstructorCall
92-
markers.put(new String(actualPrefix), actualMarker);
94+
markers.put(new String(prefix), actualMarker);
9395
} else {
9496
actualMarker = maybeMarker;
9597
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Licensed to Elasticsearch under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.elasticsearch.common.logging;
21+
22+
import org.elasticsearch.test.ESTestCase;
23+
24+
import static org.hamcrest.Matchers.containsString;
25+
26+
public class PrefixLoggerTests extends ESTestCase {
27+
public void testNullPrefix() {
28+
Exception e = expectThrows(IllegalArgumentException.class, () -> new PrefixLogger(logger, null));
29+
assertThat(e.getMessage(), containsString("use a regular logger"));
30+
}
31+
32+
public void testEmptyPrefix() {
33+
Exception e = expectThrows(IllegalArgumentException.class, () -> new PrefixLogger(logger, ""));
34+
assertThat(e.getMessage(), containsString("use a regular logger"));
35+
}
36+
}

0 commit comments

Comments
 (0)