Migrate logging from slf4j to flogger
and fix some log statements to join elements of collections to format
them properly.
Change-Id: I69b640ae98e3a320cdf957b8c84481f406780d1b
diff --git a/src/main/java/com/googlesource/gerrit/plugins/healthcheck/check/AbstractHealthCheck.java b/src/main/java/com/googlesource/gerrit/plugins/healthcheck/check/AbstractHealthCheck.java
index e65d269..ba6d75a 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/healthcheck/check/AbstractHealthCheck.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/healthcheck/check/AbstractHealthCheck.java
@@ -14,6 +14,7 @@
package com.googlesource.gerrit.plugins.healthcheck.check;
+import com.google.common.flogger.FluentLogger;
import com.google.common.util.concurrent.ListenableFuture;
import com.google.common.util.concurrent.ListeningExecutorService;
import com.google.gerrit.metrics.Counter0;
@@ -25,11 +26,9 @@
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
public abstract class AbstractHealthCheck implements HealthCheck {
- private static final Logger log = LoggerFactory.getLogger(AbstractHealthCheck.class);
+ private static final FluentLogger logger = FluentLogger.forEnclosingClass();
private final long timeout;
private final String name;
private final ListeningExecutorService executor;
@@ -72,7 +71,7 @@
try {
healthy = enabled ? doCheck() : Result.DISABLED;
} catch (Exception e) {
- log.warn("Check {} failed", name, e);
+ logger.atWarning().withCause(e).log("Check %s failed", name);
healthy = Result.FAILED;
}
Long elapsed = System.currentTimeMillis() - ts;
@@ -106,7 +105,7 @@
ListenableFuture<StatusSummary> future, long ts, Exception e, String message, Result result) {
future.cancel(true);
Long elapsed = System.currentTimeMillis() - ts;
- log.warn(message, e);
+ logger.atWarning().withCause(e).log("%s", message);
StatusSummary checkStatusSummary =
new StatusSummary(result, ts, elapsed, Collections.emptyMap());
failureCounterMetric.increment();
diff --git a/src/main/java/com/googlesource/gerrit/plugins/healthcheck/check/AuthHealthCheck.java b/src/main/java/com/googlesource/gerrit/plugins/healthcheck/check/AuthHealthCheck.java
index 2dc22dc..ffb7d16 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/healthcheck/check/AuthHealthCheck.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/healthcheck/check/AuthHealthCheck.java
@@ -16,6 +16,7 @@
import static com.googlesource.gerrit.plugins.healthcheck.check.HealthCheckNames.AUTH;
+import com.google.common.flogger.FluentLogger;
import com.google.common.util.concurrent.ListeningExecutorService;
import com.google.gerrit.metrics.MetricMaker;
import com.google.gerrit.server.account.AccountCache;
@@ -26,12 +27,10 @@
import com.google.inject.Singleton;
import com.googlesource.gerrit.plugins.healthcheck.HealthCheckConfig;
import java.util.Optional;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
@Singleton
public class AuthHealthCheck extends AbstractHealthCheck {
- private static final Logger log = LoggerFactory.getLogger(AuthHealthCheck.class);
+ private static final FluentLogger logger = FluentLogger.forEnclosingClass();
private final Realm realm;
private final AccountCache byIdCache;
private final String username;
@@ -63,11 +62,11 @@
Optional<AccountState> accountState = byIdCache.getByUsername(username);
if (!accountState.isPresent()) {
- log.error("Cannot load account state for username " + username);
+ logger.atSevere().log("Cannot load account state for username %s", username);
return Result.FAILED;
}
if (!accountState.get().account().isActive()) {
- log.error("Authentication error, account " + username + " is inactive");
+ logger.atSevere().log("Authentication error, account %s is inactive", username);
return Result.FAILED;
}
return Result.PASSED;
diff --git a/src/main/java/com/googlesource/gerrit/plugins/healthcheck/check/BlockedThreadsConfigurator.java b/src/main/java/com/googlesource/gerrit/plugins/healthcheck/check/BlockedThreadsConfigurator.java
index 40030e1..8bfe53c 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/healthcheck/check/BlockedThreadsConfigurator.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/healthcheck/check/BlockedThreadsConfigurator.java
@@ -19,6 +19,7 @@
import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.ImmutableSet;
+import com.google.common.flogger.FluentLogger;
import com.google.inject.Inject;
import com.googlesource.gerrit.plugins.healthcheck.HealthCheckConfig;
import com.googlesource.gerrit.plugins.healthcheck.check.BlockedThreadsCheck.Collector;
@@ -32,12 +33,11 @@
import java.util.Optional;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
+import java.util.stream.Collectors;
@VisibleForTesting
public class BlockedThreadsConfigurator {
- private static final Logger log = LoggerFactory.getLogger(BlockedThreadsConfigurator.class);
+ private static final FluentLogger logger = FluentLogger.forEnclosingClass();
private static final Pattern THRESHOLD_PATTERN = Pattern.compile("^(\\d\\d?)$");
static final int DEFAULT_BLOCKED_THREADS_THRESHOLD = 50;
@@ -89,10 +89,10 @@
// check configuration consistency
if (specsClassified.size() > 1) {
Collection<Threshold> specs = deduplicatePrefixes(specsClassified.get(true));
- log.warn(
+ logger.atWarning().log(
"Global and specific thresholds were configured for blocked threads check. Specific"
- + " configuration is used {}.",
- specs);
+ + " configuration is used %s.",
+ specs.stream().map(Threshold::toString).collect(Collectors.joining(", ")));
return specs;
}
@@ -103,8 +103,8 @@
: deduplicateGlobal(entry.getValue());
}
- log.info(
- "Default blocked threads check is configured with {}% threshold",
+ logger.atInfo().log(
+ "Default blocked threads check is configured with %d%% threshold",
DEFAULT_BLOCKED_THREADS_THRESHOLD);
return ImmutableSet.of(new Threshold(DEFAULT_BLOCKED_THREADS_THRESHOLD));
}
@@ -112,7 +112,7 @@
private static Collection<Threshold> deduplicateGlobal(List<Threshold> input) {
if (input.size() > 1) {
Threshold spec = input.get(input.size() - 1);
- log.warn("Multiple threshold values were configured. Using {}", spec);
+ logger.atWarning().log("Multiple threshold values were configured. Using %s", spec);
return ImmutableSet.of(spec);
}
return input;
@@ -122,10 +122,12 @@
Map<String, Threshold> deduplicated = new HashMap<>();
input.forEach(t -> deduplicated.put(t.prefix.get(), t));
if (deduplicated.size() != input.size()) {
- log.warn(
+ logger.atWarning().log(
"The same prefixes were configured multiple times. The following configuration is used"
- + " {}",
- deduplicated.values());
+ + " %s",
+ deduplicated.values().stream()
+ .map(Threshold::toString)
+ .collect(Collectors.joining(", ")));
}
return deduplicated.values();
}
@@ -144,7 +146,7 @@
}
}
- log.warn("Invalid configuration of blocked threads threshold [{}]", spec);
+ logger.atWarning().log("Invalid configuration of blocked threads threshold [%s]", spec);
return Optional.empty();
}
diff --git a/src/main/java/com/googlesource/gerrit/plugins/healthcheck/check/ChangesIndexHealthCheck.java b/src/main/java/com/googlesource/gerrit/plugins/healthcheck/check/ChangesIndexHealthCheck.java
index 324ec70..ab281f9 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/healthcheck/check/ChangesIndexHealthCheck.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/healthcheck/check/ChangesIndexHealthCheck.java
@@ -16,6 +16,7 @@
import static com.googlesource.gerrit.plugins.healthcheck.check.HealthCheckNames.CHANGES_INDEX;
+import com.google.common.flogger.FluentLogger;
import com.google.common.util.concurrent.ListeningExecutorService;
import com.google.gerrit.index.IndexType;
import com.google.gerrit.metrics.MetricMaker;
@@ -34,12 +35,10 @@
import org.eclipse.jgit.lib.Config;
import org.eclipse.jgit.storage.file.FileBasedConfig;
import org.eclipse.jgit.util.FS;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
@Singleton
public class ChangesIndexHealthCheck extends AbstractHealthCheck implements OnlineUpgradeListener {
- private static final Logger log = LoggerFactory.getLogger(QueryChangesHealthCheck.class);
+ private static final FluentLogger logger = FluentLogger.forEnclosingClass();
private static final String lockFilename = "write.lock";
private final SitePaths sitePaths;
@@ -82,13 +81,13 @@
Optional.of(
getChangesLockFiles(sitePaths.index_dir, String.format("changes_%04d", newVersion)));
if (!changes.compareAndSet(changes.get(), newLockFiles)) {
- log.info(
- "New version {} of changes index healthcheck lock files was set already by another"
+ logger.atInfo().log(
+ "New version %d of changes index healthcheck lock files was set already by another"
+ " thread",
newVersion);
} else {
- log.info(
- "Changes index healthcheck switched from index version {} to {}", oldVersion, newVersion);
+ logger.atInfo().log(
+ "Changes index healthcheck switched from index version %d to %d", oldVersion, newVersion);
}
}
@@ -99,8 +98,8 @@
IndexType indexType = new IndexType(cfg.getString("index", null, "type"));
boolean isLucene = indexType.isLucene();
if (!isLucene) {
- log.warn(
- "Configured index type [{}] is not supported for index health check therefore it is"
+ logger.atWarning().log(
+ "Configured index type [%s] is not supported for index health check therefore it is"
+ " disabled.",
indexType);
}
@@ -119,7 +118,7 @@
return getActiveIndexVersion(cfg, "changes")
.map(version -> getChangesLockFiles(indexDir, version));
} catch (IOException | ConfigInvalidException e) {
- log.error("Getting changes index version from configuration failed", e);
+ logger.atSevere().withCause(e).log("Getting changes index version from configuration failed");
}
return Optional.empty();
}
diff --git a/src/main/java/com/googlesource/gerrit/plugins/healthcheck/check/ProjectsListHealthCheck.java b/src/main/java/com/googlesource/gerrit/plugins/healthcheck/check/ProjectsListHealthCheck.java
index d448cf4..6a4f166 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/healthcheck/check/ProjectsListHealthCheck.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/healthcheck/check/ProjectsListHealthCheck.java
@@ -16,6 +16,7 @@
import static com.googlesource.gerrit.plugins.healthcheck.check.HealthCheckNames.PROJECTSLIST;
+import com.google.common.flogger.FluentLogger;
import com.google.common.util.concurrent.ListeningExecutorService;
import com.google.gerrit.extensions.common.ProjectInfo;
import com.google.gerrit.metrics.MetricMaker;
@@ -27,12 +28,10 @@
import com.google.inject.Singleton;
import com.googlesource.gerrit.plugins.healthcheck.HealthCheckConfig;
import java.util.SortedMap;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
@Singleton
public class ProjectsListHealthCheck extends AbstractHealthCheck {
- private static final Logger log = LoggerFactory.getLogger(ProjectsListHealthCheck.class);
+ private static final FluentLogger logger = FluentLogger.forEnclosingClass();
private static final int PROJECTS_LIST_LIMIT = 100;
private final Provider<ListProjects> listProjectsProvider;
private final OneOffRequestContext oneOffCtx;
@@ -63,9 +62,10 @@
if (projects != null && projects.size() > 0) {
return Result.PASSED;
}
- log.warn("Empty or null projects list: Gerrit should always have at least 1 project");
+ logger.atWarning().log(
+ "Empty or null projects list: Gerrit should always have at least 1 project");
} catch (Exception e) {
- log.warn("Unable to list projects", e);
+ logger.atWarning().withCause(e).log("Unable to list projects");
}
return Result.FAILED;
}
diff --git a/src/main/java/com/googlesource/gerrit/plugins/healthcheck/check/QueryChangesHealthCheck.java b/src/main/java/com/googlesource/gerrit/plugins/healthcheck/check/QueryChangesHealthCheck.java
index 7165b9f..c9bc3d1 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/healthcheck/check/QueryChangesHealthCheck.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/healthcheck/check/QueryChangesHealthCheck.java
@@ -16,6 +16,7 @@
import static com.googlesource.gerrit.plugins.healthcheck.check.HealthCheckNames.QUERYCHANGES;
+import com.google.common.flogger.FluentLogger;
import com.google.common.util.concurrent.ListeningExecutorService;
import com.google.gerrit.metrics.MetricMaker;
import com.google.gerrit.server.restapi.change.QueryChanges;
@@ -26,12 +27,10 @@
import com.google.inject.Singleton;
import com.googlesource.gerrit.plugins.healthcheck.HealthCheckConfig;
import java.util.List;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
@Singleton
public class QueryChangesHealthCheck extends AbstractHealthCheck {
- private static final Logger log = LoggerFactory.getLogger(QueryChangesHealthCheck.class);
+ private static final FluentLogger logger = FluentLogger.forEnclosingClass();
private final Provider<QueryChanges> queryChangesProvider;
private final int limit;
private final OneOffRequestContext oneOffCtx;
@@ -62,15 +61,14 @@
List<?> changes = queryChanges.apply(null).value();
if (changes == null) {
- log.warn("Cannot query changes: received a null list of results");
+ logger.atWarning().log("Cannot query changes: received a null list of results");
return Result.FAILED;
}
if (changes.size() < limit) {
- log.warn(
- "Query changes did not return enough items: expected {} items but got only {}",
- limit,
- changes.size());
+ logger.atWarning().log(
+ "Query changes did not return enough items: expected %d items but got only %d",
+ limit, changes.size());
return Result.FAILED;
}