Migrate to Flogger (green)

This is a 'green' Flogger migration CL. Green CLs are intended to be as
safe as possible and should be easy to review and submit.

No changes should be necessary to the code itself prior to submission,
but small changes to BUILD files may be required.

Changes within files are completely independent of each other, so this CL
can be safely split up for review using tools such as Rosie.

For more information, see []
Base CL: 197826149

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=198560170
This commit is contained in:
jianglai 2018-05-30 07:58:51 -07:00
parent 0d2fb3a8f0
commit 70b13596e4
178 changed files with 984 additions and 988 deletions

View file

@ -11,7 +11,6 @@ java_library(
exclude = ["Modules.java"],
),
deps = [
"//java/com/google/common/logging:formatting_logger",
"//java/google/registry/request/auth",
"//java/google/registry/request/lock",
"//java/google/registry/util",
@ -19,6 +18,8 @@ java_library(
"@com_google_auto_value",
"@com_google_code_findbugs_jsr305",
"@com_google_dagger",
"@com_google_flogger",
"@com_google_flogger_system_backend",
"@com_google_guava",
"@com_googlecode_json_simple",
"@javax_servlet_api",

View file

@ -16,7 +16,7 @@ package google.registry.request;
import static com.google.common.html.HtmlEscapers.htmlEscaper;
import com.google.common.logging.FormattingLogger;
import com.google.common.flogger.FluentLogger;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
@ -26,7 +26,7 @@ public abstract class HttpException extends RuntimeException {
// as per https://tools.ietf.org/html/rfc4918
private static final int SC_UNPROCESSABLE_ENTITY = 422;
private static final FormattingLogger logger = FormattingLogger.getLoggerForCallerClass();
private static final FluentLogger logger = FluentLogger.forEnclosingClass();
private final int responseCode;
@ -57,7 +57,7 @@ public abstract class HttpException extends RuntimeException {
*/
public final void send(HttpServletResponse rsp) throws IOException {
rsp.sendError(getResponseCode(), htmlEscaper().escape(getMessage()));
logger.infofmt(getCause(), "%s", this);
logger.atInfo().withCause(getCause()).log("%s", this);
}
/**

View file

@ -20,7 +20,7 @@ import static javax.servlet.http.HttpServletResponse.SC_FORBIDDEN;
import static javax.servlet.http.HttpServletResponse.SC_METHOD_NOT_ALLOWED;
import static javax.servlet.http.HttpServletResponse.SC_NOT_FOUND;
import com.google.common.logging.FormattingLogger;
import com.google.common.flogger.FluentLogger;
import google.registry.request.auth.AuthResult;
import google.registry.request.auth.RequestAuthenticator;
import google.registry.util.TypeUtils.TypeInstantiator;
@ -59,7 +59,7 @@ import javax.servlet.http.HttpServletResponse;
*/
public class RequestHandler<C> {
private static final FormattingLogger logger = FormattingLogger.getLoggerForCallerClass();
private static final FluentLogger logger = FluentLogger.forEnclosingClass();
private final Router router;
private final Provider<? extends RequestComponentBuilder<C>> requestComponentBuilderProvider;
@ -115,19 +115,19 @@ public class RequestHandler<C> {
try {
method = Action.Method.valueOf(req.getMethod());
} catch (IllegalArgumentException e) {
logger.infofmt("Unsupported method: %s", req.getMethod());
logger.atInfo().log("Unsupported method: %s", req.getMethod());
rsp.sendError(SC_METHOD_NOT_ALLOWED);
return;
}
String path = req.getRequestURI();
Optional<Route> route = router.route(path);
if (!route.isPresent()) {
logger.infofmt("No action found for: %s", path);
logger.atInfo().log("No action found for: %s", path);
rsp.sendError(SC_NOT_FOUND);
return;
}
if (!route.get().isMethodAllowed(method)) {
logger.infofmt("Method %s not allowed for: %s", method, path);
logger.atInfo().log("Method %s not allowed for: %s", method, path);
rsp.sendError(SC_METHOD_NOT_ALLOWED);
return;
}

View file

@ -8,13 +8,14 @@ java_library(
name = "auth",
srcs = glob(["*.java"]),
deps = [
"//java/com/google/common/logging:formatting_logger",
"//java/google/registry/config",
"//java/google/registry/security",
"@com_google_appengine_api_1_0_sdk",
"@com_google_auto_value",
"@com_google_code_findbugs_jsr305",
"@com_google_dagger",
"@com_google_flogger",
"@com_google_flogger_system_backend",
"@com_google_guava",
"@javax_servlet_api",
],

View file

@ -24,7 +24,7 @@ import com.google.appengine.api.oauth.OAuthServiceFailureException;
import com.google.appengine.api.users.User;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.ImmutableSet;
import com.google.common.logging.FormattingLogger;
import com.google.common.flogger.FluentLogger;
import google.registry.config.RegistryConfig.Config;
import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;
@ -38,7 +38,7 @@ public class OAuthAuthenticationMechanism implements AuthenticationMechanism {
private static final String BEARER_PREFIX = "Bearer ";
private static final FormattingLogger logger = FormattingLogger.getLoggerForCallerClass();
private static final FluentLogger logger = FluentLogger.forEnclosingClass();
private final OAuthService oauthService;
@ -76,7 +76,7 @@ public class OAuthAuthenticationMechanism implements AuthenticationMechanism {
String header = request.getHeader(AUTHORIZATION);
if ((header == null) || !header.startsWith(BEARER_PREFIX)) {
if (header != null) {
logger.infofmt("invalid authorization header");
logger.atInfo().log("invalid authorization header");
}
return AuthResult.create(NONE);
}
@ -95,14 +95,15 @@ public class OAuthAuthenticationMechanism implements AuthenticationMechanism {
String[] availableOauthScopeArray = availableOauthScopes.toArray(new String[0]);
currentUser = oauthService.getCurrentUser(availableOauthScopeArray);
isUserAdmin = oauthService.isUserAdmin(availableOauthScopeArray);
logger.infofmt("current user: %s (%s)", currentUser, isUserAdmin ? "admin" : "not admin");
logger.atInfo().log(
"current user: %s (%s)", currentUser, isUserAdmin ? "admin" : "not admin");
clientId = oauthService.getClientId(availableOauthScopeArray);
logger.infofmt("client ID: %s", clientId);
logger.atInfo().log("client ID: %s", clientId);
authorizedScopes =
ImmutableSet.copyOf(oauthService.getAuthorizedScopes(availableOauthScopeArray));
logger.infofmt("authorized scope(s): %s", authorizedScopes);
logger.atInfo().log("authorized scope(s): %s", authorizedScopes);
} catch (OAuthRequestException | OAuthServiceFailureException e) {
logger.infofmt(e, "unable to get OAuth information");
logger.atInfo().withCause(e).log("unable to get OAuth information");
return AuthResult.create(NONE);
}
if ((currentUser == null) || (clientId == null) || (authorizedScopes == null)) {
@ -112,13 +113,13 @@ public class OAuthAuthenticationMechanism implements AuthenticationMechanism {
// Make sure that the client ID matches, to avoid a confused deputy attack; see:
// http://stackoverflow.com/a/17439317/1179226
if (!allowedOauthClientIds.contains(clientId)) {
logger.info("client ID is not allowed");
logger.atInfo().log("client ID is not allowed");
return AuthResult.create(NONE);
}
// Make sure that all required scopes are present.
if (!authorizedScopes.containsAll(requiredOauthScopes)) {
logger.info("required scope(s) missing");
logger.atInfo().log("required scope(s) missing");
return AuthResult.create(NONE);
}

View file

@ -20,7 +20,7 @@ import com.google.auto.value.AutoValue;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Ordering;
import com.google.common.logging.FormattingLogger;
import com.google.common.flogger.FluentLogger;
import com.google.errorprone.annotations.Immutable;
import java.util.Optional;
import javax.inject.Inject;
@ -33,7 +33,7 @@ public class RequestAuthenticator {
private final ImmutableList<AuthenticationMechanism> apiAuthenticationMechanisms;
private final LegacyAuthenticationMechanism legacyAuthenticationMechanism;
private static final FormattingLogger logger = FormattingLogger.getLoggerForCallerClass();
private static final FluentLogger logger = FluentLogger.forEnclosingClass();
@VisibleForTesting
@Inject
@ -108,7 +108,7 @@ public class RequestAuthenticator {
* auth settings are set to NONE -- in this case, NOT_AUTHENTICATED is returned
*/
public Optional<AuthResult> authorize(AuthSettings auth, HttpServletRequest req) {
logger.infofmt("Action requires auth: %s", auth);
logger.atInfo().log("Action requires auth: %s", auth);
AuthResult authResult = authenticate(auth, req);
switch (auth.minimumLevel()) {
case NONE:
@ -116,13 +116,13 @@ public class RequestAuthenticator {
break;
case APP:
if (!authResult.isAuthenticated()) {
logger.warning("Not authorized; no authentication found");
logger.atWarning().log("Not authorized; no authentication found");
return Optional.empty();
}
break;
case USER:
if (authResult.authLevel() != AuthLevel.USER) {
logger.warning("Not authorized; no authenticated user");
logger.atWarning().log("Not authorized; no authenticated user");
// TODO(mountford): change this so that the caller knows to return a more helpful error
return Optional.empty();
}
@ -131,7 +131,7 @@ public class RequestAuthenticator {
switch (auth.userPolicy()) {
case IGNORED:
if (authResult.authLevel() == AuthLevel.USER) {
logger.warning("Not authorized; user policy is IGNORED, but a user was found");
logger.atWarning().log("Not authorized; user policy is IGNORED, but a user was found");
return Optional.empty();
}
break;
@ -141,7 +141,8 @@ public class RequestAuthenticator {
case ADMIN:
if (authResult.userAuthInfo().isPresent()
&& !authResult.userAuthInfo().get().isUserAdmin()) {
logger.warning("Not authorized; user policy is ADMIN, but the user was not an admin");
logger.atWarning().log(
"Not authorized; user policy is ADMIN, but the user was not an admin");
return Optional.empty();
}
break;
@ -166,7 +167,7 @@ public class RequestAuthenticator {
{
AuthResult authResult = appEngineInternalAuthenticationMechanism.authenticate(req);
if (authResult.isAuthenticated()) {
logger.infofmt("Authenticated via internal auth: %s", authResult);
logger.atInfo().log("Authenticated via internal auth: %s", authResult);
return authResult;
}
}
@ -177,7 +178,7 @@ public class RequestAuthenticator {
for (AuthenticationMechanism authMechanism : apiAuthenticationMechanisms) {
AuthResult authResult = authMechanism.authenticate(req);
if (authResult.isAuthenticated()) {
logger.infofmt(
logger.atInfo().log(
"Authenticated via %s: %s", authMechanism.getClass().getSimpleName(), authResult);
return authResult;
}
@ -188,13 +189,13 @@ public class RequestAuthenticator {
// checkAuthConfig will have insured that the user policy is not IGNORED.
AuthResult authResult = legacyAuthenticationMechanism.authenticate(req);
if (authResult.isAuthenticated()) {
logger.infofmt("Authenticated via legacy auth: %s", authResult);
logger.atInfo().log("Authenticated via legacy auth: %s", authResult);
return authResult;
}
break;
}
}
logger.info("No authentication found");
logger.atInfo().log("No authentication found");
return AuthResult.NOT_AUTHENTICATED;
}

View file

@ -8,11 +8,12 @@ java_library(
name = "lock",
srcs = glob(["*.java"]),
deps = [
"//java/com/google/common/logging:formatting_logger",
"//java/google/registry/model",
"//java/google/registry/util",
"@com_google_code_findbugs_jsr305",
"@com_google_dagger",
"@com_google_flogger",
"@com_google_flogger_system_backend",
"@com_google_guava",
"@joda_time",
],

View file

@ -20,7 +20,7 @@ import static com.google.common.base.Throwables.throwIfUnchecked;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Strings;
import com.google.common.collect.ImmutableSortedSet;
import com.google.common.logging.FormattingLogger;
import com.google.common.flogger.FluentLogger;
import com.google.common.util.concurrent.UncheckedExecutionException;
import google.registry.model.server.Lock;
import google.registry.util.AppEngineTimeLimiter;
@ -40,7 +40,7 @@ public class LockHandlerImpl implements LockHandler {
private static final long serialVersionUID = 6551645164118637767L;
private static final FormattingLogger logger = FormattingLogger.getLoggerForCallerClass();
private static final FluentLogger logger = FluentLogger.forEnclosingClass();
/** Fudge factor to make sure we kill threads before a lock actually expires. */
private static final Duration LOCK_TIMEOUT_FUDGE = Duration.standardSeconds(5);
@ -114,10 +114,10 @@ public class LockHandlerImpl implements LockHandler {
for (String lockName : lockNames) {
Optional<Lock> lock = acquire(lockName, tld, leaseLength);
if (!lock.isPresent()) {
logger.infofmt("Couldn't acquire lock named: %s for TLD: %s", lockName, tld);
logger.atInfo().log("Couldn't acquire lock named: %s for TLD: %s", lockName, tld);
return false;
}
logger.infofmt("Acquired lock: %s", lock);
logger.atInfo().log("Acquired lock: %s", lock);
acquiredLocks.add(lock.get());
}
delegate.call();
@ -125,7 +125,7 @@ public class LockHandlerImpl implements LockHandler {
} finally {
for (Lock lock : acquiredLocks) {
lock.release();
logger.infofmt("Released lock: %s", lock);
logger.atInfo().log("Released lock: %s", lock);
}
}
}