Skip to content

Commit

Permalink
Add loglevel config
Browse files Browse the repository at this point in the history
  • Loading branch information
tommysitu committed Jul 19, 2019
1 parent 65325fa commit 41918b1
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 7 deletions.
13 changes: 10 additions & 3 deletions docs/pages/corefunctionality/configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ If you are developing behind a cooperate proxy, you can configure Hoverfly to us
Logging
-------
Hoverfly logs to SLF4J by default, meaning that you can control log level using standard logging configuration. Here is an example ``logback.xml`` that set
Hoverfly log level to WARN:
Hoverfly logs to SLF4J by default, meaning that you have control of Hoverfly logs using JAVA logging framework.
Here is an example ``logback.xml`` that directs Hoverfly WARN logs to the console:

.. code-block:: xml
Expand All @@ -74,7 +74,7 @@ Hoverfly log level to WARN:
<root level="INFO">
<appender-ref ref="CONSOLE"/>
</root>
<logger name="hoverfly" level="WARN">
<logger name="hoverfly" level="WARN" additivity="false">
<appender-ref ref="CONSOLE" />
</logger>
Expand All @@ -93,6 +93,13 @@ Or change the log output directly to stdout:
localConfigs().logToStdOut()
Hoverfly by default generates ``INFO`` logs regardless of the external SLF4J logger configs. To get debug logging, you need
to set the log level explicitly:

.. code-block:: java
localConfigs().logLevel(LogLevel.DEBUG)
Middleware
----------
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/io/specto/hoverfly/junit/core/Hoverfly.java
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,10 @@ private void startHoverflyProcess() {
commands.add("json");
}

if (hoverflyConfig.getLogLevel().isPresent()) {
commands.add("-log-level");
commands.add(hoverflyConfig.getLogLevel().get().name().toLowerCase());
}

