Migrate all remaining classes to Flogger

This is the next part of the migration to Flogger. This change migrates
all remaining classes Flogger. This means all classes in Gerrit core now
use Flogger.

During this migration we try to make the log statements more consistent:
- avoid string concatenation
- avoid usage of String.format(...)

The visibility of the slf4j library is restricted to plugins and jgit
now. This avoids that we accidentally add new dependencies to slf4j.

Change-Id: Ide573327315a15cde69b68b5f27934deeb790d37
Signed-off-by: Edwin Kempin <ekempin@google.com>
This commit is contained in:
Edwin Kempin 2018-05-08 10:12:57 +02:00
parent a8fec0e227
commit 6ed96ca40b
40 changed files with 142 additions and 162 deletions

View File

@ -251,12 +251,6 @@ maven_jar(
sha1 = "2b8019b6249bb05d81d3a3094e468753e2b21311",
)
maven_jar(
name = "log_nop",
artifact = "org.slf4j:slf4j-nop:" + SLF4J_VERS,
sha1 = "6cca9a3b999ff28b7a35ca762b3197cd7e4c2ad1",
)
maven_jar(
name = "log_ext",
artifact = "org.slf4j:slf4j-ext:" + SLF4J_VERS,

View File

@ -6,11 +6,11 @@ java_library(
"//java/com/google/gerrit/pgm",
"//java/com/google/gerrit/pgm/util",
"//java/com/google/gerrit/util/cli",
"//lib/flogger:api",
"//lib/gwt:dev",
"//lib/jetty:server",
"//lib/jetty:servlet",
"//lib/jetty:servlets",
"//lib/log:api",
"//lib/log:log4j",
],
)

View File

