Skip to content

Commit 5c624bc

Browse files
authored
Logging: Further clean up logging ctors (#33378)
Drops and unused logging constructor, simplifies a rarely used one, and removes `Settings` from a third. There is now only a single logging ctor that takes `Settings` and we'll remove that one in a follow up change.
1 parent 46ac8d1 commit 5c624bc

File tree

11 files changed

+29
-42
lines changed

11 files changed

+29
-42
lines changed

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

+6-12
Original file line numberDiff line numberDiff line change
@@ -50,31 +50,25 @@ public class Loggers {
5050
Setting.Property.NodeScope));
5151

5252
public static Logger getLogger(Class<?> clazz, ShardId shardId, String... prefixes) {
53-
return getLogger(clazz, Settings.EMPTY,
54-
shardId.getIndex(), asArrayList(Integer.toString(shardId.id()), prefixes).toArray(new String[0]));
53+
return getLogger(clazz, shardId.getIndex(), asArrayList(Integer.toString(shardId.id()), prefixes).toArray(new String[0]));
5554
}
5655

5756
/**
5857
* Just like {@link #getLogger(Class, ShardId, String...)} but String loggerName instead of
59-
* Class.
58+
* Class and no extra prefixes.
6059
*/
61-
public static Logger getLogger(String loggerName, ShardId shardId, String... prefixes) {
62-
return getLogger(loggerName, Settings.EMPTY,
63-
asArrayList(shardId.getIndexName(), Integer.toString(shardId.id()), prefixes).toArray(new String[0]));
60+
public static Logger getLogger(String loggerName, ShardId shardId) {
61+
return ESLoggerFactory.getLogger(formatPrefix(shardId.getIndexName(), Integer.toString(shardId.id())), loggerName);
6462
}
6563

66-
public static Logger getLogger(Class<?> clazz, Settings settings, Index index, String... prefixes) {
67-
return getLogger(clazz, settings, asArrayList(Loggers.SPACE, index.getName(), prefixes).toArray(new String[0]));
64+
public static Logger getLogger(Class<?> clazz, Index index, String... prefixes) {
65+
return getLogger(clazz, Settings.EMPTY, asArrayList(Loggers.SPACE, index.getName(), prefixes).toArray(new String[0]));
6866
}
6967

7068
public static Logger getLogger(Class<?> clazz, Settings settings, String... prefixes) {
7169
return ESLoggerFactory.getLogger(formatPrefix(prefixes), clazz);
7270
}
7371

74-
public static Logger getLogger(String loggerName, Settings settings, String... prefixes) {
75-
return ESLoggerFactory.getLogger(formatPrefix(prefixes), loggerName);
76-
}
77-
7872
public static Logger getLogger(Logger parentLogger, String s) {
7973
String prefix = null;
8074
if (parentLogger instanceof PrefixLogger) {

server/src/main/java/org/elasticsearch/index/AbstractIndexComponent.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public abstract class AbstractIndexComponent implements IndexComponent {
3333
* Constructs a new index component, with the index name and its settings.
3434
*/
3535
protected AbstractIndexComponent(IndexSettings indexSettings) {
36-
this.logger = Loggers.getLogger(getClass(), indexSettings.getSettings(), indexSettings.getIndex());
36+
this.logger = Loggers.getLogger(getClass(), indexSettings.getIndex());
3737
this.deprecationLogger = new DeprecationLogger(logger);
3838
this.indexSettings = indexSettings;
3939
}

server/src/main/java/org/elasticsearch/index/CompositeIndexEventListener.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ final class CompositeIndexEventListener implements IndexEventListener {
5151
}
5252
}
5353
this.listeners = Collections.unmodifiableList(new ArrayList<>(listeners));
54-
this.logger = Loggers.getLogger(getClass(), indexSettings.getSettings(), indexSettings.getIndex());
54+
this.logger = Loggers.getLogger(getClass(), indexSettings.getIndex());
5555
}
5656

5757
@Override

server/src/main/java/org/elasticsearch/index/IndexSettings.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,7 @@ public IndexSettings(final IndexMetaData indexMetaData, final Settings nodeSetti
397397
this.settings = Settings.builder().put(nodeSettings).put(indexMetaData.getSettings()).build();
398398
this.index = indexMetaData.getIndex();
399399
version = IndexMetaData.SETTING_INDEX_VERSION_CREATED.get(settings);
400-
logger = Loggers.getLogger(getClass(), settings, index);
400+
logger = Loggers.getLogger(getClass(), index);
401401
nodeName = Node.NODE_NAME_SETTING.get(settings);
402402
this.indexMetaData = indexMetaData;
403403
numberOfShards = settings.getAsInt(IndexMetaData.SETTING_NUMBER_OF_SHARDS, null);

server/src/main/java/org/elasticsearch/index/IndexingSlowLog.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
package org.elasticsearch.index;
2121

2222
import org.apache.logging.log4j.Logger;
23+
import org.apache.logging.log4j.LogManager;
2324
import org.elasticsearch.common.Booleans;
2425
import org.elasticsearch.common.Strings;
2526
import org.elasticsearch.common.logging.Loggers;
@@ -89,7 +90,7 @@ public final class IndexingSlowLog implements IndexingOperationListener {
8990
}, Property.Dynamic, Property.IndexScope);
9091

9192
IndexingSlowLog(IndexSettings indexSettings) {
92-
this.indexLogger = Loggers.getLogger(INDEX_INDEXING_SLOWLOG_PREFIX + ".index", indexSettings.getSettings());
93+
this.indexLogger = LogManager.getLogger(INDEX_INDEXING_SLOWLOG_PREFIX + ".index");
9394
this.index = indexSettings.getIndex();
9495

9596
indexSettings.getScopedSettings().addSettingsUpdateConsumer(INDEX_INDEXING_SLOWLOG_REFORMAT_SETTING, this::setReformat);

server/src/main/java/org/elasticsearch/index/SearchSlowLog.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
package org.elasticsearch.index;
2121

2222
import org.apache.logging.log4j.Logger;
23+
import org.apache.logging.log4j.LogManager;
2324
import org.elasticsearch.common.Strings;
2425
import org.elasticsearch.common.logging.Loggers;
2526
import org.elasticsearch.common.settings.Setting;
@@ -82,8 +83,8 @@ public final class SearchSlowLog implements SearchOperationListener {
8283

8384
public SearchSlowLog(IndexSettings indexSettings) {
8485

85-
this.queryLogger = Loggers.getLogger(INDEX_SEARCH_SLOWLOG_PREFIX + ".query", indexSettings.getSettings());
86-
this.fetchLogger = Loggers.getLogger(INDEX_SEARCH_SLOWLOG_PREFIX + ".fetch", indexSettings.getSettings());
86+
this.queryLogger = LogManager.getLogger(INDEX_SEARCH_SLOWLOG_PREFIX + ".query");
87+
this.fetchLogger = LogManager.getLogger(INDEX_SEARCH_SLOWLOG_PREFIX + ".fetch");
8788

8889
indexSettings.getScopedSettings().addSettingsUpdateConsumer(INDEX_SEARCH_SLOWLOG_THRESHOLD_QUERY_WARN_SETTING, this::setQueryWarnThreshold);
8990
this.queryWarnThreshold = indexSettings.getValue(INDEX_SEARCH_SLOWLOG_THRESHOLD_QUERY_WARN_SETTING).nanos();

x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/Watcher.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ public Collection<Object> createComponents(Client client, ClusterService cluster
300300
actionFactoryMap.put(EmailAction.TYPE, new EmailActionFactory(settings, emailService, templateEngine, emailAttachmentsParser));
301301
actionFactoryMap.put(WebhookAction.TYPE, new WebhookActionFactory(settings, httpClient, templateEngine));
302302
actionFactoryMap.put(IndexAction.TYPE, new IndexActionFactory(settings, client));
303-
actionFactoryMap.put(LoggingAction.TYPE, new LoggingActionFactory(settings, templateEngine));
303+
actionFactoryMap.put(LoggingAction.TYPE, new LoggingActionFactory(templateEngine));
304304
actionFactoryMap.put(HipChatAction.TYPE, new HipChatActionFactory(settings, templateEngine, hipChatService));
305305
actionFactoryMap.put(JiraAction.TYPE, new JiraActionFactory(settings, templateEngine, jiraService));
306306
actionFactoryMap.put(SlackAction.TYPE, new SlackActionFactory(settings, templateEngine, slackService));

x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/actions/logging/ExecutableLoggingAction.java

+3-4
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@
66
package org.elasticsearch.xpack.watcher.actions.logging;
77

88
import org.apache.logging.log4j.Logger;
9-
import org.elasticsearch.common.logging.Loggers;
10-
import org.elasticsearch.common.settings.Settings;
9+
import org.apache.logging.log4j.LogManager;
1110
import org.elasticsearch.xpack.core.watcher.actions.Action;
1211
import org.elasticsearch.xpack.core.watcher.actions.ExecutableAction;
1312
import org.elasticsearch.xpack.core.watcher.execution.WatchExecutionContext;
@@ -22,9 +21,9 @@ public class ExecutableLoggingAction extends ExecutableAction<LoggingAction> {
2221
private final Logger textLogger;
2322
private final TextTemplateEngine templateEngine;
2423

25-
public ExecutableLoggingAction(LoggingAction action, Logger logger, Settings settings, TextTemplateEngine templateEngine) {
24+
public ExecutableLoggingAction(LoggingAction action, Logger logger, TextTemplateEngine templateEngine) {
2625
super(action, logger);
27-
this.textLogger = action.category != null ? Loggers.getLogger(action.category, settings) : logger;
26+
this.textLogger = action.category != null ? LogManager.getLogger(action.category) : logger;
2827
this.templateEngine = templateEngine;
2928
}
3029

x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/actions/logging/LoggingActionFactory.java

+4-7
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55
*/
66
package org.elasticsearch.xpack.watcher.actions.logging;
77

8-
import org.elasticsearch.common.logging.Loggers;
9-
import org.elasticsearch.common.settings.Settings;
8+
import org.apache.logging.log4j.LogManager;
109
import org.elasticsearch.common.xcontent.XContentParser;
1110
import org.elasticsearch.xpack.core.watcher.actions.ActionFactory;
1211
import org.elasticsearch.xpack.watcher.common.text.TextTemplateEngine;
@@ -15,18 +14,16 @@
1514

1615
public class LoggingActionFactory extends ActionFactory {
1716

18-
private final Settings settings;
1917
private final TextTemplateEngine templateEngine;
2018

21-
public LoggingActionFactory(Settings settings, TextTemplateEngine templateEngine) {
22-
super(Loggers.getLogger(ExecutableLoggingAction.class, settings));
23-
this.settings = settings;
19+
public LoggingActionFactory(TextTemplateEngine templateEngine) {
20+
super(LogManager.getLogger(ExecutableLoggingAction.class));
2421
this.templateEngine = templateEngine;
2522
}
2623

2724
@Override
2825
public ExecutableLoggingAction parseExecutable(String watchId, String actionId, XContentParser parser) throws IOException {
2926
LoggingAction action = LoggingAction.parse(watchId, actionId, parser);
30-
return new ExecutableLoggingAction(action, actionLogger, settings, templateEngine);
27+
return new ExecutableLoggingAction(action, actionLogger, templateEngine);
3128
}
3229
}

x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/actions/logging/LoggingActionTests.java

+5-10
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import org.apache.logging.log4j.Logger;
99
import org.elasticsearch.ElasticsearchParseException;
1010
import org.elasticsearch.common.SuppressLoggerChecks;
11-
import org.elasticsearch.common.settings.Settings;
1211
import org.elasticsearch.common.xcontent.XContentBuilder;
1312
import org.elasticsearch.common.xcontent.XContentParser;
1413
import org.elasticsearch.test.ESTestCase;
@@ -92,8 +91,7 @@ public void testExecute() throws Exception {
9291
}
9392

9493
public void testParser() throws Exception {
95-
Settings settings = Settings.EMPTY;
96-
LoggingActionFactory parser = new LoggingActionFactory(settings, engine);
94+
LoggingActionFactory parser = new LoggingActionFactory(engine);
9795

9896
String text = randomAlphaOfLength(10);
9997
TextTemplate template = new TextTemplate(text);
@@ -126,14 +124,13 @@ public void testParser() throws Exception {
126124
}
127125

128126
public void testParserSelfGenerated() throws Exception {
129-
Settings settings = Settings.EMPTY;
130-
LoggingActionFactory parser = new LoggingActionFactory(settings, engine);
127+
LoggingActionFactory parser = new LoggingActionFactory(engine);
131128

132129
String text = randomAlphaOfLength(10);
133130
TextTemplate template = new TextTemplate(text);
134131
String category = randomAlphaOfLength(10);
135132
LoggingAction action = new LoggingAction(template, level, category);
136-
ExecutableLoggingAction executable = new ExecutableLoggingAction(action, logger, settings, engine);
133+
ExecutableLoggingAction executable = new ExecutableLoggingAction(action, logger, engine);
137134
XContentBuilder builder = jsonBuilder();
138135
executable.toXContent(builder, Attachment.XContent.EMPTY_PARAMS);
139136

@@ -146,8 +143,7 @@ public void testParserSelfGenerated() throws Exception {
146143
}
147144

148145
public void testParserBuilder() throws Exception {
149-
Settings settings = Settings.EMPTY;
150-
LoggingActionFactory parser = new LoggingActionFactory(settings, engine);
146+
LoggingActionFactory parser = new LoggingActionFactory(engine);
151147

152148
String text = randomAlphaOfLength(10);
153149
TextTemplate template = new TextTemplate(text);
@@ -172,8 +168,7 @@ public void testParserBuilder() throws Exception {
172168
}
173169

174170
public void testParserFailure() throws Exception {
175-
Settings settings = Settings.EMPTY;
176-
LoggingActionFactory parser = new LoggingActionFactory(settings, engine);
171+
LoggingActionFactory parser = new LoggingActionFactory(engine);
177172

178173
XContentBuilder builder = jsonBuilder()
179174
.startObject().endObject();

x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/watch/WatchTests.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,7 @@ public void testParseWatchWithoutTriggerDoesNotWork() throws Exception {
438438
private WatchParser createWatchparser() throws Exception {
439439
LoggingAction loggingAction = new LoggingAction(new TextTemplate("foo"), null, null);
440440
List<ActionWrapper> actions = Collections.singletonList(new ActionWrapper("_logging_", randomThrottler(), null, null,
441-
new ExecutableLoggingAction(loggingAction, logger, settings, new MockTextTemplateEngine())));
441+
new ExecutableLoggingAction(loggingAction, logger, new MockTextTemplateEngine())));
442442

443443
ScheduleRegistry scheduleRegistry = registry(new IntervalSchedule(new IntervalSchedule.Interval(1,
444444
IntervalSchedule.Interval.Unit.SECONDS)));
@@ -622,7 +622,7 @@ private ActionRegistry registry(List<ActionWrapper> actions, ConditionRegistry c
622622
parsers.put(WebhookAction.TYPE, new WebhookActionFactory(settings, httpClient, templateEngine));
623623
break;
624624
case LoggingAction.TYPE:
625-
parsers.put(LoggingAction.TYPE, new LoggingActionFactory(settings, new MockTextTemplateEngine()));
625+
parsers.put(LoggingAction.TYPE, new LoggingActionFactory(new MockTextTemplateEngine()));
626626
break;
627627
}
628628
}

0 commit comments

Comments
 (0)