Skip to content
This repository has been archived by the owner on Jun 30, 2023. It is now read-only.

Commit

Permalink
add option to customize log level for exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
tangiel committed Dec 18, 2017
1 parent 59bb979 commit af64b9d
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package com.google.api.server.spi;

import java.util.Map;
import java.util.logging.Level;

/**
* Generic service exception that, in addition to a status message, has a status code, and
Expand All @@ -26,6 +27,7 @@ public class ServiceException extends Exception {
protected final int statusCode;
protected final String reason;
protected final String domain;
protected Level logLevel;

public ServiceException(int statusCode, String statusMessage) {
super(statusMessage);
Expand Down Expand Up @@ -99,4 +101,17 @@ public String getDomain() {
public Map<String, String> getHeaders() {
return null;
}

public Level getLogLevel() {
return logLevel == null ? getDefaultLoggingLevel(statusCode) : logLevel;
}

private static Level getDefaultLoggingLevel(int statusCode) {
return statusCode >= 500 ? Level.SEVERE : Level.INFO;
}

public static <T extends ServiceException> T withLogLevel(T exception, Level level) {
exception.logLevel = level;
return exception;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -366,16 +366,15 @@ public void invokeServiceMethod(Object service, Method method, ParamReader param
resultWriter.writeError(new UnauthorizedException(cause));
} else if (cause.getCause() != null && cause.getCause() instanceof ServiceException) {
ServiceException serviceException = (ServiceException) cause.getCause();
level = getLoggingLevel(serviceException);
level = serviceException.getLogLevel();
resultWriter.writeError(serviceException);
} else {
level = Level.SEVERE;
resultWriter.writeError(new InternalServerErrorException(cause));
}
logger.log(level, "exception occurred while calling backend method", cause);
} catch (ServiceException e) {
Level level = getLoggingLevel(e);
logger.log(level, "exception occurred while calling backend method", e);
logger.log(e.getLogLevel(), "exception occurred while calling backend method", e);
resultWriter.writeError(e);
}
}
Expand All @@ -401,10 +400,6 @@ private void validateRegisteredServices(ApiConfigValidator validator) throws Api
}
}

private static Level getLoggingLevel(ServiceException e) {
return e.getStatusCode() >= 500 ? Level.SEVERE : Level.INFO;
}

private static boolean isOAuthRequestException(Class<?> clazz) {
while (Object.class != clazz) {
if (OAUTH_EXCEPTION_CLASS.equals(clazz.getName())) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.google.api.server.spi;

import static com.google.common.truth.Truth.assertThat;

import com.google.api.server.spi.response.UnauthorizedException;
import java.util.logging.Level;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

@RunWith(JUnit4.class)
public class ServiceExceptionTest {
@Test
public void testWithLogLevel() {
UnauthorizedException ex = new UnauthorizedException("");
assertThat(ex.getLogLevel()).isEqualTo(Level.INFO);
assertThat(ServiceException.withLogLevel(ex, Level.WARNING).getLogLevel())
.isEqualTo(Level.WARNING);
}
}

0 comments on commit af64b9d

Please sign in to comment.