From 4a6b19afbdff4a2048559da367f7909ca776964b Mon Sep 17 00:00:00 2001 From: wujinhu Date: Thu, 18 Jan 2018 14:38:23 +0800 Subject: [PATCH] add flag to enable/disable logs --- .../com/aliyun/oss/common/comm/ServiceClient.java | 9 ++++++--- .../java/com/aliyun/oss/common/utils/LogUtils.java | 12 ++++++++++-- .../java/com/aliyun/oss/model/WebServiceRequest.java | 12 ++++++++++++ 3 files changed, 28 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/aliyun/oss/common/comm/ServiceClient.java b/src/main/java/com/aliyun/oss/common/comm/ServiceClient.java index 5f33406f..9950b19d 100644 --- a/src/main/java/com/aliyun/oss/common/comm/ServiceClient.java +++ b/src/main/java/com/aliyun/oss/common/comm/ServiceClient.java @@ -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, @@ -152,7 +153,8 @@ 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); @@ -160,7 +162,8 @@ private ResponseMessage sendRequestImpl(RequestMessage request, ExecutionContext 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); diff --git a/src/main/java/com/aliyun/oss/common/utils/LogUtils.java b/src/main/java/com/aliyun/oss/common/utils/LogUtils.java index 6ecabe6e..e65cecc4 100644 --- a/src/main/java/com/aliyun/oss/common/utils/LogUtils.java +++ b/src/main/java/com/aliyun/oss/common/utils/LogUtils.java @@ -53,14 +53,22 @@ public static Log getLog() { } public static void logException(String messagePrefix, ExType ex) { + logException(messagePrefix, ex, true); + } + + public static 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); + } } } } diff --git a/src/main/java/com/aliyun/oss/model/WebServiceRequest.java b/src/main/java/com/aliyun/oss/model/WebServiceRequest.java index 8113eba0..538c8661 100644 --- a/src/main/java/com/aliyun/oss/model/WebServiceRequest.java +++ b/src/main/java/com/aliyun/oss/model/WebServiceRequest.java @@ -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 parameters = new LinkedHashMap(); private Map headers = new LinkedHashMap(); @@ -72,4 +76,12 @@ public void setHeaders(Map 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; + } }