Skip to content

Commit

Permalink
Refactor ParameterizedMessage used in lambda and casted to Supplier (#…
Browse files Browse the repository at this point in the history
…87156)

This is a result of structural search/replace in intellij. This only affects log methods with a signature
logger.info((Supplier) ()-> ParametrizedMessage) logger.info((Supplier) ()-> ParametrizedMessage, Throwable)

relates #86549
  • Loading branch information
pgomulka committed May 31, 2022
1 parent aa25c1d commit 705b27a
Show file tree
Hide file tree
Showing 32 changed files with 109 additions and 354 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.message.ParameterizedMessage;
import org.apache.logging.log4j.util.Supplier;
import org.elasticsearch.ResourceNotFoundException;
import org.elasticsearch.action.search.SearchRequest;
Expand Down Expand Up @@ -61,6 +60,8 @@
import java.util.stream.Stream;
import java.util.zip.GZIPInputStream;

import static org.elasticsearch.core.Strings.format;

/**
* A component that is responsible for making the databases maintained by {@link GeoIpDownloader}
* available to ingest processors on each ingest node.
Expand Down Expand Up @@ -344,11 +345,7 @@ void updateDatabase(String databaseFileName, String recordedMd5, Path file) {
);
} catch (Exception e) {
LOGGER.debug(
(Supplier<?>) () -> new ParameterizedMessage(
"failed to reload pipeline [{}] after downloading of database [{}]",
id,
databaseFileName
),
() -> format("failed to reload pipeline [%s] after downloading of database [%s]", id, databaseFileName),
e
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.message.ParameterizedMessage;
import org.apache.logging.log4j.util.Supplier;
import org.elasticsearch.SpecialPermission;
import org.elasticsearch.script.GeneralScriptException;
import org.elasticsearch.script.Script;
Expand All @@ -32,6 +30,8 @@
import java.util.Map;
import java.util.Set;

import static org.elasticsearch.core.Strings.format;

/**
* Main entry point handling template registration, compilation and
* execution.
Expand Down Expand Up @@ -115,7 +115,7 @@ public String execute() {
return null;
});
} catch (Exception e) {
logger.error((Supplier<?>) () -> new ParameterizedMessage("Error running {}", template), e);
logger.error(() -> format("Error running %s", template), e);
throw new GeneralScriptException("Error running " + template, e);
}
return writer.toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import org.apache.http.entity.ContentType;
import org.apache.http.util.EntityUtils;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.message.ParameterizedMessage;
import org.apache.logging.log4j.util.Supplier;
import org.elasticsearch.ElasticsearchException;
import org.elasticsearch.ElasticsearchStatusException;
Expand Down Expand Up @@ -44,6 +43,7 @@
import java.util.function.BiFunction;
import java.util.function.Consumer;

import static org.elasticsearch.core.Strings.format;
import static org.elasticsearch.core.TimeValue.timeValueMillis;
import static org.elasticsearch.core.TimeValue.timeValueNanos;
import static org.elasticsearch.reindex.remote.RemoteResponseParsers.MAIN_ACTION_PARSER;
Expand Down Expand Up @@ -122,8 +122,8 @@ private void logFailure(Exception e) {
if (e instanceof ResponseException re) {
if (remoteVersion.before(Version.fromId(2000099)) && re.getResponse().getStatusLine().getStatusCode() == 404) {
logger.debug(
(Supplier<?>) () -> new ParameterizedMessage(
"Failed to clear scroll [{}] from pre-2.0 Elasticsearch. This is normal if the request terminated "
() -> format(
"Failed to clear scroll [%s] from pre-2.0 Elasticsearch. This is normal if the request terminated "
+ "normally as the scroll has already been cleared automatically.",
scrollId
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.message.ParameterizedMessage;
import org.apache.logging.log4j.util.Supplier;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.common.util.SingleObjectCache;
Expand All @@ -35,6 +33,7 @@
import java.util.Set;

import static java.util.Collections.disjoint;
import static org.elasticsearch.core.Strings.format;
import static org.elasticsearch.discovery.ec2.AwsEc2Service.HostType.PRIVATE_DNS;
import static org.elasticsearch.discovery.ec2.AwsEc2Service.HostType.PRIVATE_IP;
import static org.elasticsearch.discovery.ec2.AwsEc2Service.HostType.PUBLIC_DNS;
Expand Down Expand Up @@ -183,14 +182,7 @@ protected List<TransportAddress> fetchDynamicNodes() {
}
} catch (final Exception e) {
final String finalAddress = address;
logger.warn(
(Supplier<?>) () -> new ParameterizedMessage(
"failed to add {}, address {}",
instance.getInstanceId(),
finalAddress
),
e
);
logger.warn(() -> format("failed to add %s, address %s", instance.getInstanceId(), finalAddress), e);
}
} else {
logger.trace("not adding {}, address is null, host_type {}", instance.getInstanceId(), hostType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.message.ParameterizedMessage;
import org.apache.logging.log4j.util.Supplier;
import org.elasticsearch.cloud.gce.GceInstancesService;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.network.NetworkAddress;
Expand All @@ -36,6 +34,7 @@
import java.util.function.Function;

import static java.util.Collections.emptyList;
import static org.elasticsearch.core.Strings.format;

public class GceSeedHostsProvider implements SeedHostsProvider {

Expand Down Expand Up @@ -257,7 +256,7 @@ public List<TransportAddress> getSeedAddresses(HostsResolver hostsResolver) {
}
} catch (Exception e) {
final String finalIpPrivate = ip_private;
logger.warn((Supplier<?>) () -> new ParameterizedMessage("failed to add {}, address {}", name, finalIpPrivate), e);
logger.warn(() -> format("failed to add %s, address %s", name, finalIpPrivate), e);
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.message.ParameterizedMessage;
import org.apache.logging.log4j.util.Supplier;
import org.elasticsearch.action.bulk.BulkItemResponse;
import org.elasticsearch.action.bulk.BulkRequestBuilder;
import org.elasticsearch.action.bulk.BulkResponse;
Expand All @@ -40,6 +38,7 @@
import java.util.concurrent.atomic.AtomicLong;
import java.util.function.Consumer;

import static org.elasticsearch.core.Strings.format;
import static org.hamcrest.Matchers.emptyIterable;
import static org.hamcrest.Matchers.equalTo;

Expand Down Expand Up @@ -206,10 +205,7 @@ public void run() {
} catch (Exception e) {
trackFailure(e);
final long docId = id;
logger.warn(
(Supplier<?>) () -> new ParameterizedMessage("**** failed indexing thread {} on doc id {}", indexerId, docId),
e
);
logger.warn(() -> format("**** failed indexing thread %s on doc id %s", indexerId, docId), e);
} finally {
stopLatch.countDown();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.message.ParameterizedMessage;
import org.apache.logging.log4j.util.Supplier;
import org.elasticsearch.common.settings.Setting;
import org.elasticsearch.common.settings.Setting.Property;
import org.elasticsearch.common.settings.Settings;
Expand All @@ -24,6 +22,8 @@
import java.util.Set;
import java.util.concurrent.CopyOnWriteArrayList;

import static org.elasticsearch.core.Strings.format;

/**
* A mock task manager that allows adding listeners for events
*/
Expand All @@ -50,13 +50,7 @@ public Task register(String type, String action, TaskAwareRequest request) {
try {
listener.onTaskRegistered(task);
} catch (Exception e) {
logger.warn(
(Supplier<?>) () -> new ParameterizedMessage(
"failed to notify task manager listener about registering the task with id {}",
task.getId()
),
e
);
logger.warn(() -> format("failed to notify task manager listener about registering the task with id %s", task.getId()), e);
}
}
return task;
Expand All @@ -71,10 +65,7 @@ public Task unregister(Task task) {
listener.onTaskUnregistered(task);
} catch (Exception e) {
logger.warn(
(Supplier<?>) () -> new ParameterizedMessage(
"failed to notify task manager listener about unregistering the task with id {}",
task.getId()
),
() -> format("failed to notify task manager listener about unregistering the task with id %s", task.getId()),
e
);
}
Expand All @@ -92,10 +83,7 @@ public void waitForTaskCompletion(Task task, long untilInNanos) {
listener.waitForTaskCompletion(task);
} catch (Exception e) {
logger.warn(
(Supplier<?>) () -> new ParameterizedMessage(
"failed to notify task manager listener about waitForTaskCompletion the task with id {}",
task.getId()
),
() -> format("failed to notify task manager listener about waitForTaskCompletion the task with id %s", task.getId()),
e
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.message.ParameterizedMessage;
import org.apache.logging.log4j.util.Supplier;
import org.apache.lucene.util.CollectionUtil;
import org.apache.lucene.util.Constants;
Expand Down Expand Up @@ -86,6 +85,7 @@

import static java.util.Collections.emptyMap;
import static java.util.Collections.emptySet;
import static org.elasticsearch.core.Strings.format;
import static org.elasticsearch.transport.TransportService.NOOP_TRANSPORT_INTERCEPTOR;
import static org.hamcrest.Matchers.allOf;
import static org.hamcrest.Matchers.containsString;
Expand Down Expand Up @@ -902,10 +902,7 @@ protected void doRun() throws Exception {
try {
listener.actionGet();
} catch (Exception e) {
logger.trace(
(Supplier<?>) () -> new ParameterizedMessage("caught exception while sending to node {}", nodeA),
e
);
logger.trace(() -> format("caught exception while sending to node %s", nodeA), e);
}
}
}
Expand Down Expand Up @@ -945,10 +942,7 @@ protected void doRun() throws Exception {
} catch (ConnectTransportException e) {
// ok!
} catch (Exception e) {
logger.error(
(Supplier<?>) () -> new ParameterizedMessage("caught exception while sending to node {}", node),
e
);
logger.error(() -> format("caught exception while sending to node %s", node), e);
sendingErrors.add(e);
}
} catch (NodeNotConnectedException ex) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import java.util.List;
import java.util.stream.Stream;

