Skip to content

Commit

Permalink
add flag to enable/disable logs
Browse files Browse the repository at this point in the history
  • Loading branch information
wujinhu committed Jan 18, 2018
1 parent 7e1c994 commit 4a6b19a
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 5 deletions.
9 changes: 6 additions & 3 deletions src/main/java/com/aliyun/oss/common/comm/ServiceClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,8 @@ private ResponseMessage sendRequestImpl(RequestMessage request, ExecutionContext

return response;
} catch (ServiceException sex) {
logException("[Server]Unable to execute HTTP request: ", sex);
logException("[Server]Unable to execute HTTP request: ", sex,
request.getOriginalRequest().isLogEnabled());

// Notice that the response should not be closed in the
// finally block because if the request is successful,
Expand All @@ -152,15 +153,17 @@ private ResponseMessage sendRequestImpl(RequestMessage request, ExecutionContext
throw sex;
}
} catch (ClientException cex) {
logException("[Client]Unable to execute HTTP request: ", cex);
logException("[Client]Unable to execute HTTP request: ", cex,
request.getOriginalRequest().isLogEnabled());

closeResponseSilently(response);

if (!shouldRetry(cex, request, response, retries, retryStrategy)) {
throw cex;
}
} catch (Exception ex) {
logException("[Unknown]Unable to execute HTTP request: ", ex);
logException("[Unknown]Unable to execute HTTP request: ", ex,
request.getOriginalRequest().isLogEnabled());

closeResponseSilently(response);

Expand Down
12 changes: 10 additions & 2 deletions src/main/java/com/aliyun/oss/common/utils/LogUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,22 @@ public static Log getLog() {
}

public static <ExType> void logException(String messagePrefix, ExType ex) {
logException(messagePrefix, ex, true);
}

public static <ExType> void logException(String messagePrefix, ExType ex, boolean logEnabled) {

assert (ex instanceof Exception);

String detailMessage = messagePrefix + ((Exception) ex).getMessage();
if (ex instanceof ServiceException && errorCodeFilterList.contains(((ServiceException) ex).getErrorCode())) {
log.info(detailMessage);
if (logEnabled) {
log.info(detailMessage);
}
} else {
log.warn(detailMessage);
if (logEnabled) {
log.warn(detailMessage);
}
}
}
}
12 changes: 12 additions & 0 deletions src/main/java/com/aliyun/oss/model/WebServiceRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ public abstract class WebServiceRequest {

private ProgressListener progressListener = ProgressListener.NOOP;

//This flag is used to enable/disable INFO and WARNING logs for requests
//We enable INFO and WARNING logs by default.
private boolean logEnabled = true;

private Map<String, String> parameters = new LinkedHashMap<String, String>();
private Map<String, String> headers = new LinkedHashMap<String, String>();

Expand Down Expand Up @@ -72,4 +76,12 @@ public void setHeaders(Map<String, String> headers) {
public void addHeader(String key, String value) {
this.headers.put(key, value);
}

public boolean isLogEnabled() {
return logEnabled;
}

public void setLogEnabled(boolean logEnabled) {
this.logEnabled = logEnabled;
}
}

0 comments on commit 4a6b19a

Please sign in to comment.