Skip to content

Commit

Permalink
PR feedback: Make FAUNA_DEBUG environment variable binary.
Browse files Browse the repository at this point in the history
  • Loading branch information
David Griffin committed Oct 3, 2024
1 parent 22055b5 commit 148a9b5
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 31 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -582,10 +582,10 @@ public class App {
```

## Debugging / Tracing

If you would like to see the requests and responses the client is making and receiving, you can set the environment
variable `FAUNA_DEBUG=DEBUG`, to see the request bodies you can set `FAUNA_DEBUG=TRACE`. You can also pass in your own
log handler when constructing the client.
variable `FAUNA_DEBUG=1`. Fauna log the request and response (including headers) to `stderr`. You can also pass in your
own log handler. Setting `Level.WARNING` is equivalent to `FAUNA_DEBUG=0`, while `Level.FINE` is equivalent to
`FAUNA_DEBUG=1`. The client will log the request body at `Level.FINEST`.

```java
import java.util.logging.ConsoleHandler;
Expand Down
39 changes: 11 additions & 28 deletions src/main/java/com/fauna/client/FaunaConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,6 @@
import java.util.logging.Handler;
import java.util.logging.Level;

import static com.fauna.client.FaunaConfig.FaunaDebug.DEBUG;
import static com.fauna.client.FaunaConfig.FaunaDebug.ERROR;
import static com.fauna.client.FaunaConfig.FaunaDebug.INFO;
import static com.fauna.client.FaunaConfig.FaunaDebug.TRACE;
import static com.fauna.client.FaunaConfig.FaunaDebug.WARNING;


/**
* FaunaConfig is a configuration class used to set up and configure a connection to Fauna.
* It encapsulates various settings such as the endpoint URL, secret key, query timeout, and others.
Expand All @@ -23,14 +16,6 @@ public static class FaunaEndpoint {
public static final String LOCAL = "http://localhost:8443";
}

public static class FaunaDebug {
public static final String ERROR = "ERROR";
public static final String WARNING = "WARNING";
public static final String INFO = "INFO";
public static final String DEBUG = "DEBUG";
public static final String TRACE = "TRACE";
}

private final String endpoint;
private final String secret;
private final int maxContentionRetries;
Expand Down Expand Up @@ -105,24 +90,22 @@ public static class Builder {
private int maxContentionRetries = 3;
private Handler logHandler = defaultLogHandler();

private static Level getLogLevel(String level) {
// Map more commonly used log-level names to Java log levels:
// https://logging.apache.org/log4j/2.x/manual/customloglevels.html
// https://docs.python.org/3/library/logging.html#logging-levels
// https://developer.mozilla.org/en-US/docs/Web/API/console/debug_static
switch (level.toUpperCase()) {
case ERROR: return Level.SEVERE;
case INFO: return Level.INFO;
case DEBUG: return Level.FINE;
case TRACE: return Level.FINEST;
case WARNING:
default: return Level.WARNING;
static Level getLogLevel(String debug) {
if (debug == null || debug.isBlank()) {
return Level.WARNING;
} else {
try {
int debugInt = Integer.parseInt(debug);
return debugInt > 0 ? Level.FINE : Level.WARNING;
} catch (NumberFormatException e) {
return Level.FINE;
}
}
}

private static Handler defaultLogHandler() {
Handler logHandler = new ConsoleHandler();
logHandler.setLevel(getLogLevel(FaunaEnvironment.faunaDebug().orElse("")));
logHandler.setLevel(getLogLevel(FaunaEnvironment.faunaDebug().orElse(null)));
return logHandler;
}

Expand Down
14 changes: 14 additions & 0 deletions src/test/java/com/fauna/client/FaunaConfigTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.fauna.client;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

import java.util.logging.ConsoleHandler;
import java.util.logging.Level;
Expand Down Expand Up @@ -36,4 +38,16 @@ public void testOverridingDefaultFaunaConfig() {
assertEquals("foo", config.getSecret());
assertEquals(1, config.getMaxContentionRetries());
}

@ParameterizedTest
@ValueSource(strings = {"1", "DEBUG", "2", "foo", "0.0", " 1", " 1000 "})
public void testDebugLogVals(String val) {
assertEquals(Level.FINE, FaunaConfig.Builder.getLogLevel(val));
}

@ParameterizedTest
@ValueSource(strings = {"0", " ", "-1", "", "\n", " \r \n \t"})
public void testWarningLogVals(String val) {
assertEquals(Level.WARNING, FaunaConfig.Builder.getLogLevel(val));
}
}

0 comments on commit 148a9b5

Please sign in to comment.