Skip to content

Commit

Permalink
Merge pull request #450 from bonitoo-io/influxdb-java-issue-413
Browse files Browse the repository at this point in the history
issues #413 : Debug mode which allows HTTP requests being sent to the…
  • Loading branch information
majst01 authored Jun 4, 2018
2 parents c9ec49c + a7729c2 commit 6049aff
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 2 deletions.
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);
});
}
}

0 comments on commit 6049aff

Please sign in to comment.