@ -14,16 +14,15 @@
package com.google.gerrit.gwtdebug;
import com.google.common.flogger.FluentLogger;
import com.google.gerrit.pgm.Daemon;
import com.google.gwt.dev.codeserver.CodeServer;
import com.google.gwt.dev.codeserver.Options;
import java.util.ArrayList;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
class GerritGwtDebugLauncher {
private static final Logger log = LoggerFactory.getLogger(GerritGwtDebugLauncher.class);
private static final FluentLogger logger = FluentLogger.forEnclosingClass();
public static void main(String[] argv) throws Exception {
GerritGwtDebugLauncher launcher = new GerritGwtDebugLauncher();
@ -54,7 +53,7 @@ class GerritGwtDebugLauncher {
Options options = new Options();
if (!options.parseArgs(sdmLauncherOptions.toArray(new String[sdmLauncherOptions.size()]))) {
log.error("Failed to parse codeserver arguments");
logger.atSevere().log("Failed to parse codeserver arguments");
return 1;
}
@ -65,11 +64,11 @@ class GerritGwtDebugLauncher {
new Daemon()
.main(daemonLauncherOptions.toArray(new String[daemonLauncherOptions.size()]));
if (r != 0) {
log.error("Daemon exited with return code: " + r);
logger.atSevere().log("Daemon exited with return code: %d", r);
return 1;
}
} catch (Exception e) {
log.error("Cannot start daemon", e);
logger.atSevere().withCause(e).log("Cannot start daemon");
return 1;
}

View File

@ -42,11 +42,11 @@ java_library(
"//lib/bouncycastle:bcpg",
"//lib/bouncycastle:bcprov",
"//lib/commons:compress",
"//lib/flogger:api",
"//lib/guice",
"//lib/guice:guice-assistedinject",
"//lib/guice:guice-servlet",
"//lib/jgit/org.eclipse.jgit:jgit",
"//lib/log:api",
"//lib/mina:sshd",
"//prolog:gerrit-prolog-common",
],

View File

@ -13,8 +13,6 @@ java_library(
"//lib:args4j",
"//lib:guava",
"//lib/asciidoctor",
"//lib/log:api",
"//lib/log:nop",
],
)

View File

@ -24,8 +24,8 @@ gwt_module(
"//lib:servlet-api-3_1",
"//lib/auto:auto-value",
"//lib/auto:auto-value-annotations",
"//lib/flogger:api",
"//lib/jgit/org.eclipse.jgit:jgit",
"//lib/log:api",
],
gwt_xml = "Common.gwt.xml",
visibility = ["//visibility:public"],
@ -50,8 +50,8 @@ java_library(
"//lib:servlet-api-3_1",
"//lib/auto:auto-value",
"//lib/auto:auto-value-annotations",
"//lib/flogger:api",
"//lib/jgit/org.eclipse.jgit:jgit",
"//lib/log:api",
],
)

View File

@ -21,26 +21,25 @@ import com.google.common.annotations.GwtIncompatible;
import com.google.common.collect.ComparisonChain;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Ordering;
import com.google.common.flogger.FluentLogger;
import java.io.IOException;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.NoSuchFileException;
import java.nio.file.Path;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@GwtIncompatible("Unemulated classes in java.nio and Guava")
public final class SiteLibraryLoaderUtil {
private static final Logger log = LoggerFactory.getLogger(SiteLibraryLoaderUtil.class);
private static final FluentLogger logger = FluentLogger.forEnclosingClass();
public static void loadSiteLib(Path libdir) {
try {
List<Path> jars = listJars(libdir);
IoUtil.loadJARs(jars);
log.debug("Loaded site libraries: {}", jarList(jars));
logger.atFine().log("Loaded site libraries: %s", jarList(jars));
} catch (IOException e) {
log.error("Error scanning lib directory " + libdir, e);
logger.atSevere().withCause(e).log("Error scanning lib directory %s", libdir);
}
}

View File

@ -18,16 +18,15 @@ import static java.nio.charset.StandardCharsets.UTF_8;
import com.google.common.annotations.GwtIncompatible;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.flogger.FluentLogger;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@GwtIncompatible("Unemulated com.google.gerrit.common.Version")
public class Version {
private static final Logger log = LoggerFactory.getLogger(Version.class);
private static final FluentLogger logger = FluentLogger.forEnclosingClass();
@VisibleForTesting static final String DEV = "(dev)";
@ -57,7 +56,7 @@ public class Version {
return vs;
}
} catch (IOException e) {
log.error(e.getMessage(), e);
logger.atSevere().withCause(e).log(e.getMessage());
return "(unknown version)";
}
}

View File

@ -23,6 +23,7 @@ import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.ListMultimap;
import com.google.common.collect.Lists;
import com.google.common.flogger.FluentLogger;
import com.google.common.io.CharStreams;
import com.google.gerrit.elasticsearch.builders.QueryBuilder;
import com.google.gerrit.elasticsearch.builders.SearchSourceBuilder;
@ -69,11 +70,9 @@ import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.nio.entity.NStringEntity;
import org.elasticsearch.client.Response;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
abstract class AbstractElasticIndex<K, V> implements Index<K, V> {
private static final Logger log = LoggerFactory.getLogger(AbstractElasticIndex.class);
private static final FluentLogger logger = FluentLogger.forEnclosingClass();
protected static final String BULK = "_bulk";
protected static final String IGNORE_UNMAPPED = "ignore_unmapped";
@ -332,7 +331,7 @@ abstract class AbstractElasticIndex<K, V> implements Index<K, V> {
}
}
} else {
log.error(statusLine.getReasonPhrase());
logger.atSevere().log(statusLine.getReasonPhrase());
}
final List<T> r = Collections.unmodifiableList(results);
return new ResultSet<T>() {

View File

@ -17,6 +17,7 @@ java_library(
"//lib/commons:codec",
"//lib/commons:lang",
"//lib/elasticsearch-rest-client",
"//lib/flogger:api",
"//lib/guice",
"//lib/guice:guice-assistedinject",
"//lib/httpcomponents:httpasyncclient",
@ -25,6 +26,5 @@ java_library(
"//lib/httpcomponents:httpcore-nio",
"//lib/jackson:jackson-core",
"//lib/jgit/org.eclipse.jgit:jgit",
"//lib/log:api",
],
)

View File

@ -15,6 +15,7 @@
package com.google.gerrit.elasticsearch;
import com.google.common.base.Strings;
import com.google.common.flogger.FluentLogger;
import com.google.common.primitives.Ints;
import com.google.gerrit.extensions.registration.DynamicSet;
import com.google.gerrit.index.Index;
@ -31,12 +32,10 @@ import java.io.IOException;
import java.util.Collection;
import java.util.TreeMap;
import org.eclipse.jgit.lib.Config;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Singleton
public class ElasticIndexVersionManager extends VersionManager {
private static final Logger log = LoggerFactory.getLogger(ElasticIndexVersionManager.class);
private static final FluentLogger logger = FluentLogger.forEnclosingClass();
private final String prefix;
private final ElasticIndexVersionDiscovery versionDiscovery;
@ -61,13 +60,13 @@ public class ElasticIndexVersionManager extends VersionManager {
for (String version : versionDiscovery.discover(prefix, def.getName())) {
Integer v = Ints.tryParse(version);
if (v == null || version.length() != 4) {
log.warn("Unrecognized version in index {}: {}", def.getName(), version);
logger.atWarning().log("Unrecognized version in index %s: %s", def.getName(), version);
continue;
}
versions.put(v, new Version<V>(null, v, true, cfg.getReady(def.getName(), v)));
}
} catch (IOException e) {
log.error("Error scanning index: " + def.getName(), e);
logger.atSevere().withCause(e).log("Error scanning index: %s", def.getName());
}
for (Schema<V> schema : def.getSchemas().values()) {

View File

@ -14,6 +14,7 @@
package com.google.gerrit.elasticsearch;
import com.google.common.flogger.FluentLogger;
import com.google.gerrit.extensions.events.LifecycleListener;
import com.google.gerrit.lifecycle.LifecycleModule;
import com.google.gson.JsonParser;
@ -32,12 +33,10 @@ import org.apache.http.impl.nio.client.HttpAsyncClientBuilder;
import org.elasticsearch.client.Response;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Singleton
class ElasticRestClientProvider implements Provider<RestClient>, LifecycleListener {
private static final Logger log = LoggerFactory.getLogger(ElasticRestClientProvider.class);
private static final FluentLogger logger = FluentLogger.forEnclosingClass();
private final HttpHost[] hosts;
private final String username;
@ -68,7 +67,7 @@ class ElasticRestClientProvider implements Provider<RestClient>, LifecycleListen
if (client == null) {
client = build();
ElasticVersion version = getVersion();
log.info("Elasticsearch integration version {}", version);
logger.atInfo().log("Elasticsearch integration version %s", version);
}
}
}
@ -117,7 +116,7 @@ class ElasticRestClientProvider implements Provider<RestClient>, LifecycleListen
.getAsJsonObject()
.get("number")
.getAsString();
log.info("Connected to Elasticsearch version {}", version);
logger.atInfo().log("Connected to Elasticsearch version %s", version);
return ElasticVersion.forVersion(version);
} catch (IOException e) {
throw new FailedToGetVersion(e);

View File

@ -11,9 +11,9 @@ java_library(
"//lib:gwtorm",
"//lib/bouncycastle:bcpg-neverlink",
"//lib/bouncycastle:bcprov-neverlink",
"//lib/flogger:api",
"//lib/guice",
"//lib/guice:guice-assistedinject",
"//lib/jgit/org.eclipse.jgit:jgit",
"//lib/log:api",
],
)

View File

@ -20,6 +20,7 @@ import static com.google.gerrit.server.account.externalids.ExternalId.SCHEME_GPG
import com.google.common.base.CharMatcher;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Maps;
import com.google.common.flogger.FluentLogger;
import com.google.common.io.BaseEncoding;
import com.google.gerrit.common.PageLinks;
import com.google.gerrit.server.IdentifiedUser;
@ -44,8 +45,6 @@ import org.bouncycastle.openpgp.PGPSignature;
import org.bouncycastle.openpgp.operator.bc.BcPGPContentVerifierBuilderProvider;
import org.eclipse.jgit.lib.Config;
import org.eclipse.jgit.transport.PushCertificateIdent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Checker for GPG public keys including Gerrit-specific checks.
@ -54,7 +53,7 @@ import org.slf4j.LoggerFactory;
* ID in the database, or an email address thereof.
*/
public class GerritPublicKeyChecker extends PublicKeyChecker {
private static final Logger log = LoggerFactory.getLogger(GerritPublicKeyChecker.class);
private static final FluentLogger logger = FluentLogger.forEnclosingClass();
@Singleton
public static class Factory {
@ -137,7 +136,7 @@ public class GerritPublicKeyChecker extends PublicKeyChecker {
return checkIdsForArbitraryUser(key);
} catch (PGPException | OrmException e) {
String msg = "Error checking user IDs for key";
log.warn(msg + " " + keyIdToString(key.getKeyID()), e);
logger.atWarning().withCause(e).log("%s %s", msg, keyIdToString(key.getKeyID()));
return CheckResult.bad(msg);
}
}

View File

@ -14,15 +14,14 @@
package com.google.gerrit.gpg;
import com.google.common.flogger.FluentLogger;
import com.google.gerrit.extensions.config.FactoryModule;
import com.google.gerrit.gpg.api.GpgApiModule;
import com.google.gerrit.server.EnableSignedPush;
import org.eclipse.jgit.lib.Config;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class GpgModule extends FactoryModule {
private static final Logger log = LoggerFactory.getLogger(GpgModule.class);
private static final FluentLogger logger = FluentLogger.forEnclosingClass();
private final Config cfg;
@ -39,7 +38,7 @@ public class GpgModule extends FactoryModule {
bindConstant().annotatedWith(EnableSignedPush.class).to(enableSignedPush);
if (configEnableSignedPush && !havePgp) {
log.info("Bouncy Castle PGP not installed; signed push verification is disabled");
logger.atInfo().log("Bouncy Castle PGP not installed; signed push verification is disabled");
}
if (enableSignedPush) {
install(new SignedPushModule());

View File

@ -28,6 +28,7 @@ import static org.bouncycastle.bcpg.sig.RevocationReasonTags.NO_REASON;
import static org.bouncycastle.openpgp.PGPSignature.DIRECT_KEY;
import static org.bouncycastle.openpgp.PGPSignature.KEY_REVOCATION;
import com.google.common.flogger.FluentLogger;
import com.google.gerrit.extensions.common.GpgKeyInfo.Status;
import java.io.IOException;
import java.util.ArrayList;
@ -49,12 +50,10 @@ import org.bouncycastle.openpgp.PGPPublicKeyRing;
import org.bouncycastle.openpgp.PGPPublicKeyRingCollection;
import org.bouncycastle.openpgp.PGPSignature;
import org.bouncycastle.openpgp.operator.bc.BcPGPContentVerifierBuilderProvider;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/** Checker for GPG public keys for use in a push certificate. */
public class PublicKeyChecker {
private static final Logger log = LoggerFactory.getLogger(PublicKeyChecker.class);
private static final FluentLogger logger = FluentLogger.forEnclosingClass();
// https://tools.ietf.org/html/rfc4880#section-5.2.3.13
private static final int COMPLETE_TRUST = 120;
@ -294,12 +293,11 @@ public class PublicKeyChecker {
// Revoker is authorized and there is a revocation signature by this
// revoker, but the key is not in the store so we can't verify the
// signature.
log.info(
"Key "
+ Fingerprint.toString(key.getFingerprint())
+ " is revoked by "
+ Fingerprint.toString(rfp)
+ ", which is not in the store. Assuming revocation is valid.");
logger
.atInfo()
.log(
"Key %s is revoked by %s, which is not in the store. Assuming revocation is valid.",
Fingerprint.toString(key.getFingerprint()), Fingerprint.toString(rfp));
problems.add(reasonToString(getRevocationReason(revocation)));
continue;
}

View File

@ -21,6 +21,7 @@ import static com.google.gerrit.gpg.PublicKeyStore.keyIdToString;
import static com.google.gerrit.gpg.PublicKeyStore.keyToString;
import com.google.common.base.Joiner;
import com.google.common.flogger.FluentLogger;
import com.google.gerrit.extensions.common.GpgKeyInfo.Status;
import java.io.ByteArrayInputStream;
import java.io.IOException;
@ -38,12 +39,10 @@ import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.transport.PushCertificate;
import org.eclipse.jgit.transport.PushCertificate.NonceStatus;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/** Checker for push certificates. */
public abstract class PushCertificateChecker {
private static final Logger log = LoggerFactory.getLogger(PushCertificateChecker.class);
private static final FluentLogger logger = FluentLogger.forEnclosingClass();
public static class Result {
private final PGPPublicKey key;
@ -107,7 +106,7 @@ public abstract class PushCertificateChecker {
}
} catch (PGPException | IOException e) {
String msg = "Internal error checking push certificate";
log.error(msg, e);
logger.atSevere().withCause(e).log(msg);
results.add(CheckResult.bad(msg));
}

View File

@ -15,6 +15,7 @@
package com.google.gerrit.gpg;
import com.google.common.base.Strings;
import com.google.common.flogger.FluentLogger;
import com.google.gerrit.extensions.registration.DynamicSet;
import com.google.gerrit.reviewdb.client.BooleanProjectConfig;
import com.google.gerrit.reviewdb.client.Project;
@ -42,11 +43,9 @@ import org.eclipse.jgit.transport.PreReceiveHook;
import org.eclipse.jgit.transport.PreReceiveHookChain;
import org.eclipse.jgit.transport.ReceivePack;
import org.eclipse.jgit.transport.SignedPushConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
class SignedPushModule extends AbstractModule {
private static final Logger log = LoggerFactory.getLogger(SignedPushModule.class);
private static final FluentLogger logger = FluentLogger.forEnclosingClass();
@Override
protected void configure() {
@ -93,11 +92,13 @@ class SignedPushModule extends AbstractModule {
rp.setSignedPushConfig(null);
return;
} else if (signedPushConfig == null) {
log.error(
"receive.enableSignedPush is true for project {} but"
+ " false in gerrit.config, so signed push verification is"
+ " disabled",
project.get());
logger
.atSevere()
.log(
"receive.enableSignedPush is true for project %s but"
+ " false in gerrit.config, so signed push verification is"
+ " disabled",
project.get());
rp.setSignedPushConfig(null);
return;
}

View File

@ -19,6 +19,7 @@ import static java.nio.charset.StandardCharsets.UTF_8;
import com.google.common.base.CharMatcher;
import com.google.common.collect.ImmutableList;
import com.google.common.flogger.FluentLogger;
import com.google.common.io.BaseEncoding;
import com.google.gerrit.extensions.common.GpgKeyInfo;
import com.google.gerrit.extensions.registration.DynamicMap;
@ -53,12 +54,10 @@ import org.bouncycastle.openpgp.PGPException;
import org.bouncycastle.openpgp.PGPPublicKey;
import org.bouncycastle.openpgp.PGPPublicKeyRing;
import org.eclipse.jgit.util.NB;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Singleton
public class GpgKeys implements ChildCollection<AccountResource, GpgKey> {
private static final Logger log = LoggerFactory.getLogger(GpgKeys.class);
private static final FluentLogger logger = FluentLogger.forEnclosingClass();
public static final String MIME_TYPE = "application/pgp-keys";
@ -164,7 +163,9 @@ public class GpgKeys implements ChildCollection<AccountResource, GpgKey> {
}
}
if (!found) {
log.warn("No public key stored for fingerprint {}", Fingerprint.toString(fp));
logger
.atWarning()
.log("No public key stored for fingerprint %s", Fingerprint.toString(fp));
}
}
}

View File

@ -24,6 +24,7 @@ import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.flogger.FluentLogger;
import com.google.common.io.BaseEncoding;
import com.google.gerrit.common.errors.EmailException;
import com.google.gerrit.extensions.api.accounts.GpgKeysInput;
@ -70,12 +71,11 @@ import org.eclipse.jgit.errors.ConfigInvalidException;
import org.eclipse.jgit.lib.CommitBuilder;
import org.eclipse.jgit.lib.PersonIdent;
import org.eclipse.jgit.lib.RefUpdate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Singleton
public class PostGpgKeys implements RestModifyView<AccountResource, GpgKeysInput> {
private final Logger log = LoggerFactory.getLogger(getClass());
private static final FluentLogger logger = FluentLogger.forEnclosingClass();
private final Provider<PersonIdent> serverIdent;
private final Provider<CurrentUser> self;
private final Provider<PublicKeyStore> storeProvider;
@ -223,10 +223,12 @@ public class PostGpgKeys implements RestModifyView<AccountResource, GpgKeysInput
try {
addKeyFactory.create(rsrc.getUser(), addedKeys).send();
} catch (EmailException e) {
log.error(
"Cannot send GPG key added message to "
+ rsrc.getUser().getAccount().getPreferredEmail(),
e);
logger
.atSevere()
.withCause(e)
.log(
"Cannot send GPG key added message to %s",
rsrc.getUser().getAccount().getPreferredEmail());
}
break;
case NO_CHANGE:

View File

@ -43,7 +43,7 @@ java_library(
"//lib/antlr:java_runtime",
"//lib/auto:auto-value",
"//lib/auto:auto-value-annotations",
"//lib/flogger:api",
"//lib/jgit/org.eclipse.jgit:jgit",
"//lib/log:api",
],
)

View File

@ -22,17 +22,18 @@ import com.google.common.base.Predicates;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.flogger.FluentLogger;
import com.google.gwtorm.server.OrmException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/** Specific version of a secondary index schema. */
public class Schema<T> {
private static final FluentLogger logger = FluentLogger.forEnclosingClass();
public static class Builder<T> {
private final List<FieldDef<T, ?>> fields = new ArrayList<>();
@ -58,8 +59,6 @@ public class Schema<T> {
}
}
private static final Logger log = LoggerFactory.getLogger(Schema.class);
public static class Values<T> {
private final FieldDef<T, ?> field;
private final Iterable<?> values;
@ -184,7 +183,10 @@ public class Schema<T> {
try {
v = f.get(obj);
} catch (OrmException e) {
log.error("error getting field {} of {}", f.getName(), obj, e);
logger
.atSevere()
.withCause(e)
.log("error getting field %s of %s", f.getName(), obj);
return null;
}
if (v == null) {

View File

@ -17,6 +17,7 @@ package com.google.gerrit.index;
import static com.google.common.base.Preconditions.checkNotNull;
import com.google.common.base.Stopwatch;
import com.google.common.flogger.FluentLogger;
import com.google.common.util.concurrent.ListenableFuture;
import com.google.common.util.concurrent.MoreExecutors;
import java.io.OutputStream;
@ -27,11 +28,9 @@ import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import org.eclipse.jgit.lib.ProgressMonitor;
import org.eclipse.jgit.util.io.NullOutputStream;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public abstract class SiteIndexer<K, V, I extends Index<K, V>> {
private static final Logger log = LoggerFactory.getLogger(SiteIndexer.class);
private static final FluentLogger logger = FluentLogger.forEnclosingClass();
public static class Result {
private final long elapsedNanos;
@ -128,7 +127,7 @@ public abstract class SiteIndexer<K, V, I extends Index<K, V>> {
}
private void fail(Throwable t) {
log.error("Failed to index " + desc, t);
logger.atSevere().withCause(t).log("Failed to index %s", desc);
ok.set(false);
}

View File

@ -5,7 +5,7 @@ java_library(
deps = [
"//java/com/google/gerrit/extensions:api",
"//lib:guava",
"//lib/flogger:api",
"//lib/guice",
"//lib/log:api",
],
)

View File

@ -16,6 +16,7 @@ package com.google.gerrit.lifecycle;
import com.google.common.base.Preconditions;
import com.google.common.collect.Lists;
import com.google.common.flogger.FluentLogger;
import com.google.gerrit.extensions.events.LifecycleListener;
import com.google.gerrit.extensions.registration.RegistrationHandle;
import com.google.inject.Binding;
@ -24,10 +25,11 @@ import com.google.inject.Provider;
import com.google.inject.TypeLiteral;
import com.google.inject.util.Providers;
import java.util.List;
import org.slf4j.LoggerFactory;
/** Tracks and executes registered {@link LifecycleListener}s. */
public class LifecycleManager {
private static final FluentLogger logger = FluentLogger.forEnclosingClass();
private final List<Provider<LifecycleListener>> listeners = newList();
private final List<RegistrationHandle> handles = newList();
@ -105,7 +107,7 @@ public class LifecycleManager {
try {
obj.stop();
} catch (Throwable err) {
LoggerFactory.getLogger(obj.getClass()).warn("Failed to stop", err);
logger.atWarning().withCause(err).log("Failed to stop %s", obj.getClass());
}
startedIndex = i - 1;
}

View File

@ -22,6 +22,7 @@ import com.google.common.base.Joiner;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.ListMultimap;
import com.google.common.collect.Sets;
import com.google.common.flogger.FluentLogger;
import com.google.common.util.concurrent.AbstractFuture;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;
@ -80,12 +81,10 @@ import org.apache.lucene.search.Sort;
import org.apache.lucene.search.TopFieldDocs;
import org.apache.lucene.store.AlreadyClosedException;
import org.apache.lucene.store.Directory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/** Basic Lucene index implementation. */
public abstract class AbstractLuceneIndex<K, V> implements Index<K, V> {
private static final Logger log = LoggerFactory.getLogger(AbstractLuceneIndex.class);
private static final FluentLogger logger = FluentLogger.forEnclosingClass();
static String sortFieldName(FieldDef<?, ?> f) {
return f.getName() + "_SORT";
@ -145,18 +144,19 @@ public abstract class AbstractLuceneIndex<K, V> implements Index<K, V> {
autoCommitWriter.commit();
}
} catch (IOException e) {
log.error("Error committing " + index + " Lucene index", e);
logger.atSevere().withCause(e).log("Error committing %s Lucene index", index);
} catch (OutOfMemoryError e) {
log.error("Error committing " + index + " Lucene index", e);
logger.atSevere().withCause(e).log("Error committing %s Lucene index", index);
try {
autoCommitWriter.close();
} catch (IOException e2) {
log.error(
"SEVERE: Error closing "
+ index
+ " Lucene index after OOM;"
+ " index may be corrupted.",
e);
logger
.atSevere()
.withCause(e)
.log(
"SEVERE: Error closing %s Lucene index after OOM;"
+ " index may be corrupted.",
index);
}
}
},
@ -227,10 +227,13 @@ public abstract class AbstractLuceneIndex<K, V> implements Index<K, V> {
writerThread.shutdown();
try {
if (!writerThread.awaitTermination(5, TimeUnit.SECONDS)) {
log.warn("shutting down " + name + " index with pending Lucene writes");
logger.atWarning().log("shutting down %s index with pending Lucene writes", name);
}
} catch (InterruptedException e) {
log.warn("interrupted waiting for pending Lucene writes of " + name + " index", e);
logger
.atWarning()
.withCause(e)
.log("interrupted waiting for pending Lucene writes of %s index", name);
}
reopenThread.close();
@ -244,7 +247,7 @@ public abstract class AbstractLuceneIndex<K, V> implements Index<K, V> {
try {
searcherManager.maybeRefreshBlocking();
} catch (IOException e) {
log.warn("error finishing pending Lucene writes", e);
logger.atWarning().withCause(e).log("error finishing pending Lucene writes");
}
try {
@ -252,12 +255,12 @@ public abstract class AbstractLuceneIndex<K, V> implements Index<K, V> {
} catch (AlreadyClosedException e) {
// Ignore.
} catch (IOException e) {
log.warn("error closing Lucene writer", e);
logger.atWarning().withCause(e).log("error closing Lucene writer");
}
try {
dir.close();
} catch (IOException e) {
log.warn("error closing Lucene directory", e);
logger.atWarning().withCause(e).log("error closing Lucene directory");
}
}
@ -453,7 +456,7 @@ public abstract class AbstractLuceneIndex<K, V> implements Index<K, V> {
try {
return reopenThread.waitForGeneration(gen, 0);
} catch (InterruptedException e) {
log.warn("Interrupted waiting for searcher generation", e);
logger.atWarning().withCause(e).log("Interrupted waiting for searcher generation");
return false;
}
}
@ -529,7 +532,7 @@ public abstract class AbstractLuceneIndex<K, V> implements Index<K, V> {
try {
release(searcher);
} catch (IOException e) {
log.warn("cannot release Lucene searcher", e);
logger.atWarning().withCause(e).log("cannot release Lucene searcher");
}
}
}

View File

@ -35,10 +35,10 @@ java_library(
"//java/com/google/gerrit/server",
"//lib:guava",
"//lib:gwtorm",
"//lib/flogger:api",
"//lib/guice",
"//lib/guice:guice-assistedinject",
"//lib/jgit/org.eclipse.jgit:jgit",
"//lib/log:api",
"//lib/lucene:lucene-analyzers-common",
"//lib/lucene:lucene-core-and-backward-codecs",
"//lib/lucene:lucene-misc",

View File

@ -34,6 +34,7 @@ import com.google.common.collect.Iterables;
import com.google.common.collect.ListMultimap;
import com.google.common.collect.MultimapBuilder;
import com.google.common.collect.Sets;
import com.google.common.flogger.FluentLogger;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListeningExecutorService;
import com.google.gerrit.index.QueryOptions;
@ -91,8 +92,6 @@ import org.apache.lucene.search.TopFieldDocs;
import org.apache.lucene.store.RAMDirectory;
import org.apache.lucene.util.BytesRef;
import org.eclipse.jgit.lib.Config;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Secondary index implementation using Apache Lucene.
@ -102,7 +101,7 @@ import org.slf4j.LoggerFactory;
* a committed write and it showing up to other threads' searchers.
*/
public class LuceneChangeIndex implements ChangeIndex {
private static final Logger log = LoggerFactory.getLogger(LuceneChangeIndex.class);
private static final FluentLogger logger = FluentLogger.forEnclosingClass();
static final String UPDATED_SORT_FIELD = sortFieldName(ChangeField.UPDATED);
static final String ID_SORT_FIELD = sortFieldName(ChangeField.LEGACY_ID);
@ -380,7 +379,7 @@ public class LuceneChangeIndex implements ChangeIndex {
try {
indexes.get(i).release(searchers[i]);
} catch (IOException e) {
log.warn("cannot release Lucene searcher", e);
logger.atWarning().withCause(e).log("cannot release Lucene searcher");
}
}
}

View File

@ -14,6 +14,7 @@
package com.google.gerrit.lucene;
import com.google.common.flogger.FluentLogger;
import com.google.common.primitives.Ints;
import com.google.gerrit.extensions.registration.DynamicSet;
import com.google.gerrit.index.Index;
@ -33,12 +34,10 @@ import java.nio.file.Path;
import java.util.Collection;
import java.util.TreeMap;
import org.eclipse.jgit.lib.Config;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Singleton
public class LuceneVersionManager extends VersionManager {
private static final Logger log = LoggerFactory.getLogger(LuceneVersionManager.class);
private static final FluentLogger logger = FluentLogger.forEnclosingClass();
static Path getDir(SitePaths sitePaths, String name, Schema<?> schema) {
return sitePaths.index_dir.resolve(String.format("%s_%04d", name, schema.getVersion()));
@ -62,7 +61,7 @@ public class LuceneVersionManager extends VersionManager {
Path p = getDir(sitePaths, def.getName(), schema);
boolean isDir = Files.isDirectory(p);
if (Files.exists(p) && !isDir) {
log.warn("Not a directory: {}", p.toAbsolutePath());
logger.atWarning().log("Not a directory: %s", p.toAbsolutePath());
}
int v = schema.getVersion();
versions.put(v, new Version<>(schema, v, isDir, cfg.getReady(def.getName(), v)));
@ -78,7 +77,7 @@ public class LuceneVersionManager extends VersionManager {
String versionStr = n.substring(prefix.length());
Integer v = Ints.tryParse(versionStr);
if (v == null || versionStr.length() != 4) {
log.warn("Unrecognized version in index directory: {}", p.toAbsolutePath());
logger.atWarning().log("Unrecognized version in index directory: %s", p.toAbsolutePath());
continue;
}
if (!versions.containsKey(v)) {
@ -86,7 +85,7 @@ public class LuceneVersionManager extends VersionManager {
}
}
} catch (IOException e) {
log.error("Error scanning index directory: " + sitePaths.index_dir, e);
logger.atSevere().withCause(e).log("Error scanning index directory: %s", sitePaths.index_dir);
}
return versions;
}

View File

@ -8,8 +8,8 @@ java_library(
"//java/com/google/gerrit/lifecycle",
"//java/org/eclipse/jgit:server",
"//lib:guava",
"//lib/flogger:api",
"//lib/guice",
"//lib/jgit/org.eclipse.jgit:jgit",
"//lib/log:api",
],
)

View File

@ -14,15 +14,14 @@
package com.google.gerrit.metrics.proc;
import com.google.common.flogger.FluentLogger;
import java.lang.management.ManagementFactory;
import java.lang.management.OperatingSystemMXBean;
import java.lang.reflect.Method;
import java.util.Arrays;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
class OperatingSystemMXBeanProvider {
private static final Logger log = LoggerFactory.getLogger(OperatingSystemMXBeanProvider.class);
private static final FluentLogger logger = FluentLogger.forEnclosingClass();
private final OperatingSystemMXBean sys;
private final Method getProcessCpuTime;
@ -41,10 +40,10 @@ class OperatingSystemMXBeanProvider {
return new OperatingSystemMXBeanProvider(sys);
}
} catch (ReflectiveOperationException e) {
log.debug("No implementation for {}", name, e);
logger.atFine().withCause(e).log("No implementation for %s", name);
}
}
log.warn("No implementation of UnixOperatingSystemMXBean found");
logger.atWarning().log("No implementation of UnixOperatingSystemMXBean found");
return null;
}
}

View File

@ -35,11 +35,11 @@ java_library(
"//lib:junit",
"//lib/auto:auto-value",
"//lib/auto:auto-value-annotations",
"//lib/flogger:api",
"//lib/guice",
"//lib/guice:guice-servlet",
"//lib/jgit/org.eclipse.jgit:jgit",
"//lib/jgit/org.eclipse.jgit.junit:junit",
"//lib/log:api",
"//lib/truth",
],
)

View File

@ -19,6 +19,7 @@ import static java.util.stream.Collectors.toList;
import com.google.auto.value.AutoValue;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.flogger.FluentLogger;
import com.google.gerrit.common.Nullable;
import com.google.gerrit.common.errors.EmailException;
import com.google.gerrit.server.git.WorkQueue;
@ -35,8 +36,6 @@ import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Email sender implementation that records messages in memory.
@ -48,7 +47,7 @@ import org.slf4j.LoggerFactory;
*/
@Singleton
public class FakeEmailSender implements EmailSender {
private static final Logger log = LoggerFactory.getLogger(FakeEmailSender.class);
private static final FluentLogger logger = FluentLogger.forEnclosingClass();
public static class Module extends AbstractModule {
@Override
@ -166,7 +165,7 @@ public class FakeEmailSender implements EmailSender {
try {
task.get();
} catch (ExecutionException | InterruptedException e) {
log.warn("error finishing email task", e);
logger.atWarning().withCause(e).log("error finishing email task");
}
}
}

View File

@ -21,6 +21,7 @@ import static java.util.stream.Collectors.toList;
import com.google.common.base.Joiner;
import com.google.common.collect.ImmutableListMultimap;
import com.google.common.collect.ListMultimap;
import com.google.common.flogger.FluentLogger;
import com.google.gerrit.reviewdb.client.Change;
import com.google.gerrit.reviewdb.client.Project;
import com.google.gerrit.reviewdb.client.RefNames;
@ -46,12 +47,10 @@ import java.util.stream.Stream;
import org.eclipse.jgit.errors.RepositoryNotFoundException;
import org.eclipse.jgit.lib.Repository;
import org.junit.runner.Description;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Singleton
public class NoteDbChecker {
static final Logger log = LoggerFactory.getLogger(NoteDbChecker.class);
private static final FluentLogger logger = FluentLogger.forEnclosingClass();
private final Provider<ReviewDb> dbProvider;
private final GitRepositoryManager repoManager;
@ -193,7 +192,7 @@ public class NoteDbChecker {
} catch (Throwable t) {
String msg = "Error converting change: " + c;
msgs.add(msg);
log.error(msg, t);
logger.atSevere().withCause(t).log(msg);
continue;
}
List<String> diff = expected.differencesFrom(actual);

View File

@ -8,8 +8,8 @@ java_library(
"//java/com/google/gerrit/reviewdb:server",
"//java/com/google/gerrit/server",
"//lib:gwtorm",
"//lib/flogger:api",
"//lib/jgit/org.eclipse.jgit:jgit",
"//lib/log:api",
"//lib/prolog:runtime",
],
)

View File

@ -14,6 +14,7 @@
package gerrit;
import com.google.common.flogger.FluentLogger;
import com.google.gerrit.reviewdb.client.Account;
import com.google.gerrit.reviewdb.client.PatchSet;
import com.google.gerrit.server.rules.StoredValues;
@ -25,11 +26,9 @@ import com.googlecode.prolog_cafe.lang.Prolog;
import com.googlecode.prolog_cafe.lang.StructureTerm;
import com.googlecode.prolog_cafe.lang.SymbolTerm;
import com.googlecode.prolog_cafe.lang.Term;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class PRED_uploader_1 extends Predicate.P1 {
private static final Logger log = LoggerFactory.getLogger(PRED_uploader_1.class);
private static final FluentLogger logger = FluentLogger.forEnclosingClass();
private static final SymbolTerm user = SymbolTerm.intern("user", 1);
@ -45,9 +44,11 @@ public class PRED_uploader_1 extends Predicate.P1 {
PatchSet patchSet = StoredValues.getPatchSet(engine);
if (patchSet == null) {
log.error(
"Failed to load current patch set of change "
+ StoredValues.getChange(engine).getChangeId());
logger
.atSevere()
.log(
"Failed to load current patch set of change %s",
StoredValues.getChange(engine).getChangeId());
return engine.fail();
}

View File

@ -6,6 +6,5 @@ java_library(
"//java/com/google/gerrit/util/ssl",
"//lib/commons:codec",
"//lib/commons:net",
"//lib/log:api",
],
)

View File

@ -20,6 +20,7 @@ import static com.google.gerrit.server.group.SystemGroupBackend.REGISTERED_USERS
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.flogger.FluentLogger;
import com.google.gerrit.acceptance.AbstractDaemonTest;
import com.google.gerrit.acceptance.NoHttpd;
import com.google.gerrit.acceptance.Sandboxed;
@ -31,13 +32,11 @@ import java.util.Collections;
import java.util.List;
import java.util.Map;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@NoHttpd
@UseSsh
public class SshCommandsIT extends AbstractDaemonTest {
private static final Logger log = LoggerFactory.getLogger(SshCommandsIT.class);
private static final FluentLogger logger = FluentLogger.forEnclosingClass();
// TODO: It would be better to dynamically generate these lists
private static final List<String> COMMON_ROOT_COMMANDS =
@ -129,7 +128,7 @@ public class SshCommandsIT extends AbstractDaemonTest {
// content of the stderr, which will always start with "gerrit command" when the --help
// option is used.
String cmd = String.format("gerrit%s%s %s", root.isEmpty() ? "" : " ", root, command);
log.debug(cmd);
logger.atFine().log(cmd);
adminSshSession.exec(String.format("%s --help", cmd));
String response = adminSshSession.getError();
assertWithMessage(String.format("command %s failed: %s", command, response))

View File

@ -24,11 +24,11 @@ junit_tests(
"//lib/bouncycastle:bcpg-neverlink",
"//lib/bouncycastle:bcprov",
"//lib/bouncycastle:bcprov-neverlink",
"//lib/flogger:api",
"//lib/guice",
"//lib/guice:guice-assistedinject",
"//lib/jgit/org.eclipse.jgit:jgit",
"//lib/jgit/org.eclipse.jgit.junit:junit",
"//lib/log:api",
"//lib/truth",
],
)

View File

@ -1,18 +1,13 @@
java_library(
name = "api",
data = ["//lib:LICENSE-slf4j"],
visibility = ["//visibility:public"],
visibility = [
"//lib/jgit/org.eclipse.jgit:__pkg__",
"//plugins:__pkg__",
],
exports = ["@log_api//jar"],
)
java_library(
name = "nop",
data = ["//lib:LICENSE-slf4j"],
visibility = ["//visibility:public"],
exports = ["@log_nop//jar"],
runtime_deps = [":api"],
)
java_library(
name = "ext",
data = ["//lib:LICENSE-slf4j"],