Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

issues #413 : Debug mode which allows HTTP requests being sent to the… #450

Merged
merged 5 commits into from
Jun 4, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

- Allow write precision of TimeUnit other than Nanoseconds [PR #321](https://github.com/influxdata/influxdb-java/pull/321)
- Support dynamic measurement name in InfluxDBResultMapper [PR #423](https://github.com/influxdata/influxdb-java/pull/423)
- Debug mode which allows HTTP requests being sent to the database to be logged [PR #450](https://github.com/influxdata/influxdb-java/pull/450)

## 2.10 [2018-04-26]

Expand Down
24 changes: 24 additions & 0 deletions src/main/java/org/influxdb/InfluxDB.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@
*/
public interface InfluxDB {

/**
* The system property key to set the http logging level across the JVM.
* @see LogLevel for available values
*/
public static final String LOG_LEVEL_PROPERTY = "org.influxdb.InfluxDB.logLevel";

/** Controls the level of logging of the REST layer. */
public enum LogLevel {
/** No logging. */
Expand All @@ -40,6 +46,24 @@ public enum LogLevel {
* Note: This requires that the entire request and response body be buffered in memory!
*/
FULL;
/**
* Parses the string argument as a LogLevel constant.
* @param value a {@code String} containing the {@code LogLevel constant}
* representation to be parsed
* @return the LogLevel constant representation of the param
* or {@code NONE} for null or any invalid String representation.
*/
public static LogLevel parseLogLevel(final String value) {
LogLevel logLevel = NONE;
if (value != null) {
try {
logLevel = valueOf(value.toUpperCase());
} catch (IllegalArgumentException e) {
}
}

return logLevel;
}
}

/**
Expand Down
17 changes: 15 additions & 2 deletions src/main/java/org/influxdb/impl/InfluxDBImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,14 @@ public class InfluxDBImpl implements InfluxDB {

private static final String SHOW_DATABASE_COMMAND_ENCODED = Query.encode("SHOW DATABASES");

/**
* This static constant holds the http logging log level expected in DEBUG mode
* It is set by System property {@code org.influxdb.InfluxDB.logLevel}.
*
* @see org.influxdb.impl.LOG_LEVEL_PROPERTY
*/
private static final LogLevel LOG_LEVEL = LogLevel.parseLogLevel(System.getProperty(LOG_LEVEL_PROPERTY));

private final InetAddress hostAddress;
private final String username;
private final String password;
Expand All @@ -86,8 +94,10 @@ public InfluxDBImpl(final String url, final String username, final String passwo
this.hostAddress = parseHostAddress(url);
this.username = username;
this.password = password;

this.loggingInterceptor = new HttpLoggingInterceptor();
this.loggingInterceptor.setLevel(Level.NONE);
setLogLevel(LOG_LEVEL);

this.gzipRequestInterceptor = new GzipRequestInterceptor();
this.retrofit = new Retrofit.Builder()
.baseUrl(url)
Expand All @@ -104,8 +114,10 @@ public InfluxDBImpl(final String url, final String username, final String passwo
this.hostAddress = parseHostAddress(url);
this.username = username;
this.password = password;

this.loggingInterceptor = new HttpLoggingInterceptor();
this.loggingInterceptor.setLevel(Level.NONE);
setLogLevel(LOG_LEVEL);

this.gzipRequestInterceptor = new GzipRequestInterceptor();
this.retrofit = new Retrofit.Builder()
.baseUrl(url)
Expand Down Expand Up @@ -739,4 +751,5 @@ public void dropRetentionPolicy(final String rpName, final String database) {
execute(this.influxDBService.postQuery(this.username, this.password,
Query.encode(queryBuilder.toString())));
}

}
33 changes: 33 additions & 0 deletions src/test/java/org/influxdb/LogLevelTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package org.influxdb;

import java.util.HashMap;
import java.util.Map;

import org.influxdb.InfluxDB.LogLevel;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.platform.runner.JUnitPlatform;
import org.junit.runner.RunWith;

/**
* Test the InfluxDBimpl log level setting from system property.
*
* @author hoan.le [at] bonitoo.io
*
*/
@RunWith(JUnitPlatform.class)
public class LogLevelTest {
@Test
public void testParseLogLevel() {
Map<String, LogLevel> logLevelMap = new HashMap<>();
logLevelMap.put(null, LogLevel.NONE);
logLevelMap.put("NONE", LogLevel.NONE);
logLevelMap.put("BASIC", LogLevel.BASIC);
logLevelMap.put("HEADERS", LogLevel.HEADERS);
logLevelMap.put("FULL", LogLevel.FULL);
logLevelMap.put("abc", LogLevel.NONE);
logLevelMap.forEach((value, logLevel) -> {
Assertions.assertEquals(LogLevel.parseLogLevel(value), logLevel);
});
}
}