Skip to content

Commit b0b6cca

Browse files
authored
Merge pull request #117 from wujinhu/log
add flag to enable/disable logs
2 parents 7e1c994 + 4a6b19a commit b0b6cca

File tree

3 files changed

+28
-5
lines changed

3 files changed

+28
-5
lines changed

src/main/java/com/aliyun/oss/common/comm/ServiceClient.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,8 @@ private ResponseMessage sendRequestImpl(RequestMessage request, ExecutionContext
141141

142142
return response;
143143
} catch (ServiceException sex) {
144-
logException("[Server]Unable to execute HTTP request: ", sex);
144+
logException("[Server]Unable to execute HTTP request: ", sex,
145+
request.getOriginalRequest().isLogEnabled());
145146

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

157159
closeResponseSilently(response);
158160

159161
if (!shouldRetry(cex, request, response, retries, retryStrategy)) {
160162
throw cex;
161163
}
162164
} catch (Exception ex) {
163-
logException("[Unknown]Unable to execute HTTP request: ", ex);
165+
logException("[Unknown]Unable to execute HTTP request: ", ex,
166+
request.getOriginalRequest().isLogEnabled());
164167

165168
closeResponseSilently(response);
166169

src/main/java/com/aliyun/oss/common/utils/LogUtils.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,22 @@ public static Log getLog() {
5353
}
5454

5555
public static <ExType> void logException(String messagePrefix, ExType ex) {
56+
logException(messagePrefix, ex, true);
57+
}
58+
59+
public static <ExType> void logException(String messagePrefix, ExType ex, boolean logEnabled) {
5660

5761
assert (ex instanceof Exception);
5862

5963
String detailMessage = messagePrefix + ((Exception) ex).getMessage();
6064
if (ex instanceof ServiceException && errorCodeFilterList.contains(((ServiceException) ex).getErrorCode())) {
61-
log.info(detailMessage);
65+
if (logEnabled) {
66+
log.info(detailMessage);
67+
}
6268
} else {
63-
log.warn(detailMessage);
69+
if (logEnabled) {
70+
log.warn(detailMessage);
71+
}
6472
}
6573
}
6674
}

src/main/java/com/aliyun/oss/model/WebServiceRequest.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ public abstract class WebServiceRequest {
3131

3232
private ProgressListener progressListener = ProgressListener.NOOP;
3333

34+
//This flag is used to enable/disable INFO and WARNING logs for requests
35+
//We enable INFO and WARNING logs by default.
36+
private boolean logEnabled = true;
37+
3438
private Map<String, String> parameters = new LinkedHashMap<String, String>();
3539
private Map<String, String> headers = new LinkedHashMap<String, String>();
3640

@@ -72,4 +76,12 @@ public void setHeaders(Map<String, String> headers) {
7276
public void addHeader(String key, String value) {
7377
this.headers.put(key, value);
7478
}
79+
80+
public boolean isLogEnabled() {
81+
return logEnabled;
82+
}
83+
84+
public void setLogEnabled(boolean logEnabled) {
85+
this.logEnabled = logEnabled;
86+
}
7587
}

0 commit comments

Comments
 (0)