Skip to content

Commit

Permalink
fix reviewing comments
Browse files Browse the repository at this point in the history
   + value change of debug mode logLevel is not supported
   + LOG_LEVEL_PROPERTY instead of LOG_LEVEL_PROP
  • Loading branch information
lxhoan committed May 31, 2018
1 parent 75b6ada commit 8871d01
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 74 deletions.
6 changes: 0 additions & 6 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -245,12 +245,6 @@
<version>2.18.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.github.stephenc.jcip</groupId>
<artifactId>jcip-annotations</artifactId>
<version>1.0-1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.squareup.retrofit2</groupId>
<artifactId>retrofit</artifactId>
Expand Down
21 changes: 20 additions & 1 deletion src/main/java/org/influxdb/InfluxDB.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.influxdb;

import org.influxdb.InfluxDB.LogLevel;
import org.influxdb.dto.BatchPoints;
import org.influxdb.dto.Point;
import org.influxdb.dto.Pong;
Expand Down Expand Up @@ -30,7 +31,7 @@ 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_PROP = "org.influxdb.InfluxDB.logLevel";
public static final String LOG_LEVEL_PROPERTY = "org.influxdb.InfluxDB.logLevel";

/** Controls the level of logging of the REST layer. */
public enum LogLevel {
Expand All @@ -46,6 +47,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
21 changes: 7 additions & 14 deletions src/main/java/org/influxdb/impl/InfluxDBImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public class InfluxDBImpl implements InfluxDB {
static final okhttp3.MediaType MEDIA_TYPE_STRING = MediaType.parse("text/plain");

private static final String SHOW_DATABASE_COMMAND_ENCODED = Query.encode("SHOW DATABASES");
private static final String DEBUG_MODE_LOG_LEVEL = System.getProperty(LOG_LEVEL_PROPERTY);

private final InetAddress hostAddress;
private final String username;
Expand Down Expand Up @@ -86,8 +87,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();
initLogLevel();
setLogLevel(LogLevel.parseLogLevel(DEBUG_MODE_LOG_LEVEL));

this.gzipRequestInterceptor = new GzipRequestInterceptor();
this.retrofit = new Retrofit.Builder()
.baseUrl(url)
Expand All @@ -104,8 +107,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();
initLogLevel();
setLogLevel(LogLevel.parseLogLevel(DEBUG_MODE_LOG_LEVEL));

this.gzipRequestInterceptor = new GzipRequestInterceptor();
this.retrofit = new Retrofit.Builder()
.baseUrl(url)
Expand Down Expand Up @@ -740,16 +745,4 @@ public void dropRetentionPolicy(final String rpName, final String database) {
Query.encode(queryBuilder.toString())));
}

private void initLogLevel() {
String value = System.getProperty(LOG_LEVEL_PROP);
LogLevel logLevel = LogLevel.NONE;

if (value != null) {
try {
logLevel = LogLevel.valueOf(value.toUpperCase());
} catch (IllegalArgumentException e) {
}
}
setLogLevel(logLevel);
}
}
53 changes: 0 additions & 53 deletions src/test/java/org/influxdb/InfluxDBLogLevelTest.java

This file was deleted.

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 8871d01

Please sign in to comment.