import static org.elasticsearch.core.Strings.format;
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.lessThanOrEqualTo;
Expand Down Expand Up @@ -166,28 +167,20 @@ public void checkFailNumberOfArguments3() {
logger.info("Hello {}, {}, {}, {}, {}, {}, {}", "world", 2, "third argument", 4, 5, 6, 7, new String("last arg"));
}

public void checkNumberOfArgumentsParameterizedMessage3() {
logger.info((Supplier<?>) () -> new ParameterizedMessage("Hello {}, {}, {}", "world", 2, "third argument"));
}

public void checkFailNumberOfArgumentsParameterizedMessage3() {
logger.info((Supplier<?>) () -> new ParameterizedMessage("Hello {}, {}", "world", 2, "third argument"));
}

public void checkOrderOfExceptionArgument() {
logger.info("Hello", new Exception());
}

public void checkOrderOfExceptionArgument1() {
logger.info((Supplier<?>) () -> new ParameterizedMessage("Hello {}", "world"), new Exception());
logger.info(() -> format("Hello %s", "world"), new Exception());
}

public void checkFailOrderOfExceptionArgument1() {
logger.info("Hello {}", "world", new Exception());
}

public void checkOrderOfExceptionArgument2() {
logger.info((Supplier<?>) () -> new ParameterizedMessage("Hello {}, {}", "world", 42), new Exception());
logger.info(() -> format("Hello %s, %s", "world", 42), new Exception());
}

