Skip to content

Commit

Permalink
Adds an HTTPLogging interceptor #496
Browse files Browse the repository at this point in the history
  • Loading branch information
germanattanasio committed Mar 30, 2017
1 parent 9f40779 commit 620d8d9
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 0 deletions.
31 changes: 31 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ APIs and SDKs that use cognitive computing to solve complex problems.
* [Android](#android)
* [Running in Bluemix](#running-in-bluemix)
* [Default Headers](#default-headers)
* [Debug](#debug)
* [Eclipse and Intellij](#working-with-eclipse-and-intellij-idea)
* [License](#license)
* [Contributing](#contributing)
Expand Down Expand Up @@ -248,6 +249,36 @@ service.setDefaultHeaders(headers);

// All the api calls from now on will send the default headers
```
## Debug

HTTP requests can be logging by adding a `loggging.properties` file to your classpath.

```none
handlers=java.util.logging.ConsoleHandler
java.util.logging.ConsoleHandler.level=FINE
java.util.logging.ConsoleHandler.formatter=java.util.logging.SimpleFormatter
java.util.logging.SimpleFormatter.format=%1$tb %1$td, %1$tY %1$tl:%1$tM:%1$tS %1$Tp %2$s %4$s: %5$s%n
.level=SEVERE
okhttp3.level=FINE
okhttp3.mockwebserver.level=WARNING
com.ibm.watson.level=FINE
com.ibm.watson.developer_cloud.util.level=SEVERE
```

The configuration above will log only the URL and query parameters for each request.

For example:
```none
Mar 30, 2017 7:31:22 PM okhttp3.internal.platform.Platform log
INFO: --> POST https://gateway.watsonplatform.net/tradeoff-analytics/api/v1/dilemmas?generate_visualization=false http/1.1 (923-byte body)
Mar 30, 2017 7:31:22 PM okhttp3.internal.platform.Platform log
INFO: <-- 200 OK https://gateway.watsonplatform.net/tradeoff-analytics/api/v1/dilemmas?generate_visualization=false (104ms, unknown-length body)
Mar 30, 2017 7:31:23 PM okhttp3.internal.platform.Platform log
INFO: --> POST https://gateway.watsonplatform.net/tradeoff-analytics/api/v1/dilemmas?generate_visualization=true http/1.1 (12398-byte body)
Mar 30, 2017 7:31:35 PM okhttp3.internal.platform.Platform log
INFO: <-- 200 OK https://gateway.watsonplatform.net/tradeoff-analytics/api/v1/dilemmas?generate_visualization=true (12311ms, unknown-length body)
```
**Warning:** The logs generated by this logger when using the level `FINE` or `ALL` has the potential to leak sensitive information such as "Authorization" or "Cookie" headers and the contents of request and response bodies. This data should only be logged in a controlled way or in a non-production environment.

## Build + Test

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import com.ibm.watson.developer_cloud.service.exception.UnauthorizedException;
import com.ibm.watson.developer_cloud.service.exception.UnsupportedException;
import com.ibm.watson.developer_cloud.util.CredentialUtils;
import com.ibm.watson.developer_cloud.util.HttpLogging;
import com.ibm.watson.developer_cloud.util.RequestUtils;
import com.ibm.watson.developer_cloud.util.ResponseConverterUtils;
import com.ibm.watson.developer_cloud.util.ResponseUtils;
Expand Down Expand Up @@ -124,6 +125,7 @@ protected OkHttpClient configureHttpClient() {
builder.writeTimeout(60, TimeUnit.SECONDS);
builder.readTimeout(90, TimeUnit.SECONDS);

builder.addNetworkInterceptor(HttpLogging.getLoggingInterceptor());
return builder.build();
}

Expand Down
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;
}
}

0 comments on commit 620d8d9

Please sign in to comment.