-
Notifications
You must be signed in to change notification settings - Fork 532
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds an HTTPLogging interceptor #496
- Loading branch information
1 parent
9f40779
commit 620d8d9
Showing
3 changed files
with
81 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
core/src/main/java/com/ibm/watson/developer_cloud/util/HttpLogging.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* | ||
* Copyright 2017 IBM Corp. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations under the License. | ||
*/ | ||
package com.ibm.watson.developer_cloud.util; | ||
|
||
import java.util.logging.Logger; | ||
|
||
import com.ibm.watson.developer_cloud.service.WatsonService; | ||
|
||
import okhttp3.logging.HttpLoggingInterceptor; | ||
import okhttp3.logging.HttpLoggingInterceptor.Level; | ||
|
||
/** | ||
* HttpLogging logs HTTP request and response data. | ||
* | ||
* Instantiates a new HTTP logging. The logging level will be determinate by the {@link Logger} used in this class. | ||
* Basic HTTP request response will be log if Level is INFO, HEADERS if level is FINE and all the bodies if Level is | ||
* ALL. | ||
*/ | ||
public class HttpLogging { | ||
private static final Logger LOG = Logger.getLogger(WatsonService.class.getName()); | ||
|
||
private HttpLogging() { } | ||
|
||
private static final HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor(); | ||
static { | ||
if (LOG.isLoggable(java.util.logging.Level.ALL)) { | ||
loggingInterceptor.setLevel(Level.BODY); | ||
} else if (LOG.isLoggable(java.util.logging.Level.FINE)) { | ||
loggingInterceptor.setLevel(Level.HEADERS); | ||
} else if (LOG.isLoggable(java.util.logging.Level.INFO)) { | ||
loggingInterceptor.setLevel(Level.BASIC); | ||
} | ||
} | ||
|
||
public static HttpLoggingInterceptor getLoggingInterceptor() { | ||
return loggingInterceptor; | ||
} | ||
} |