public void checkFailOrderOfExceptionArgument2() {
Expand All @@ -198,10 +191,6 @@ public void checkNonConstantMessageWithZeroArguments(boolean b) {
logger.info(Boolean.toString(b), new Exception());
}

public void checkFailNonConstantMessageWithArguments(boolean b) {
logger.info((Supplier<?>) () -> new ParameterizedMessage(Boolean.toString(b), 42), new Exception());
}

public void checkComplexUsage(boolean b) {
String message = "Hello {}, {}";
Object[] args = new Object[] { "world", 42 };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
package org.elasticsearch.license;

import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.message.ParameterizedMessage;
import org.apache.logging.log4j.util.Supplier;
import org.apache.lucene.util.BytesRef;
import org.elasticsearch.license.License.OperationMode;
import org.elasticsearch.watcher.FileChangesListener;
Expand All @@ -20,6 +18,8 @@
import java.nio.file.Path;
import java.util.concurrent.atomic.AtomicBoolean;

import static org.elasticsearch.core.Strings.format;

/**
* File based watcher for license {@link OperationMode}
* Watches for changes in <code>licenseModePath</code>, use
Expand Down Expand Up @@ -97,27 +97,15 @@ private synchronized void onChange(Path file) {
try {
content = Files.readAllBytes(licenseModePath);
} catch (IOException e) {
logger.error(
(Supplier<?>) () -> new ParameterizedMessage(
"couldn't read operation mode from [{}]",
licenseModePath.toAbsolutePath()
),
e
);
logger.error(() -> format("couldn't read operation mode from [%s]", licenseModePath.toAbsolutePath()), e);
return;
}
// this UTF-8 conversion is much pickier than java String
final String operationMode = new BytesRef(content).utf8ToString();
try {
newOperationMode = OperationMode.parse(operationMode);
} catch (IllegalArgumentException e) {
logger.error(
(Supplier<?>) () -> new ParameterizedMessage(
"invalid operation mode in [{}]",
licenseModePath.toAbsolutePath()
),
e
);
logger.error(() -> format("invalid operation mode in [%s]", licenseModePath.toAbsolutePath()), e);
return;
}
}
Expand Down

0 comments on commit 705b27a

Please sign in to comment.