if (hoverflyConfig.isMiddlewareEnabled()) {
final String path = hoverflyConfig.getLocalMiddleware().getPath();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public class HoverflyConfiguration {
private boolean plainHttpTunneling;
private String upstreamProxy;
private Logger hoverflyLogger;
private LogLevel logLevel;
private boolean statefulCapture;
private SimulationPreprocessor simulationPreprocessor;
private String binaryNameFormat;
Expand All @@ -55,7 +56,7 @@ public class HoverflyConfiguration {
final boolean webServer,
final boolean statefulCapture,
final SimulationPreprocessor preprocessor) {
this(proxyPort, adminPort, proxyLocalHost, destination, proxyCaCertificate, captureHeaders, webServer, null, statefulCapture, preprocessor);
this(proxyPort, adminPort, proxyLocalHost, destination, proxyCaCertificate, captureHeaders, webServer, null, null, statefulCapture, preprocessor);
setScheme(scheme);
setHost(host);
this.authToken = authToken;
Expand All @@ -74,6 +75,7 @@ public HoverflyConfiguration(final int proxyPort,
final List<String> captureHeaders,
final boolean webServer,
final Logger hoverflyLogger,
final LogLevel logLevel,
final boolean statefulCapture,
final SimulationPreprocessor preprocessor
) {
Expand All @@ -85,6 +87,7 @@ public HoverflyConfiguration(final int proxyPort,
this.captureHeaders = captureHeaders;
this.webServer = webServer;
this.hoverflyLogger = hoverflyLogger;
this.logLevel = logLevel;
this.statefulCapture = statefulCapture;
this.simulationPreprocessor = preprocessor;
}
Expand Down Expand Up @@ -239,6 +242,10 @@ public Optional<Logger> getHoverflyLogger() {
return Optional.ofNullable(hoverflyLogger);
}

public Optional<LogLevel> getLogLevel() {
return Optional.ofNullable(logLevel);
}

public boolean isStatefulCapture() {
return statefulCapture;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.Optional;

/**
* Config builder interface for settings specific to {@link Hoverfly} managed internally
Expand All @@ -36,6 +35,7 @@ public class LocalHoverflyConfig extends HoverflyConfig {
private LocalMiddleware localMiddleware;
private String upstreamProxy;
private Logger hoverflyLogger = LoggerFactory.getLogger("hoverfly");
private LogLevel logLevel;
private List<String> commands = new LinkedList<>();

/**
Expand All @@ -46,6 +46,7 @@ public class LocalHoverflyConfig extends HoverflyConfig {
*/
public LocalHoverflyConfig sslCertificatePath(String sslCertificatePath) {
this.sslCertificatePath = sslCertificatePath;

return this;
}

Expand Down Expand Up @@ -125,6 +126,16 @@ public LocalHoverflyConfig logToStdOut() {
return this;
}

/**
* Set the log level of Hoverfly. The default level is INFO.
* @param logLevel {@link LogLevel} to set
* @return the {@link HoverflyConfig} for further customizations
*/
public LocalHoverflyConfig logLevel(LogLevel logLevel) {
this.logLevel = logLevel;
return this;
}

/**
* Set additional commands for starting Hoverfly.
* @param commands More Hoverfly command flags.
Expand All @@ -139,7 +150,7 @@ public LocalHoverflyConfig addCommands(String... commands) {
@Override
public HoverflyConfiguration build() {
HoverflyConfiguration configs = new HoverflyConfiguration(proxyPort, adminPort, proxyLocalHost, destination,
proxyCaCert, captureHeaders, webServer, hoverflyLogger, statefulCapture, simulationPreprocessor);
proxyCaCert, captureHeaders, webServer, hoverflyLogger, logLevel, statefulCapture, simulationPreprocessor);
configs.setSslCertificatePath(sslCertificatePath);
configs.setSslKeyPath(sslKeyPath);
configs.setTlsVerificationDisabled(tlsVerificationDisabled);
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/io/specto/hoverfly/junit/core/config/LogLevel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package io.specto.hoverfly.junit.core.config;

public enum LogLevel {

DEBUG,
INFO,
WARN,
ERROR,
FATAL,
PANIC
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@

import io.specto.hoverfly.junit.core.config.HoverflyConfiguration;
import java.net.InetSocketAddress;
import java.util.Optional;

import io.specto.hoverfly.junit.core.config.LogLevel;
import org.junit.Rule;
import org.junit.Test;
import org.junit.contrib.java.lang.system.EnvironmentVariables;
import org.slf4j.LoggerFactory;

import static io.specto.hoverfly.junit.core.HoverflyConfig.localConfigs;
import static io.specto.hoverfly.junit.core.HoverflyConfig.remoteConfigs;
Expand Down Expand Up @@ -35,6 +39,8 @@ public void shouldHaveDefaultSettings() {
assertThat(configs.isWebServer()).isFalse();
assertThat(configs.isTlsVerificationDisabled()).isFalse();
assertThat(configs.isStatefulCapture()).isFalse();
assertThat(configs.getLogLevel()).isNotPresent();
assertThat(configs.getHoverflyLogger()).isEqualTo(Optional.of(LoggerFactory.getLogger("hoverfly")));
}

@Test
Expand Down Expand Up @@ -173,4 +179,11 @@ public void shouldAddCommands() {

assertThat(configs.getCommands()).containsExactly("-log-level", "error", "-disable-cache", "-generate-ca-cert");
}

@Test
public void shouldSetLogLevel() {
HoverflyConfiguration configs = localConfigs().logLevel(LogLevel.DEBUG).build();

assertThat(configs.getLogLevel()).isEqualTo(Optional.of(LogLevel.DEBUG));
}
}
20 changes: 20 additions & 0 deletions src/test/java/io/specto/hoverfly/junit/core/HoverflyTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import io.specto.hoverfly.junit.api.model.ModeArguments;
import io.specto.hoverfly.junit.api.view.HoverflyInfoView;
import io.specto.hoverfly.junit.core.config.LocalHoverflyConfig;
import io.specto.hoverfly.junit.core.config.LogLevel;
import io.specto.hoverfly.junit.core.model.DelaySettings;
import io.specto.hoverfly.junit.core.model.RequestFieldMatcher;
import io.specto.hoverfly.junit.core.model.RequestResponsePair;
Expand All @@ -30,6 +31,9 @@
import org.mockito.Mockito;
import org.powermock.reflect.Whitebox;
import org.slf4j.LoggerFactory;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;
import org.zeroturnaround.exec.StartedProcess;

import javax.net.ssl.SSLContext;
Expand Down Expand Up @@ -66,6 +70,22 @@ public void shouldStartHoverflyOnConfiguredPort() {
assertThat(hoverfly.getHoverflyConfig().getProxyPort()).isEqualTo(EXPECTED_PROXY_PORT);
}

@Test
public void shouldSetDebugLogging() {
systemOut.enableLog();
hoverfly = new Hoverfly(localConfigs().logToStdOut().logLevel(LogLevel.DEBUG), SIMULATE);
hoverfly.start();

RestTemplate restTemplate = new RestTemplate();

try {
restTemplate.getForEntity("https://test.api", Void.class);
} catch (RestClientException ignored) {
}

assertThat(systemOut.getLogWithNormalizedLineSeparator()).contains("Checking cache for request");
}

@Test
public void shouldLogToStdOut() {
final Appender<ILoggingEvent> appender = Mockito.mock(Appender.class);
Expand Down
2 changes: 1 addition & 1 deletion src/test/resources/logback.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<root level="${LOG_LEVEL}">
<appender-ref ref="CONSOLE"/>
</root>
<logger name="hoverfly" level="WARN">
<logger name="hoverfly" level="INFO" additivity="false">
<appender-ref ref="CONSOLE" />
</logger>

Expand Down

0 comments on commit 41918b1

Please sign in to comment.